Skip to content

Commit

Permalink
Adding #.multiplicity to MultiMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Yomguithereal committed Nov 6, 2017
1 parent 7f50dee commit 18eb4f4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Adding `#.has` to `FuzzyMap`.
* Adding `#.has` to `FuzzyMultiMap`.
* Adding `#.multiplicity` to `MultiMap`.
* Renaming `RangeMap` to `IncrementalMap`.
* Renaming `Index` to `FuzzyMap`.
* Renaming `MultiIndex` to `FuzzyMultiMap`.
Expand Down
16 changes: 16 additions & 0 deletions multi-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ MultiMap.prototype.get = function(key) {
return this.items.get(key);
};

/**
* Method used to return the multiplicity of the given key, meaning the number
* of times it is set, or, more trivially, the size of the attached container.
*
* @param {any} key - Key to check.
* @return {number}
*/
MultiMap.prototype.multiplicity = function(key) {
var container = this.items.get(key);

if (typeof container === 'undefined')
return 0;

return this.Container === Set ? container.size : container.length;
};

/**
* Method used to iterate over each of the key/value pairs.
*
Expand Down
22 changes: 22 additions & 0 deletions test/multi-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ describe('MultiMap', function() {
assert.strictEqual(map.has('two'), false);
});

it('should be possible to get the multiplicity of a key in the map.', function() {
var map = new MultiMap();

map.set('one', 'hello');
map.set('one', 'world');
map.set('two', 'test');

assert.strictEqual(map.multiplicity('three'), 0);
assert.strictEqual(map.multiplicity('one'), 2);
assert.strictEqual(map.multiplicity('two'), 1);

map = new MultiMap(Set);

map.set('one', 'hello');
map.set('one', 'hello');
map.set('two', 'test');

assert.strictEqual(map.multiplicity('three'), 0);
assert.strictEqual(map.multiplicity('one'), 1);
assert.strictEqual(map.multiplicity('two'), 1);
});

it('should be possible to delete keys.', function() {
var map = new MultiMap();

Expand Down

0 comments on commit 18eb4f4

Please sign in to comment.