forked from alastairpatrick/valuecollection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValueMap.esm.js
178 lines (159 loc) · 5.08 KB
/
ValueMap.esm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"use strict";
import isEqual from "lodash.isEqual";
import { hash } from "./hash.esm.js";
const has = Object.prototype.hasOwnProperty;
/**
* The ValueMap object holds key-value pairs. Any value (both objects and primitive values) may be used as either a key or a value.
* By default, keys are considered equal using lodash's isEqual, which is deep equality.
*/
class ValueMap extends Map {
static name = 'ValueMap';
/**
* Create a new ValueMap.
* @param {Iterable} [iterable] An iterable object whose elements are [key, value] pairs to add to the new ValueMap. If you don't specify this parameter, or its value is null, the new ValueMap is empty.
*/
constructor(iterable) {
super();
this.clear();
if (iterable) {
for (let [k, v] of iterable)
this.set(k, v);
}
}
/**
* Returns the number of elements in a ValueMap object.
* @returns {number} The number of elements.
*/
get size() {
return this.size_;
}
/**
* Adds or updates an element with a specified key and value to a ValueMap object.
* @param {*} k The key of the element to add to the ValueMap object.
* @param {*} v The value of the element to add to the ValueMap object.
* @returns {ValueMap} The ValueMap object.
*/
set(k, v) {
let h = this.getHash(k);
let b = this.hash[h];
if (b === undefined)
b = this.hash[h] = [];
let n = b.find(n => this.isEqual(n.key, k))
if (n === undefined) {
b.push({
key: k,
value: v,
});
++this.size_;
} else {
n.value = v;
}
return this;
}
/**
* The has() method returns a boolean indicating whether an element with the specified key exists or not.
* @param {*} k The key of the element to test for presence in the ValueMap object.
* @returns {boolean} Returns true if an element with the specified key exists in the ValueMap object; otherwise false.
*/
has(k) {
let h = this.getHash(k);
let b = this.hash[h];
if (b === undefined)
return false;
let ni = b.findIndex(n => this.isEqual(n.key, k))
return ni >= 0;
}
/**
* The get() method returns a specified element from a ValueMap object.
* @param {*} k The key of the element to return from the ValueMap object.
* @returns {*} Returns the element associated with the specified key or undefined if the key can't be found in the ValueMap object.
*/
get(k) {
let h = this.getHash(k);
let b = this.hash[h];
if (b === undefined)
return undefined;
let n = b.find(n => this.isEqual(n.key, k))
if (n === undefined)
return undefined;
return n.value;
}
/**
* The delete() method removes the specified element from a ValueMap object if a corresponding key exists.
* @param {*} k The key of the element to remove from the ValueMap object.
* @returns {boolean} Returns true if an element with the specified key existed in the ValueMap object and was removed; otherwise false.
*/
delete(k) {
let h = this.getHash(k);
let b = this.hash[h];
if (b === undefined)
return false;
let ni = b.findIndex(n => this.isEqual(n.key, k))
if (ni < 0)
return false;
b.splice(ni, 1);
--this.size_;
if (b.length === 0)
delete this.hash[h];
return true;
}
/**
* The values() method returns a new Iterator object that contains the values for each element in the ValueMap object.
* Note that the values are not returned in insertion order.
* @returns {Iterator} A new ValueMap iterator object.
*/
*values() {
for (let h in this.hash) {
for (let n of this.hash[h])
yield n.value;
}
}
/**
* The keys() method returns a new Iterator object that contains the keys for each element in the ValueMap object.
* Note that the keys are not returned in insertion order.
* @returns {Iterator} A new ValueMap iterator object.
*/
*keys() {
for (let h in this.hash) {
for (let n of this.hash[h])
yield n.key;
}
}
/**
* The entries() method returns a new Iterator object that contains the [key, value] pairs for each element in the ValueMap object.
* Note that the elements are not returned in insertion order.
* @returns {Iterator} A new ValueMap iterator object.
*/
*entries() {
for (let h in this.hash) {
for (let n of this.hash[h])
yield [n.key, n.value];
}
}
/**
* The forEach() method invokes a function with three parameters: value, key, and the valueMap itself.
* Note that the elements are not itterated in insertion order.
* @returns {Iterator} A new ValueMap iterator object.
*/
forEach(func) {
for (let h in this.hash) {
for (let n of this.hash[h])
func(n.value, n.key, this);
}
}
/**
* The clear() method removes all elements from a ValueMap object.
*/
clear() {
this.hash = Object.create(null);
this.size_ = 0;
}
toJSON() {
return JSON.stringify([...this]);
}
}
ValueMap.prototype.getHash = hash;
ValueMap.prototype.isEqual = isEqual;
if (Symbol && Symbol.iterator)
ValueMap.prototype[Symbol.iterator] = ValueMap.prototype.entries;
export { ValueMap };