You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.1 KiB

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