@skele/core
Version:
Core package of the Skele framework. Element definitions, registrations and traversal.
1 lines • 5.01 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:true});exports.zipper=zipper;exports.getChildren=exports.edit=exports.replace=exports.left=exports.right=exports.root=exports.up=exports.down=exports.rights=exports.lefts=exports.path=exports.makeNode=exports.children=exports.isBranch=exports.value=exports.node=exports.makeZipper=void 0;var _deprecated=_interopRequireDefault(require("../log/deprecated"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};var ownKeys=Object.keys(source);if(typeof Object.getOwnPropertySymbols==='function'){ownKeys=ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym){return Object.getOwnPropertyDescriptor(source,sym).enumerable;}));}ownKeys.forEach(function(key){_defineProperty(target,key,source[key]);});}return target;}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}function _toConsumableArray(arr){return _arrayWithoutHoles(arr)||_iterableToArray(arr)||_nonIterableSpread();}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance");}function _arrayWithoutHoles(arr){if(Array.isArray(arr)){for(var i=0,arr2=new Array(arr.length);i<arr.length;i++){arr2[i]=arr[i];}return arr2;}}function _toArray(arr){return _arrayWithHoles(arr)||_iterableToArray(arr)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArray(iter){if((typeof Symbol==="function"?Symbol.iterator:"@@iterator")in Object(iter)||Object.prototype.toString.call(iter)==="[object Arguments]")return Array.from(iter);}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}var END='END';function zipper(isBranch,children,makeNode,root){return{node:root,path:null,meta:{isBranch:isBranch,children:children,makeNode:makeNode}};}var makeZipper=(0,_deprecated.default)('Use partial or direct call to zipper() instead',function(isBranch,children,makeNode){var z=zipper.bind(undefined,isBranch,children,makeNode);z.from=z;return z;});exports.makeZipper=makeZipper;var node=function node(loc){return loc.node;};exports.node=node;var value=(0,_deprecated.default)('use node() instead',node);exports.value=value;var isBranch=function isBranch(loc){return loc.meta.isBranch(loc.node);};exports.isBranch=isBranch;var children=function children(loc){if(isBranch(loc)){return loc.meta.children(loc.node);}throw new Error('called children() on a leaf node');};exports.children=children;var makeNode=function makeNode(loc,node,children){return loc.meta.makeNode(node,children);};exports.makeNode=makeNode;var path=function path(loc){return loc.path.pnodes;};exports.path=path;var lefts=function lefts(loc){return loc.path.l&&loc.path.l.length>0?loc.path.l:null;};exports.lefts=lefts;var rights=function rights(loc){return loc.path.r&&loc.path.r.length>0?loc.path.r:null;};exports.rights=rights;var down=function down(loc){if(isBranch(loc)){var cs=children(loc);if(cs&&cs.length>0){var _cs=_toArray(cs),c=_cs[0],cnext=_cs.slice(1);return{node:c,path:{l:[],r:cnext,pnodes:loc.node.path?[].concat(_toConsumableArray(loc.node.path),[loc.node]):[loc.node],ppath:loc.path},meta:loc.meta};}}return null;};exports.down=down;var up=function up(loc){var node=loc.node,path=loc.path;if(path!=null){var l=path.l,r=path.r,pnodes=path.pnodes,ppath=path.ppath,isChanged=path.isChanged;if(pnodes&&pnodes.length>0){var pnode=pnodes[pnodes.length-1];if(isChanged){return{node:makeNode(loc,pnode,[].concat(_toConsumableArray(l||[]),[node],_toConsumableArray(r||[]))),path:ppath&&_objectSpread({},ppath,{isChanged:true}),meta:loc.meta};}else{return{node:pnode,path:ppath,meta:loc.meta};}}}return null;};exports.up=up;var root=function root(loc){if(loc.path===END){return node(loc);}else{var p=up(loc);if(p==null)return node(loc);return root(p);}};exports.root=root;var right=function right(loc){var node=loc.node,path=loc.path;if(path){var l=path.l,r=path.r;if(r&&r.length>0){return{node:r[0],path:_objectSpread({},path,{l:[].concat(_toConsumableArray(l||[]),[node]),r:r.slice(1)}),meta:loc.meta};}}return null;};exports.right=right;var left=function left(loc){var node=loc.node,path=loc.path;if(path){var l=path.l,r=path.r;if(l&&l.length>0){return{node:l[l.length-1],path:_objectSpread({},path,{l:l.slice(0,-1),r:[node].concat(_toConsumableArray(r||[]))}),meta:loc.meta};}}};exports.left=left;var replace=function replace(node,loc){return _objectSpread({},loc,{node:node,path:_objectSpread({},loc.path||{},{isChanged:loc.path&&loc.path.isChanged||node!==loc.node})});};exports.replace=replace;var edit=function edit(f,loc){return replace(f(node(loc)),loc);};exports.edit=edit;var getChildren=(0,_deprecated.default)('Use children() instead.',children);exports.getChildren=getChildren;