NameMap.js
1.74 KB
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
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import { matchNamePath } from './valueUtil';
/**
* NameMap like a `Map` but accepts `string[]` as key.
*/
var NameMap = /*#__PURE__*/function () {
function NameMap() {
_classCallCheck(this, NameMap);
this.list = [];
}
_createClass(NameMap, [{
key: "set",
value: function set(key, value) {
var index = this.list.findIndex(function (item) {
return matchNamePath(item.key, key);
});
if (index !== -1) {
this.list[index].value = value;
} else {
this.list.push({
key: key,
value: value
});
}
}
}, {
key: "get",
value: function get(key) {
var result = this.list.find(function (item) {
return matchNamePath(item.key, key);
});
return result && result.value;
}
}, {
key: "update",
value: function update(key, updater) {
var origin = this.get(key);
var next = updater(origin);
if (!next) {
this.delete(key);
} else {
this.set(key, next);
}
}
}, {
key: "delete",
value: function _delete(key) {
this.list = this.list.filter(function (item) {
return !matchNamePath(item.key, key);
});
}
}, {
key: "map",
value: function map(callback) {
return this.list.map(callback);
}
}, {
key: "toJSON",
value: function toJSON() {
var json = {};
this.map(function (_ref) {
var key = _ref.key,
value = _ref.value;
json[key.join('.')] = value;
return null;
});
return json;
}
}]);
return NameMap;
}();
export default NameMap;