Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 265b3dc

Browse files
committed
Merge branch 'map-delete-all' of https://github.com/vinnymac/immutable-js into vinnymac-map-delete-all
2 parents dc21c59 + bbfedb6 commit 265b3dc

File tree

10 files changed

+117
-23
lines changed

10 files changed

+117
-23
lines changed

__tests__/Map.ts

+16
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,20 @@ describe('Map', () => {
343343
expect(is(m1, m2)).toBe(true);
344344
});
345345

346+
it('deletes all the provided keys', () => {
347+
var NOT_SET = undefined;
348+
var m1 = Map({ A: 1, B: 2, C: 3 });
349+
var m2 = m1.deleteAll(["A", "B"]);
350+
expect(m2.get("A")).toBe(NOT_SET);
351+
expect(m2.get("B")).toBe(NOT_SET);
352+
expect(m2.get("C")).toBe(3);
353+
expect(m2.size).toBe(1);
354+
});
355+
356+
it('remains unchanged when no keys are provided', () => {
357+
var m1 = Map({ A: 1, B: 2, C: 3 });
358+
var m2 = m1.deleteAll([]);
359+
expect(m1).toBe(m2);
360+
});
361+
346362
});

dist/immutable-nonambient.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,17 @@
623623
delete(key: K): Map<K, V>;
624624
remove(key: K): Map<K, V>;
625625

