1 var hasOwnProperty = Object.prototype.hasOwnProperty
3 module.exports = PseudoMap
5 function PseudoMap (set) {
6 if (!(this instanceof PseudoMap)) // whyyyyyyy
7 throw new TypeError("Constructor PseudoMap requires 'new'")
12 if ((set instanceof PseudoMap) ||
13 (typeof Map === 'function' && set instanceof Map))
14 set.forEach(function (value, key) {
17 else if (Array.isArray(set))
18 set.forEach(function (kv) {
19 this.set(kv[0], kv[1])
22 throw new TypeError('invalid argument')
26 PseudoMap.prototype.forEach = function (fn, thisp) {
28 Object.keys(this._data).forEach(function (k) {
30 fn.call(thisp, this._data[k].value, this._data[k].key)
34 PseudoMap.prototype.has = function (k) {
35 return !!find(this._data, k)
38 PseudoMap.prototype.get = function (k) {
39 var res = find(this._data, k)
40 return res && res.value
43 PseudoMap.prototype.set = function (k, v) {
47 PseudoMap.prototype.delete = function (k) {
48 var res = find(this._data, k)
50 delete this._data[res._index]
55 PseudoMap.prototype.clear = function () {
56 var data = Object.create(null)
59 Object.defineProperty(this, '_data', {
67 Object.defineProperty(PseudoMap.prototype, 'size', {
69 return this._data.size
76 PseudoMap.prototype.values =
77 PseudoMap.prototype.keys =
78 PseudoMap.prototype.entries = function () {
79 throw new Error('iterators are not implemented in this version')
82 // Either identical, or both NaN
83 function same (a, b) {
84 return a === b || a !== a && b !== b
87 function Entry (k, v, i) {
93 function find (data, k) {
94 for (var i = 0, s = '_' + k, key = s;
95 hasOwnProperty.call(data, key);
97 if (same(data[key].key, k))
102 function set (data, k, v) {
103 for (var i = 0, s = '_' + k, key = s;
104 hasOwnProperty.call(data, key);
106 if (same(data[key].key, k)) {
112 data[key] = new Entry(k, v, key)