Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/collections/maps.js
4062 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Helper methods that operate on Map-like objects (e.g. ES6
9
* Maps).
10
*/
11
12
goog.module('goog.collections.maps');
13
goog.module.declareLegacyNamespace();
14
15
/**
16
* A MapLike implements the same public interface as an ES6 Map, without tying
17
* the underlying code directly to the implementation. Any additions to this
18
* type should also be present on ES6 Maps.
19
* @template K,V
20
* @record
21
*/
22
class MapLike {
23
constructor() {
24
/** @const {number} The number of items in this map. */
25
this.size;
26
}
27
28
/**
29
* @param {K} key The key to set in the map.
30
* @param {V} val The value to set for the given key in the map.
31
*/
32
set(key, val) {};
33
34
/**
35
* @param {K} key The key to retrieve from the map.
36
* @return {V|undefined} The value for this key, or undefined if the key is
37
* not present in the map.
38
*/
39
get(key) {};
40
41
/**
42
* @return {!IteratorIterable<K>} An ES6 Iterator that iterates over the keys
43
* in the map.
44
*/
45
keys() {};
46
47
/**
48
* @return {!IteratorIterable<V>} An ES6 Iterator that iterates over the
49
* values in the map.
50
*/
51
values() {};
52
53
/**
54
* @param {K} key The key to check.
55
* @return {boolean} True iff this key is present in the map.
56
*/
57
has(key) {};
58
}
59
exports.MapLike = MapLike;
60
61
/**
62
* Iterates over each entry in the given entries and sets the entry in
63
* the map, overwriting any existing entries for the key.
64
* @param {!MapLike<K,V>} map The map to set entries on.
65
* @param {?Iterable<!Array<K|V>>} entries The iterable of entries. This
66
* iterable should really be of type Iterable<Array<[K,V]>>, but the tuple
67
* type is not representable in the Closure Type System.
68
* @template K,V
69
*/
70
function setAll(map, entries) {
71
if (!entries) return;
72
for (const [k, v] of entries) {
73
map.set(k, v);
74
}
75
}
76
exports.setAll = setAll;
77
78
/**
79
* Determines if a given map contains the given value, optionally using
80
* a custom comparison function.
81
* @param {!MapLike<?,V1>} map The map whose values to check.
82
* @param {V2} val The value to check for.
83
* @param {(function(V1,V2): boolean)=} valueEqualityFn The comparison function
84
* used to determine if the given value is equivalent to any of the values
85
* in the map. If no function is provided, defaults to strict equality
86
* (===).
87
* @return {boolean} True iff the given map contains the given value according
88
* to the comparison function.
89
* @template V1,V2
90
*/
91
function hasValue(map, val, valueEqualityFn = defaultEqualityFn) {
92
for (const v of map.values()) {
93
if (valueEqualityFn(v, val)) return true;
94
}
95
return false;
96
}
97
exports.hasValue = hasValue;
98
99
/** @const {function(?,?): boolean} */
100
const defaultEqualityFn = (a, b) => a === b;
101
102
/**
103
* Compares two maps using their public APIs to determine if they have
104
* equal contents, optionally using a custom comparison function when comaring
105
* values.
106
* @param {!MapLike<K,V1>} map The first map
107
* @param {!MapLike<K,V2>} otherMap The other map
108
* @param {(function(V1,V2): boolean)=} valueEqualityFn The comparison function
109
* used to determine if the values obtained from each map are equivalent. If
110
* no function is provided, defaults to strict equality (===).
111
* @return {boolean}
112
* @template K,V1,V2
113
*/
114
function equals(map, otherMap, valueEqualityFn = defaultEqualityFn) {
115
if (map === otherMap) return true;
116
if (map.size !== otherMap.size) return false;
117
for (const key of map.keys()) {
118
if (!otherMap.has(key)) return false;
119
if (!valueEqualityFn(map.get(key), otherMap.get(key))) return false;
120
}
121
return true;
122
}
123
exports.equals = equals;
124
125
/**
126
* Returns a new ES6 Map in which all the keys and values from the
127
* given map are interchanged (keys become values and values become keys). If
128
* multiple keys in the given map to the same value, the resulting value in the
129
* transposed map is implementation-dependent.
130
*
131
* It acts very similarly to {goog.object.transpose(Object)}.
132
* @param {!MapLike<K,V>} map The map to transpose.
133
* @return {!Map<V,K>} A transposed version of the given map.
134
* @template K,V
135
*/
136
function transpose(map) {
137
const /** !Map<V,K> */ transposed = new Map();
138
for (const key of map.keys()) {
139
const val = map.get(key);
140
transposed.set(val, key);
141
}
142
return transposed;
143
}
144
exports.transpose = transpose;
145
146
/**
147
* ToObject returns a new object whose properties are the keys from the Map.
148
* @param {!MapLike<K,V>} map The map to convert into an object.
149
* @return {!Object<K,V>} An object representation of the Map.
150
* @template K,V
151
*/
152
function toObject(map) {
153
const /** !Object<K,V> */ obj = {};
154
for (const key of map.keys()) {
155
obj[key] = map.get(key);
156
}
157
return obj;
158
}
159
exports.toObject = toObject;
160
161