Skip to content

Commit 78b1616

Browse files
committed
remove is default, delete is aliased (with caveats). Thanks @mathiasbynens. Closes #1
1 parent 7a53ec9 commit 78b1616

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ A fast and robust stringmap implementation that can hold any string keys,
33
including `__proto__`, with minimal overhead compared to a plain object.
44
Works in node and browsers.
55

6+
The API is created to be as close to the ES6 Map API as possible. Prefer
7+
`sm.remove("key")` for deleting a key. ES6 Map uses `map.delete("key")`
8+
instead and for that reason `sm['delete']("key")` is available as a
9+
stringmap alias as well. Never do `sm.delete("key")` unless you're
10+
certain to be in the land of ES5 or later.
11+
612

713

814
## Examples
@@ -17,7 +23,7 @@ sm1.set("check", true);
1723
sm1.set("__proto__", -1);
1824
console.log(sm1.has("greeting")); // true
1925
console.log(sm1.get("__proto__")); // -1
20-
sm1.delete("greeting");
26+
sm1.remove("greeting");
2127
console.log(sm1.keys()); // [ 'check', '__proto__' ]
2228
console.log(sm1.values()); // [ true, -1 ]
2329
console.log(sm1.items()); // [ [ 'check', true ], [ '__proto__', -1 ] ]

examples.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sm1.set("check", true);
66
sm1.set("__proto__", -1);
77
console.log(sm1.has("greeting")); // true
88
console.log(sm1.get("__proto__")); // -1
9-
sm1.delete("greeting");
9+
sm1.remove("greeting");
1010
console.log(sm1.keys()); // [ 'check', '__proto__' ]
1111
console.log(sm1.values()); // [ true, -1 ]
1212
console.log(sm1.items()); // [ [ 'check', true ], [ '__proto__', -1 ] ]

stringmap.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ var StringMap = (function() {
106106
}
107107
};
108108

109-
stringmap.prototype['delete'] = function(key) {
109+
stringmap.prototype.remove = function(key) {
110110
if (typeof key !== "string") {
111111
throw new Error("StringMap expected string key");
112112
}
@@ -120,6 +120,12 @@ var StringMap = (function() {
120120
return didExist;
121121
};
122122

123+
// alias remove to delete but beware:
124+
// sm.delete("key"); // OK in ES5 and later
125+
// sm['delete']("key"); // OK in all ES versions
126+
// sm.remove("key"); // OK in all ES versions
127+
stringmap.prototype['delete'] = stringmap.prototype.remove;
128+
123129
stringmap.prototype.isEmpty = function() {
124130
for (var key in this.obj) {
125131
if (hasOwnProperty.call(this.obj, key)) {

0 commit comments

Comments
 (0)