@graffy/common
Version:
Common libraries that used by various Graffy modules.
113 lines (92 loc) • 3.33 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { findFirst, isRange, isBranch } from '../node';
import { encodeArgs } from '../coding';
export var PATH_SEPARATOR = '.';
export function makePath(path) {
if (Array.isArray(path) && path.every(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);
}
export function wrapValue(value, path, version) {
if (version === void 0) {
version = 0;
}
var node = _extends({}, encodeArgs(path[path.length - 1]), {
value: value,
version: version
});
return wrap([node], path.slice(0, -1), version);
}
export function wrap(graph, path, version) {
if (version === void 0) {
version = 0;
}
if (!Array.isArray(graph) || !graph.length) return;
if (!Array.isArray(path)) throw Error('wrap.path_not_array ' + path);
var children = graph;
for (var i = path.length - 1; i >= 0; i--) {
children = [_extends({}, encodeArgs(path[i]), {
version: version,
children: children
})];
}
return children;
}
export function unwrap(graph, path) {
var children = graph;
if (!Array.isArray(path)) throw Error('unwrap.path_not_array ' + path);
var node = {
children: children
};
for (var i = 0; i < path.length; i++) {
var _encodeArgs = encodeArgs(path[i]),
key = _encodeArgs.key;
children = node.children;
if (!children) return null; // This path does not exist.
node = children[findFirst(children, key)];
if (!node || node.key > key) return undefined; // We lack knowledge.
if (isRange(node)) return null; // This is known to be null.
}
return node.children || node.value;
}
export function wrapObject(object, path) {
if (!Array.isArray(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;
}
export function unwrapObject(object, path) {
if (!Array.isArray(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;
}
export function remove(children, path) {
if (!Array.isArray(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 = findFirst(children, key);
var node = children[ix];
if (!node || node.key > key || isRange(node)) return children; // let filteredNode;
if (path.length === 1) {
// This is the final segment, delete the matching node from children.
return children.slice(0, ix).concat(children.slice(ix + 1));
}
if (!isBranch(node)) return children; // Recurse into the next slice.
var filteredChildren = remove(node.children, path.slice(1));
if (filteredChildren === path.children) return children;
var filteredNode = filteredChildren.length ? _extends({}, node, {
children: filteredChildren
}) : [];
return children.slice(0, ix).concat(filteredNode, children.slice(ix + 1));
}