Skip to content

Commit d2c2d3d

Browse files
langium: extended Multimap constructor to accept an Iterable of initial data. (#2020)
added corresponding tests
1 parent 910e8a0 commit d2c2d3d

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

packages/langium/src/utils/collections.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export class MultiMap<K, V> {
1515
private map = new Map<K, V[]>();
1616

1717
constructor()
18-
constructor(elements: Array<[K, V]>)
19-
constructor(elements?: Array<[K, V]>) {
18+
constructor(elements: Iterable<[K, V]>)
19+
constructor(elements?: Iterable<[K, V]>) {
2020
if (elements) {
2121
for (const [key, value] of elements) {
2222
this.add(key, value);

packages/langium/test/utils/collections.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@
55
******************************************************************************/
66

77
import { describe, expect, test } from 'vitest';
8-
import { MultiMap } from 'langium';
8+
import { MultiMap, stream } from 'langium';
99

1010
describe('MultiMap', () => {
1111

12+
test('constructor with array', () => {
13+
const multimap = new MultiMap<string, string>([['a', 'foo'], ['a', 'bar'], ['b', 'baz']]);
14+
expect(multimap.size).toBe(3);
15+
expect(multimap.keys().toArray().length).toBe(2);
16+
});
17+
18+
test('constructor with iterable', () => {
19+
const multimap = new MultiMap<string, string>(stream([['a', 'foo'], ['a', 'bar'], ['b', 'baz']]));
20+
expect(multimap.size).toBe(3);
21+
expect(multimap.keys().toArray().length).toBe(2);
22+
});
23+
1224
test('addAll when empty', () => {
1325
const multimap = new MultiMap<string, string>();
1426
multimap.addAll('a', ['foo', 'bar', 'baz']);

0 commit comments

Comments
 (0)