UNPKG

@graffy/common

Version:

Common libraries that used by various Graffy modules.

109 lines (81 loc) 3.66 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); exports.__esModule = true; exports.makePath = makePath; exports.wrap = wrap; exports.unwrap = unwrap; exports.remove = remove; exports.PATH_SEPARATOR = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends")); var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat")); var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice")); var _every = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/every")); var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array")); var _node = require("../node"); 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 []; var pathArray = path.split(PATH_SEPARATOR); return pathArray[0] === '' ? (0, _slice["default"])(pathArray).call(pathArray, 1) : pathArray; } 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 = [{ key: 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 key = path[i]; children = node.children; if (!children) return null; // This path does not exist. node = children[(0, _node.getIndex)(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 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.getIndex)(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)); }