Comparing version 1.2.0 to 1.3.0
31
dict.js
"use strict"; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
var MANGLE_STRING = "~"; | ||
function mangle(key) { | ||
return "~" + key; | ||
return MANGLE_STRING + key; | ||
} | ||
function unmangle(key) { | ||
return key.substring(MANGLE_STRING.length); | ||
} | ||
function methods(obj, methodHash) { | ||
@@ -54,5 +61,25 @@ for (var methodName in methodHash) { | ||
--size; | ||
delete store[mangled]; | ||
return true; | ||
} | ||
delete store[mangle(key)]; | ||
return false; | ||
}, | ||
clear: function () { | ||
store = Object.create(null); | ||
size = 0; | ||
}, | ||
forEach: function (callback, thisArg) { | ||
if (typeof callback !== "function") { | ||
throw new TypeError("`callback` must be a function"); | ||
} | ||
for (var mangledKey in store) { | ||
if (hasOwnProperty.call(store, mangledKey)) { | ||
var key = unmangle(mangledKey); | ||
var value = store[mangledKey]; | ||
callback.call(thisArg, value, key, dict); | ||
} | ||
} | ||
} | ||
@@ -59,0 +86,0 @@ }); |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"author": "Domenic Denicola <domenic@domenicdenicola.com> (http://domenicdenicola.com)", | ||
@@ -28,7 +28,9 @@ "license": "WTFPL", | ||
"devDependencies": { | ||
"chai": ">= 1.3.0", | ||
"coffee-script": ">= 1.4.0", | ||
"jshint": ">= 0.9.1", | ||
"mocha": ">= 1.6.0" | ||
"chai": "~1.6", | ||
"coffee-script": "~1.6.2", | ||
"jshint": "~1.1", | ||
"mocha": "~1.9.0", | ||
"sinon": "~1.6", | ||
"sinon-chai": "~2.4" | ||
} | ||
} |
# An Easy But Safe String-Keyed Store | ||
Don't stuff things into objects. Use a `dict` instead. | ||
Don't stuff things into objects. Use a Dict instead. | ||
@@ -9,3 +9,3 @@ ## The Problem | ||
```javascript | ||
```js | ||
var hash = {}; | ||
@@ -21,3 +21,3 @@ | ||
```javascript | ||
```js | ||
var hash = {}; | ||
@@ -30,3 +30,3 @@ | ||
```javascript | ||
```js | ||
var hash = {}; | ||
@@ -44,9 +44,37 @@ var anotherObject = { foo: "bar" }; | ||
## `dict` Is the Solution | ||
## Dict Is the Solution | ||
Just do an `npm install dict` and you're good to go: | ||
Just do an `npm install dict --save` and you're ready to use this nice-looking API: | ||
```javascript | ||
```js | ||
var dict = require("dict"); | ||
var d = dict({ | ||
IV: "A New Hope", | ||
V: "The Empire Strikes Back", | ||
VI: "Return of the Jedi" | ||
}); | ||
d.has("IV"); // true | ||
d.get("V"); // "The Empire Strikes Back" | ||
d.size; // 3 | ||
d.has("I"); // false | ||
d.set("I", "The Phantom Menace"); | ||
d.get("I"); // "The Phantom Menace" | ||
d.delete("I"); // true | ||
d.get("I"); // undefined | ||
d.get("I", "Jar-Jar's Fun Time"); // "Jar-Jar's Fun Time" | ||
d.forEach(function (value, key) { | ||
console.log("Star Wars Episode " + key + ": " + value); | ||
}); | ||
d.clear(); | ||
d.size; // 0 | ||
``` | ||
And of course, Dict prides itself in being bulletproof against all that nastiness we talked about earlier: | ||
```javascript | ||
var d = dict(); | ||
@@ -57,8 +85,8 @@ | ||
console.log(d.has("hasOwnProperty")); // false :) | ||
console.log(d.has("hasOwnProperty")); // false | ||
var anotherObject = { baz: "qux" }; | ||
d.set("__proto__", anotherObject); | ||
console.log(d.has("baz")); // false :) | ||
console.log(d.has("__proto__")); // true :) | ||
console.log(d.has("baz")); // false | ||
console.log(d.has("__proto__")); // true | ||
``` | ||
@@ -68,6 +96,8 @@ | ||
* A lightweight [ES6-inspired][3] API: `get`, `set`, `has`, `delete`, and a `size` property. | ||
* A lightweight [ES6-inspired][3] API: | ||
- `get`, `set`, `has` and `delete` basic operations. | ||
- A `size` property and `forEach` method for introspection. | ||
- A `clear` method for clearing out all keys and values. | ||
* `get` accepts a second argument as a fallback for if the key isn't present (like [Mozilla's `WeakMap`][4]). | ||
* Doesn't let you get away with being dumb: if you pass a non-string as a key, you're going to get a `TypeError`. | ||
* A full suite of unit tests using [mocha][5] and [chai][6]: `npm test` awaits you. | ||
@@ -81,2 +111,4 @@ ## See Also | ||
* [es6-shim][10]'s `Map` if you want more than just strings for your keys. | ||
* `Object.create(null)` if you don't have to deal with V8 or JavaScriptCore, for which | ||
`"__proto__" in Object.create(null)` is still true. | ||
@@ -86,3 +118,3 @@ | ||
[2]: http://www.google.com/support/forum/p/Google+Docs/thread?tid=0cd4a00bd4aef9e4 | ||
[3]: http://wiki.ecmascript.org/doku.php?id=harmony:simple_maps_and_sets | ||
[3]: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-15.14.4 | ||
[4]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/WeakMap | ||
@@ -89,0 +121,0 @@ [5]: http://visionmedia.github.com/mocha/ |
7483
85
119
6