8000 Merge pull request #727 from glenjamin/map-factory · githubsheng/immutable-js@d941412 · GitHub
[go: up one dir, main page]

Skip to content

Commit d941412

Browse files
committed
Merge pull request immutable-js#727 from glenjamin/map-factory
Short-hand constructor for Map, taking flat arguments
2 parents 7179438 + 3cbdcc5 commit d941412

File tree

6 files changed

+60
-14
lines changed

6 files changed

+60
-14
lines changed

__tests__/Map.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ describe('Map', () => {
8888
expect(m.toJS()).toEqual({ 'length': 3, '1': 'one' });
8989
});
9090

91+
it('accepts flattened pairs via of()', () => {
92+
var m: Map<any, any> = Map.of(1, 'a', 2, 'b', 3, 'c');
93+
expect(m.size).toBe(3);
94+
expect(m.get(1)).toBe('a');
95+
expect(m.get(2)).toBe('b');
96+
expect(m.get(3)).toBe('c');
97+
});
98+
99+
it('does not accept mismatched flattened pairs via of()', () => {
100+
expect(() => {
101+
Map.of(1, 2, 3);
102+
}).toThrow('Missing value for key: 3');
103+
});
104+
91105
it('converts back to JS object', () => {
92106
var m = Map({'a': 'A', 'b': 'B', 'c': 'C'});
93107
expect(m.toObject()).toEqual({'a': 'A', 'b': 'B', 'c': 'C'});

dist/immutable.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,11 @@ declare module Immutable {
420420
* True if the provided value is a Map
421421
*/
422422
function isMap(maybeMap: any): boolean;
423+
424+
/**
425+
* Creates a new Map from alternating keys and values
426+
*/
427+
function of(...keyValues: any[]): Map<any, any>;
423428
}
424429

425430
/**

dist/immutable.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,17 @@
12221222
});
12231223
}
12241224

1225+
Map.of = function() {var keyValues = SLICE$0.call(arguments, 0);
1226+
return emptyMap().withMutations(function(map ) {
1227+
for (var i = 0; i < keyValues.length; i += 2) {
1228+
if (i + 1 >= keyValues.length) {
1229+
throw new Error('Missing value for key: ' + keyValues[i]);
1230+
}
1231+
map.set(keyValues[i], keyValues[i + 1]);
1232+
}
1233+
});
1234+
};
1235+
12251236
Map.prototype.toString = function() {
12261237
return this.__toString('Map {', '}');
12271238
};

dist/immutable.min.js

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Map.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ export class Map extends KeyedCollection {
3737
});
3838
}
3939

40+
static of(...keyValues) {
41+
return emptyMap().withMutations(map => {
42+
for (var i = 0; i < keyValues.length; i += 2) {
43+
if (i + 1 >= keyValues.length) {
44+
throw new Error('Missing value for key: ' + keyValues[i]);
45+
}
46+
map.set(keyValues[i], keyValues[i + 1]);
47+
}
48+
});
49+
}
50+
4051
toString() {
4152
return this.__toString('Map {', '}');
4253
}

type-definitions/Immutable.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,11 @@ declare module Immutable {
420420
* True if the provided value is a Map
421421
*/
422422
function isMap(maybeMap: any): boolean;
423+
424+
/**
425+
* Creates a new Map from alternating keys and values
426+
*/
427+
function of(...keyValues: any[]): Map<any, any>;
423428
}
424429

425430
/**

0 commit comments

Comments
 (0)
0