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
import Hash from './Hash.js';
/**
* Gets the data for `map`.
* 获取“map”的数据。
* @private
* @param {Object} map 要查询的映射。
* @param {string} key 参考关键。
* @returns {*} 返回地图数据。
*/
function getMapData({ __data__ }, key) {
const data = __data__;
return isKeyable(key) ? data[typeof key === 'string' ? 'string' : 'hash'] : data.map;
}
/**
* Checks if `value` is suitable for use as unique object key.
* 检查' value '是否适合用作唯一的对象键。
* @private
* @param {*} value 要检查的值。
* @returns {boolean} 如果' value '合适,则返回' true ',否则返回' false '。
*/
function isKeyable(value) {
const type = typeof value;
return type === 'string' || type === 'number' || type === 'symbol' || type === 'boolean'
? value !== '__proto__'
: value === null;
}
class MapCache {
/**
* Creates a map cache object to store key-value pairs.
* 创建一个映射缓存对象来存储键值对
* @private
* @constructor
* @param {Array} [entries] 要缓存的键值对。
*/
constructor(entries) {
let index = -1;
const length = entries == null ? 0 : entries.length;
this.clear();
while (++index < length) {
const entry = entries[index];
this.set(entry[0], entry[1]);
}
}
/**
* Removes all key-value entries from the map.
* 从映射中删除所有键值项。
* @memberOf MapCache
*/
clear() {
this.size = 0;
this.__data__ = {
hash: new Hash(),
map: new Map(),
string: new Hash(),
};
}
/**
* Removes `key` and its value from the map.
* 从映射中删除' key '及其值。
* @memberOf MapCache
* @param {string} key 要删除的值的键。
* @returns {boolean} 如果条目被删除,则返回“true”,否则返回“false”。
*/
delete(key) {
const result = getMapData(this, key)['delete'](key);
this.size -= result ? 1 : 0;
return result;
}
/**
* Gets the map value for `key`.
* 获取' key '的映射值。
* @memberOf MapCache
* @param {string} key 要获取的值的键。
* @returns {*} 返回条目值。
*/
get(key) {
return getMapData(this, key).get(key);
}
/**
* Checks if a map value for `key` exists.
* 检查' key '的map值是否存在。
* @memberOf MapCache
* @param {string} key 要检查的key。
* @returns {boolean} 如果' key '的条目存在,则返回' true ',否则返回' false '。
*/
has(key) {
return getMapData(this, key).has(key);
}
/**
* Sets the map `key` to `value`.
* 将映射“键”设置为“值”。
* @memberOf MapCache
* @param {string} key 要设置的值的键。
* @param {*} value 要设置的值。
* @returns {Object} 返回映射缓存实例。
*/
set(key, value) {
const data = getMapData(this, key);
const size = data.size;
data.set(key, value);
this.size += data.size == size ? 0 : 1;
return this;
}
}
export default MapCache;