91 lines
2.5 KiB
JavaScript
91 lines
2.5 KiB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
var SPLIT = '__@field_split__';
|
|
|
|
/**
|
|
* Convert name path into string to fast the fetch speed of Map.
|
|
*/
|
|
function normalize(namePath) {
|
|
return namePath.map(function (cell) {
|
|
return "".concat(_typeof(cell), ":").concat(cell);
|
|
})
|
|
// Magic split
|
|
.join(SPLIT);
|
|
}
|
|
|
|
/**
|
|
* NameMap like a `Map` but accepts `string[]` as key.
|
|
*/
|
|
var NameMap = /*#__PURE__*/function () {
|
|
function NameMap() {
|
|
_classCallCheck(this, NameMap);
|
|
_defineProperty(this, "kvs", new Map());
|
|
}
|
|
_createClass(NameMap, [{
|
|
key: "set",
|
|
value: function set(key, value) {
|
|
this.kvs.set(normalize(key), value);
|
|
}
|
|
}, {
|
|
key: "get",
|
|
value: function get(key) {
|
|
return this.kvs.get(normalize(key));
|
|
}
|
|
}, {
|
|
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.kvs.delete(normalize(key));
|
|
}
|
|
|
|
// Since we only use this in test, let simply realize this
|
|
}, {
|
|
key: "map",
|
|
value: function map(callback) {
|
|
return _toConsumableArray(this.kvs.entries()).map(function (_ref) {
|
|
var _ref2 = _slicedToArray(_ref, 2),
|
|
key = _ref2[0],
|
|
value = _ref2[1];
|
|
var cells = key.split(SPLIT);
|
|
return callback({
|
|
key: cells.map(function (cell) {
|
|
var _cell$match = cell.match(/^([^:]*):(.*)$/),
|
|
_cell$match2 = _slicedToArray(_cell$match, 3),
|
|
type = _cell$match2[1],
|
|
unit = _cell$match2[2];
|
|
return type === 'number' ? Number(unit) : unit;
|
|
}),
|
|
value: value
|
|
});
|
|
});
|
|
}
|
|
}, {
|
|
key: "toJSON",
|
|
value: function toJSON() {
|
|
var json = {};
|
|
this.map(function (_ref3) {
|
|
var key = _ref3.key,
|
|
value = _ref3.value;
|
|
json[key.join('.')] = value;
|
|
return null;
|
|
});
|
|
return json;
|
|
}
|
|
}]);
|
|
return NameMap;
|
|
}();
|
|
export default NameMap; |