Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions multi-map.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,44 @@ interface MultiMapConstructor {
new <K, V>(container?: ArrayConstructor): MultiMap<K, V, V[]>;

from<K, V>(
iterable: Iterable<[K, V]> | {[key: string]: V},
iterable: ReadonlyMap<K, V>,
Container: SetConstructor
): MultiMap<K, V, Set<V>>;
from<V>(
iterable: ReadonlySet<V>,
Container: SetConstructor
): MultiMap<V, V, Set<V>>;
from<V>(
iterable: Iterable<V>,
Container: SetConstructor
): MultiMap<number, V, Set<V>>;
from<V>(
iterable: {[key: string]: V},
Container: SetConstructor
): MultiMap<string, V, Set<V>>;
from<K, V>(
iterable: Iterable<[K, V]> | {[key: string]: V},
iterable: ReadonlyMap<K, V>,
Container?: ArrayConstructor
): MultiMap<K, V, V[]>;
from<V>(
iterable: ReadonlySet<V>,
Container?: ArrayConstructor
): MultiMap<V, V, V[]>;
from<V>(
iterable: Iterable<V>,
Container?: ArrayConstructor
): MultiMap<number, V, V[]>;
from<V>(
iterable: {[key: string]: V},
Container?: ArrayConstructor
): MultiMap<string, V, V[]>;

fromEntries<K, V>(
iterable: Iterable<readonly [K, V]>,
Container: SetConstructor
): MultiMap<K, V, Set<V>>;
fromEntries<K, V>(
iterable: Iterable<readonly [K, V]>,
Container?: ArrayConstructor
): MultiMap<K, V, V[]>;
}
Expand Down
20 changes: 20 additions & 0 deletions multi-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,26 @@ MultiMap.from = function(iterable, Container) {
return map;
};

/**
* Static @.fromEntries function creating a map from key-value entries.
*
* @param {Iterable} iterable - Iterable of [key, value] entries.
* @param {Class} Container - Container.
* @return {MultiMap}
*/
MultiMap.fromEntries = function(iterable, Container) {
var map = new MultiMap(Container);

for (var entry of iterable) {
if (entry === null || (typeof entry !== 'object' && typeof entry !== 'function'))
throw new TypeError('MultiMap.fromEntries: expecting a key-value entry.');

map.set(entry[0], entry[1]);
}

return map;
};

/**
* Exporting.
*/
Expand Down
15 changes: 15 additions & 0 deletions test/exports/exports-named-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ multimap.set(45, 'test');
multimap.get(45).has('test');
let stringMultimap: MultiMap<string, number> = MultiMap.from({one: 1});
stringMultimap.get('one').indexOf(1);
const multimapEntries: ReadonlyArray<readonly [string, number]> = [['one', 1]];
const fromEntries = MultiMap.fromEntries(multimapEntries);
const entryValues: number[] = fromEntries.get('one');
const fromSetEntries = MultiMap.fromEntries(multimapEntries, Set);
const entrySet: Set<number> = fromSetEntries.get('one');
const fromMapEntries: MultiMap<string, number> = MultiMap.fromEntries(new Map([['one', 1]]));
entryValues.indexOf(1);
entrySet.has(1);
fromMapEntries.get('one').indexOf(1);
const indexedMultimap: MultiMap<number, readonly [string, number]> = MultiMap.from(multimapEntries);
const setKeyMultimap: MultiMap<string, string, Set<string>> = MultiMap.from(new Set(['one']), Set);
const mappedMultimap: MultiMap<string, number> = MultiMap.from(new Map([['one', 1]]));
indexedMultimap.get(0).length;
setKeyMultimap.get('one').has('one');
mappedMultimap.get('one').indexOf(1);
for (const _ of multimap) { }
for (const _ of multimap.keys()) { }
for (const _ of multimap.values()) { }
Expand Down
73 changes: 73 additions & 0 deletions test/multi-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,79 @@ describe('MultiMap', function() {
assert.deepStrictEqual(map.get('one'), ['hello']);
});

describe('fromEntries', function() {
it('should group entries by their keys and preserve duplicate values.', function() {
var map = MultiMap.fromEntries([
['one', 'hello'], ['two', 'world'], ['one', 'hello']
]);

assert.strictEqual(map.size, 3);
assert.strictEqual(map.dimension, 2);
assert.deepStrictEqual(map.get('one'), ['hello', 'hello']);
assert.deepStrictEqual(map.get('two'), ['world']);
});

it('should deduplicate values when using Set containers.', function() {
var map = MultiMap.fromEntries([
['one', 'hello'], ['one', 'world'], ['one', 'hello']
], Set);

assert.strictEqual(map.size, 2);
assert.strictEqual(map.dimension, 1);
assert.deepStrictEqual(map.get('one'), new Set(['hello', 'world']));
});

it('should accept maps and preserve object keys.', function() {
var key = {},
map = MultiMap.fromEntries(new Map([[key, 'hello']]));

assert.deepStrictEqual(map.get(key), ['hello']);
});

it('should accept one-shot iterables and empty input.', function() {
function* entries() {
yield ['one', 'hello'];
yield ['one', 'world'];
}

var map = MultiMap.fromEntries(entries());

assert.deepStrictEqual(map.get('one'), ['hello', 'world']);
assert.strictEqual(MultiMap.fromEntries([]).size, 0);
});

it('should reject non-entry values and close their iterator.', function() {
var closed = false;

function* entries() {
try {
yield null;
}
finally {
closed = true;
}
}

assert.throws(function() {
MultiMap.fromEntries(entries());
}, /entry/);
assert.strictEqual(closed, true);

[1, 'ab', undefined].forEach(function(value) {
assert.throws(function() {
MultiMap.fromEntries([value]);
}, /entry/);
});
});

it('should preserve the indexed behavior of from.', function() {
var map = MultiMap.from([['one', 'hello']]);

assert.deepStrictEqual(map.get(0), [['one', 'hello']]);
assert.strictEqual(map.has('one'), false);
});
});

it('should work with vectors.', function() {
var map = new MultiMap(Vector.Uint8Vector);

Expand Down