UNPKG

@graffy/common

Version:

Common libraries that used by various Graffy modules.

149 lines (110 loc) 4.63 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); exports.__esModule = true; exports.makePath = makePath; exports.wrapValue = wrapValue; exports.wrap = wrap; exports.unwrap = unwrap; exports.wrapObject = wrapObject; exports.unwrapObject = unwrapObject; exports.remove = remove; exports.PATH_SEPARATOR = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends")); var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array")); var _every = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/every")); var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice")); var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat")); var _node = require("../node"); var _coding = require("../coding"); var PATH_SEPARATOR = '.'; exports.PATH_SEPARATOR = PATH_SEPARATOR; function makePath(path) { if ((0, _isArray.default)(path) && (0, _every.default)(path).call(path, function (key) { return typeof key === 'string'; })) { return path; } if (typeof path !== 'string') throw Error('makePath.path_not_string'); if (!path.length || path === PATH_SEPARATOR) return []; return path.split(PATH_SEPARATOR); } function wrapValue(value, path, version) { if (version === void 0) { version = 0; } var node = (0, _extends2.default)({}, (0, _coding.encodeArgs)(path[path.length - 1]), { value: value, version: version }); return wrap([node], (0, _slice.default)(path).call(path, 0, -1), version); } function wrap(graph, path, version) { if (version === void 0) { version = 0; } if (!(0, _isArray.default)(graph) || !graph.length) return; if (!(0, _isArray.default)(path)) throw Error('wrap.path_not_array ' + path); var children = graph; for (var i = path.length - 1; i >= 0; i--) { children = [(0, _extends2.default)({}, (0, _coding.encodeArgs)(path[i]), { version: version, children: children })]; } return children; } function unwrap(graph, path) { var children = graph; if (!(0, _isArray.default)(path)) throw Error('unwrap.path_not_array ' + path); var node = { children: children }; for (var i = 0; i < path.length; i++) { var _encodeArgs = (0, _coding.encodeArgs)(path[i]), key = _encodeArgs.key; children = node.children; if (!children) return null; // This path does not exist. node = children[(0, _node.findFirst)(children, key)]; if (!node || node.key > key) return undefined; // We lack knowledge. if ((0, _node.isRange)(node)) return null; // This is known to be null. } return node.children || node.value; } function wrapObject(object, path) { if (!(0, _isArray.default)(path)) throw Error('wrapObject.path_not_array ' + path); for (var i = path.length - 1; i >= 0; i--) { var _object; object = (_object = {}, _object[path[i]] = object, _object); } return object; } function unwrapObject(object, path) { if (!(0, _isArray.default)(path)) throw Error('unwrapObject.path_not_array ' + path); for (var i = 0; i < path.length; i++) { if (!object || typeof object !== 'object') return; object = object[path[i]]; } return object; } function remove(children, path) { var _context2; if (!(0, _isArray.default)(path)) throw Error('del.path_not_array ' + path); if (!children) return null; // This path does not exist. if (!path.length) return []; // Remove everything. var key = path[0]; var ix = (0, _node.findFirst)(children, key); var node = children[ix]; if (!node || node.key > key || (0, _node.isRange)(node)) return children; // let filteredNode; if (path.length === 1) { var _context; // This is the final segment, delete the matching node from children. return (0, _concat.default)(_context = (0, _slice.default)(children).call(children, 0, ix)).call(_context, (0, _slice.default)(children).call(children, ix + 1)); } if (!(0, _node.isBranch)(node)) return children; // Recurse into the next slice. var filteredChildren = remove(node.children, (0, _slice.default)(path).call(path, 1)); if (filteredChildren === path.children) return children; var filteredNode = filteredChildren.length ? (0, _extends2.default)({}, node, { children: filteredChildren }) : []; return (0, _concat.default)(_context2 = (0, _slice.default)(children).call(children, 0, ix)).call(_context2, filteredNode, (0, _slice.default)(children).call(children, ix + 1)); }