626+
/**
627+
* Returns a new Map which excludes the provided `keys`.
628+
*
629+
* var names = Immutable.Map({ a: "Aaron", b: "Barry", c: "Connor" });
630+
* names.deleteAll(['a', 'c']); // { b: "Barry" }
631+
*
632+
* @alias removeAll
633+
*/
634+
deleteAll(keys: Array<K> | ESIterable<K>): Map<K, V>;
635+
removeAll(keys: Array<K> | ESIterable<K>): Map<K, V>;
636+
626637
/**
627638
* Returns a new Map containing no keys or values.
628639
*

dist/immutable.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,17 @@ declare module Immutable {
623623
delete(key: K): Map<K, V>;
624624
remove(key: K): Map<K, V>;
625625

626+
/**
627+
* Returns a new Map which excludes the provided `keys`.
628+
*
629+
* var names = Immutable.Map({ a: "Aaron", b: "Barry", c: "Connor" });
630+
* names.deleteAll(['a', 'c']); // { b: "Barry" }
631+
*
632+
* @alias removeAll
633+
*/
634+
deleteAll(keys: Array<K> | ESIterable<K>): Map<K, V>;
635+
removeAll(keys: Array<K> | ESIterable<K>): Map<K, V>;
636+
626637
/**
627638
* Returns a new Map containing no keys or values.
628639
*

dist/immutable.js

+13
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,18 @@
12671267
return this.updateIn(keyPath, function() {return NOT_SET});
12681268
};
12691269

1270+
Map.prototype.deleteAll = function(keys) {
1271+
var iterable = Iterable(keys);
1272+
1273+
if (iterable.size === 0) {
1274+
return this;
1275+
}
1276+
1277+
return this.withMutations(function(map ) {
1278+
iterable.forEach(function(key ) {return map.remove(key)});
1279+
});
1280+
};
1281+
12701282
Map.prototype.update = function(k, notSetValue, updater) {
12711283
return arguments.length === 1 ?
12721284
k(this) :
@@ -1407,6 +1419,7 @@
14071419
MapPrototype[IS_MAP_SENTINEL] = true;
14081420
MapPrototype[DELETE] = MapPrototype.remove;
14091421
MapPrototype.removeIn = MapPrototype.deleteIn;
1422+
MapPrototype.removeAll = MapPrototype.deleteAll;
14101423

14111424

14121425
// #pragma Trie Nodes

dist/immutable.js.flow

+3
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ declare class Map<K,V> extends KeyedCollection<K,V> {
476476
remove(key: K): this;
477477
clear(): this;
478478

479+
deleteAll(keys: ESIterable<K>): Map<K, V>;
480+
removeAll(keys: ESIterable<K>): Map<K, V>;
481+
479482
update<K_,V_>(updater: (value: this) => Map<K_,V_>): Map<K_,V_>;
480483
update<V_>(key: K, updater: (value: V) => V_): Map<K,V|V_>;
481484
update<V_>(key: K, notSetValue: V_, updater: (value: V) => V_): Map<K,V|V_>;

dist/immutable.min.js

+22-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Map.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import { is } from './is'
1111
import { fromJS } from './fromJS'
12-
import { isIterable, KeyedIterable, isOrdered } from './Iterable'
12+
import { isIterable, KeyedIterable, isOrdered, Iterable } from './Iterable'
1313
import { KeyedCollection } from './Collection'
1414
import { DELETE, SHIFT, SIZE, MASK, NOT_SET, CHANGE_LENGTH, DID_ALTER, OwnerID,
1515
MakeRef, SetRef, arrCopy } from './TrieUtils'
@@ -78,6 +78,18 @@ export class Map extends KeyedCollection {
7878
return this.updateIn(keyPath, () => NOT_SET);
7979
}
8080

81+
deleteAll(keys) {
82+
var iterable = Iterable(keys);
83+
84+
if (iterable.size === 0) {
85+
return this;
86+
}
87+
88+
return this.withMutations(map => {
89+
iterable.forEach(key => map.remove(key));
90+
});
91+
}
92+
8193
update(k, notSetValue, updater) {
8294
return arguments.length === 1 ?
8395
k(this) :
@@ -218,6 +230,7 @@ export var MapPrototype = Map.prototype;
218230
MapPrototype[IS_MAP_SENTINEL] = true;
219231
MapPrototype[DELETE] = MapPrototype.remove;
220232
MapPrototype.removeIn = MapPrototype.deleteIn;
233+
MapPrototype.removeAll = MapPrototype.deleteAll;
221234

222235

223236
// #pragma Trie Nodes

type-definitions/Immutable.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,17 @@ declare module Immutable {
623623
delete(key: K): Map<K, V>;
624624
remove(key: K): Map<K, V>;
625625

626+
/**
627+
* Returns a new Map which excludes the provided `keys`.
628+
*
629+
* var names = Immutable.Map({ a: "Aaron", b: "Barry", c: "Connor" });
630+
* names.deleteAll(['a', 'c']); // { b: "Barry" }
631+
*
632+
* @alias removeAll
633+
*/
634+
deleteAll(keys: Array<K> | ESIterable<K>): Map<K, V>;
635+
removeAll(keys: Array<K> | ESIterable<K>): Map<K, V>;
636+
626637
/**
627638
* Returns a new Map containing no keys or values.
628639
*

type-definitions/immutable.js.flow

+3
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ declare class Map<K,V> extends KeyedCollection<K,V> {
476476
remove(key: K): this;
477477
clear(): this;
478478

479+
deleteAll(keys: ESIterable<K>): Map<K, V>;
480+
removeAll(keys: ESIterable<K>): Map<K, V>;
481+
479482
update<K_,V_>(updater: (value: this) => Map<K_,V_>): Map<K_,V_>;
480483
update<V_>(key: K, updater: (value: V) => V_): Map<K,V|V_>;
481484
update<V_>(key: K, notSetValue: V_, updater: (value: V) => V_): Map<K,V|V_>;

type-definitions/tests/immutable-flow.js

+13
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ stringToNumber = Map().set(1, '')
211211

212212
stringToNumber = Map({'a': 0}).delete('a')
213213
stringToNumber = Map({'a': 0}).remove('a')
214+
// $ExpectError
215+
stringToNumber = Map({'a': 0}).delete(1)
216+
// $ExpectError
217+
stringToNumber = Map({'a': 0}).remove(1)
218+
219+
stringToNumber = Map({'a': 0}).deleteAll(['a'])
220+
stringToNumber = Map({'a': 0}).removeAll(['a'])
221+
// $ExpectError
222+
stringToNumber = Map({'a': 0}).deleteAll(1)
223+
// $ExpectError
224+
stringToNumber = Map({'a': 0}).deleteAll([1])
225+
// $ExpectError
226+
stringToNumber = Map({'a': 0}).removeAll([1])
214227

215228
stringToNumber = Map({'a': 0}).clear()
216229

0 commit comments

Comments
 (0)