Skip to content

Commit 5726bd1

Browse files
committed
Follow up #1455 to add Set behavior for no-op maps
1 parent d960d5c commit 5726bd1

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

__tests__/Set.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ describe('Set', () => {
9292
expect(s.toObject()).toEqual({ a: 'a', b: 'b', c: 'c' });
9393
});
9494

95+
it('maps no-ops return the same reference', () => {
96+
const s = Set([1, 2, 3]);
97+
const r = s.map(value => value);
98+
expect(r).toBe(s);
99+
});
100+
95101
it('unions an unknown collection of Sets', () => {
96102
const abc = Set(['a', 'b', 'c']);
97103
const cat = Set(['c', 'a', 't']);

src/Set.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export class Set extends SetCollection {
8080

8181
// @pragma Composition
8282

83+
map(mapper, context) {
84+
return updateSet(this, this._map.map(v => mapper(v, v, this), context));
85+
}
86+
8387
union(...iters) {
8488
iters = iters.filter(x => x.size !== 0);
8589
if (iters.length === 0) {

type-definitions/Immutable.d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,9 +1714,6 @@ declare module Immutable {
17141714
*
17151715
* Set([1,2]).map(x => 10 * x)
17161716
* // Set [10,20]
1717-
*
1718-
* Note: `map()` always returns a new instance, even if it produced the same
1719-
* value at every step.
17201717
*/
17211718
map<M>(
17221719
mapper: (value: T, key: T, iter: this) => M,
@@ -1816,9 +1813,6 @@ declare module Immutable {
18161813
*
18171814
* OrderedSet([ 1, 2 ]).map(x => 10 * x)
18181815
* // OrderedSet [10, 20]
1819-
*
1820-
* Note: `map()` always returns a new instance, even if it produced the same
1821-
* value at every step.
18221816
*/
18231817
map<M>(
18241818
mapper: (value: T, key: T, iter: this) => M,

0 commit comments

Comments
 (0)