• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# pseudomap
2
3A thing that is a lot like ES6 `Map`, but without iterators, for use
4in environments where `for..of` syntax and `Map` are not available.
5
6If you need iterators, or just in general a more faithful polyfill to
7ES6 Maps, check out [es6-map](http://npm.im/es6-map).
8
9If you are in an environment where `Map` is supported, then that will
10be returned instead, unless `process.env.TEST_PSEUDOMAP` is set.
11
12You can use any value as keys, and any value as data.  Setting again
13with the identical key will overwrite the previous value.
14
15Internally, data is stored on an `Object.create(null)` style object.
16The key is coerced to a string to generate the key on the internal
17data-bag object.  The original key used is stored along with the data.
18
19In the event of a stringified-key collision, a new key is generated by
20appending an increasing number to the stringified-key until finding
21either the intended key or an empty spot.
22
23Note that because object traversal order of plain objects is not
24guaranteed to be identical to insertion order, the insertion order
25guarantee of `Map.prototype.forEach` is not guaranteed in this
26implementation.  However, in all versions of Node.js and V8 where this
27module works, `forEach` does traverse data in insertion order.
28
29## API
30
31Most of the [Map
32API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map),
33with the following exceptions:
34
351. A `Map` object is not an iterator.
362. `values`, `keys`, and `entries` methods are not implemented,
37   because they return iterators.
383. The argument to the constructor can be an Array of `[key, value]`
39   pairs, or a `Map` or `PseudoMap` object.  But, since iterators
40   aren't used, passing any plain-old iterator won't initialize the
41   map properly.
42
43## USAGE
44
45Use just like a regular ES6 Map.
46
47```javascript
48var PseudoMap = require('pseudomap')
49
50// optionally provide a pseudomap, or an array of [key,value] pairs
51// as the argument to initialize the map with
52var myMap = new PseudoMap()
53
54myMap.set(1, 'number 1')
55myMap.set('1', 'string 1')
56var akey = {}
57var bkey = {}
58myMap.set(akey, { some: 'data' })
59myMap.set(bkey, { some: 'other data' })
60```
61