Skip to content

Commit c083d6b

Browse files
committed
Add {map,set}.clear.
1 parent 50617b7 commit c083d6b

6 files changed

Lines changed: 37 additions & 3 deletions

File tree

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ Like [ES6 Maps](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc
247247
* Keys are coerced to strings.
248248
* [map.set](#map_set) returns the new value, not the map itself.
249249
* [map.each](#map_each), not [map.forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach). (Also, no *thisArg*.)
250-
* [map.remove](#map_remove), not [map.delete](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete); also, no [map.clear](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear).
250+
* [map.remove](#map_remove), not [map.delete](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete).
251251
* [map.entries](#map_entries) returns an array of {key, value} objects, not an iterator of [key, value].
252252
* [map.size](#map_size) is a method, not a [property](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size); also, there’s [map.empty](#map_empty).
253253

@@ -280,6 +280,10 @@ Sets the *value* for the specified *key* string; returns the new *value*. If the
280280

281281
If the map has an entry for the specified *key* string, removes the entry and returns true. Otherwise, this method does nothing and returns false.
282282

283+
<a name="map_clear" href="#map_clear">#</a> <i>map</i>.<b>clear</b>()
284+
285+
Removes all entries from this map.
286+
283287
<a name="map_keys" href="#map_keys">#</a> <i>map</i>.<b>keys</b>()
284288

285289
Returns an array of string keys for every entry in this map. The order of the returned keys is arbitrary.
@@ -311,7 +315,7 @@ Like [ES6 Sets](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc
311315
* Values are coerced to strings.
312316
* [set.add](#set_add) returns the new value, not the set itself.
313317
* [set.each](#set_each), not [set.forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/forEach). (Also, no *thisArg*.)
314-
* [set.remove](#set_remove), not [set.delete](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/delete); also, no [set.clear](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/clear).
318+
* [set.remove](#set_remove), not [set.delete](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/delete).
315319
* [set.size](#set_size) is a method, not a [property](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/size); also, there’s [set.empty](#set_empty).
316320

317321
<a name="set" href="#set">#</a> <b>set</b>([<i>array</i>])
@@ -330,6 +334,10 @@ Adds the specified *value* string to this set. Returns *value*.
330334

331335
If the set contains the specified *value* string, removes it and returns true. Otherwise, this method does nothing and returns false.
332336

337+
<a name="set_clear" href="#set_clear">#</a> <i>set</i>.<b>clear</b>()
338+
339+
Removes all values from this set.
340+
333341
<a name="set_values" href="#set_values">#</a> <i>set</i>.<b>values</b>()
334342

335343
Returns an array of the string values in this set. The order of the returned values is arbitrary. Can be used as a convenient way of computing the unique values for a set of strings. For example:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "d3-arrays",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Array manipulation, ordering, searching, summarizing, etc.",
55
"keywords": [
66
"d3",

src/map.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Map.prototype = map.prototype = {
1616
var property = prefix + key;
1717
return property in this && delete this[property];
1818
},
19+
clear: function() {
20+
for (var property in this) if (property[0] === prefix) delete this[property];
21+
},
1922
keys: function() {
2023
var keys = [];
2124
for (var property in this) if (property[0] === prefix) keys.push(property.slice(1));

src/set.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Set.prototype = set.prototype = {
1212
return value;
1313
},
1414
remove: proto.remove,
15+
clear: proto.clear,
1516
values: proto.keys,
1617
size: proto.size,
1718
empty: proto.empty,

test/map-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ tape("map.size() returns the number of distinct keys", function(test) {
9191
test.end();
9292
});
9393

94+
tape("map.clear() removes all entries", function(test) {
95+
var m = arrays.map();
96+
m.set("foo", 1);
97+
m.set("bar", 2);
98+
m.set("foo", 3);
99+
m.clear();
100+
test.equal(m.size(), 0);
101+
test.deepEqual(m.entries(), []);
102+
test.end();
103+
});
104+
94105
tape("map.empty() returns true only if the map is empty", function(test) {
95106
var m = arrays.map();
96107
test.equal(m.empty(), true);

test/set-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ tape("set.size() returns the number of distinct values", function(test) {
5151
test.end();
5252
});
5353

54+
tape("set.clear() removes all values", function(test) {
55+
var s = arrays.set();
56+
s.add("foo");
57+
s.add("bar");
58+
s.add("foo");
59+
s.clear();
60+
test.equal(s.size(), 0);
61+
test.deepEqual(s.values(), []);
62+
test.end();
63+
});
64+
5465
tape("set.empty() returns true only if the set is empty", function(test) {
5566
var s = arrays.set();
5667
test.equal(s.empty(), true);

0 commit comments

Comments
 (0)