@cranq/tree-utils
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -14,5 +14,3 @@ export * from "./path/pathEquals"; | ||
export * from "./types/ContainerNode"; | ||
export * from "./types/DictionaryNode"; | ||
export * from "./types/LeafNode"; | ||
export * from "./types/ListNode"; | ||
export * from "./types/Node"; | ||
@@ -19,0 +17,0 @@ export * from "./types/NodeGenerator"; |
@@ -123,5 +123,3 @@ module.exports = | ||
__exportStar(__webpack_require__(/*! ./types/ContainerNode */ "./src/types/ContainerNode.ts"), exports); | ||
__exportStar(__webpack_require__(/*! ./types/DictionaryNode */ "./src/types/DictionaryNode.ts"), exports); | ||
__exportStar(__webpack_require__(/*! ./types/LeafNode */ "./src/types/LeafNode.ts"), exports); | ||
__exportStar(__webpack_require__(/*! ./types/ListNode */ "./src/types/ListNode.ts"), exports); | ||
__exportStar(__webpack_require__(/*! ./types/Node */ "./src/types/Node.ts"), exports); | ||
@@ -595,16 +593,2 @@ __exportStar(__webpack_require__(/*! ./types/NodeGenerator */ "./src/types/NodeGenerator.ts"), exports); | ||
/***/ "./src/types/DictionaryNode.ts": | ||
/*!*************************************!*\ | ||
!*** ./src/types/DictionaryNode.ts ***! | ||
\*************************************/ | ||
/*! no static exports found */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/***/ }), | ||
/***/ "./src/types/LeafNode.ts": | ||
@@ -624,16 +608,2 @@ /*!*******************************!*\ | ||
/***/ "./src/types/ListNode.ts": | ||
/*!*******************************!*\ | ||
!*** ./src/types/ListNode.ts ***! | ||
\*******************************/ | ||
/*! no static exports found */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/***/ }), | ||
/***/ "./src/types/Node.ts": | ||
@@ -640,0 +610,0 @@ /*!***************************!*\ |
@@ -1,4 +0,2 @@ | ||
import { DictionaryNode } from "./DictionaryNode"; | ||
import { ListNode } from "./ListNode"; | ||
import { Node } from "./Node"; | ||
export declare type ContainerNode<V extends Node> = ListNode<V> | DictionaryNode<any, V>; | ||
export declare type ContainerNode<V extends Node> = Array<V> | Record<any, V>; |
{ | ||
"name": "@cranq/tree-utils", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Tree manipulation utilities", | ||
"scripts": { | ||
"build": "webpack --display-error-details", | ||
"test": "tsc --noEmit && jest" | ||
"test": "tsc --noEmit && jest", | ||
"ci": "npm ci && npm run build && npm run test" | ||
}, | ||
@@ -29,2 +30,3 @@ "author": "Dan Stocker", | ||
"jest": "^26.0.1", | ||
"jest-junit": "^12.0.0", | ||
"ts-jest": "^26.1.1", | ||
@@ -31,0 +33,0 @@ "ts-loader": "^7.0.5", |
@@ -14,5 +14,3 @@ export * from "./path/pathEquals"; | ||
export * from "./types/ContainerNode"; | ||
export * from "./types/DictionaryNode"; | ||
export * from "./types/LeafNode"; | ||
export * from "./types/ListNode"; | ||
export * from "./types/Node"; | ||
@@ -19,0 +17,0 @@ export * from "./types/NodeGenerator"; |
import {ContainerNode} from "../types/ContainerNode"; | ||
import {DictionaryNode} from "../types/DictionaryNode"; | ||
import {Node} from "../types/Node"; | ||
@@ -36,6 +35,6 @@ import {NodeGenerator} from "../types/NodeGenerator"; | ||
for (const key in values) { | ||
const value = (values as DictionaryNode<string, V>)[key]; | ||
(node as DictionaryNode<string, V>)[key] = | ||
const value = (values as Record<string, V>)[key]; | ||
(node as Record<string, V>)[key] = | ||
typeof value === "function" ? | ||
value((node as DictionaryNode<string, V>)[key]) : | ||
value((node as Record<string, V>)[key]) : | ||
value; | ||
@@ -42,0 +41,0 @@ } |
import {map} from "@cranq/object-utils"; | ||
import {ContainerNode} from "../types/ContainerNode"; | ||
import {DictionaryNode} from "../types/DictionaryNode"; | ||
import {ListNode} from "../types/ListNode"; | ||
import {Node} from "../types/Node"; | ||
@@ -53,5 +51,5 @@ import {NodeGenerator} from "../types/NodeGenerator"; | ||
function evalDictionaryNode<V extends Node>( | ||
values: DictionaryNode<string, V | NodeGenerator<V> | undefined>, | ||
node: DictionaryNode<string, V> | ||
): DictionaryNode<string, V> { | ||
values: Record<string, V | NodeGenerator<V> | undefined>, | ||
node: Record<string, V> | ||
): Record<string, V> { | ||
return map(values, (value, key) => typeof value === "function" ? | ||
@@ -63,4 +61,4 @@ value(node[key]) : | ||
function evalNewDictionaryNode<V extends Node>( | ||
values: DictionaryNode<string, V | NodeGenerator<V> | undefined> | ||
): DictionaryNode<string, V> { | ||
values: Record<string, V | NodeGenerator<V> | undefined> | ||
): Record<string, V> { | ||
return map(values, (value) => typeof value === "function" ? | ||
@@ -73,7 +71,7 @@ value() : | ||
values: ContainerNode<V | NodeGenerator<V> | undefined>, | ||
node: ListNode<V> | ||
): ListNode<V> { | ||
node: Array<V> | ||
): Array<V> { | ||
const result = [...node]; | ||
for (const key in values) { | ||
const value = (values as DictionaryNode<string, V>)[key]; | ||
const value = (values as Record<string, V>)[key]; | ||
const i = Number(key); | ||
@@ -88,4 +86,4 @@ result[i] = typeof value === "function" ? | ||
function evalNewListNode<V extends Node>( | ||
values: ListNode<V | NodeGenerator<V> | undefined> | ||
): ListNode<V> { | ||
values: Array<V | NodeGenerator<V> | undefined> | ||
): Array<V> { | ||
return values.map((value) => typeof value === "function" ? | ||
@@ -92,0 +90,0 @@ value() : |
import {ContainerNode} from "../types/ContainerNode"; | ||
import {DictionaryNode} from "../types/DictionaryNode"; | ||
import {Node} from "../types/Node"; | ||
@@ -16,3 +15,3 @@ import {Path} from "../types/Path"; | ||
): void { | ||
const parent = getNode(tree, path.slice(0, -1)) as DictionaryNode<string, N>; | ||
const parent = getNode(tree, path.slice(0, -1)) as Record<string, N>; | ||
if (parent instanceof Object) { | ||
@@ -19,0 +18,0 @@ const lastPathComponent = path[path.length - 1]; |
import {filter} from "@cranq/object-utils"; | ||
import {ContainerNode} from "../types/ContainerNode"; | ||
import {DictionaryNode} from "../types/DictionaryNode"; | ||
import {Node} from "../types/Node"; | ||
@@ -30,3 +29,3 @@ import {Path} from "../types/Path"; | ||
} else { | ||
parentAfter = filter(parentBefore as DictionaryNode<string, N>, | ||
parentAfter = filter(parentBefore as Record<string, N>, | ||
(value, key) => key !== lastPathComponent); | ||
@@ -33,0 +32,0 @@ } |
import {isEmpty} from "@cranq/object-utils"; | ||
import {ContainerNode} from "../types/ContainerNode"; | ||
import {DictionaryNode} from "../types/DictionaryNode"; | ||
import {Node} from "../types/Node"; | ||
@@ -17,6 +16,6 @@ import {Path} from "../types/Path"; | ||
): void { | ||
let parent = tree as DictionaryNode<string, Node>; | ||
let parent = tree as Record<string, Node>; | ||
const pathLength = path.length; | ||
// creating trail of nodes along path | ||
const trail = [] as Array<DictionaryNode<string, Node>>; | ||
const trail = [] as Array<Record<string, Node>>; | ||
for (let i = 0; i < pathLength; i++) { | ||
@@ -26,3 +25,3 @@ const key = path[i]; | ||
trail.push(parent); | ||
parent = parent[key] as DictionaryNode<string, Node>; | ||
parent = parent[key] as Record<string, Node>; | ||
} else { | ||
@@ -46,3 +45,3 @@ break; | ||
const node = parent[key] as Node; | ||
if (!(node instanceof Object) || isEmpty(node as DictionaryNode<string, Node>)) { | ||
if (!(node instanceof Object) || isEmpty(node as Record<string, Node>)) { | ||
delete parent[key]; | ||
@@ -49,0 +48,0 @@ } |
import {ContainerNode} from "../types/ContainerNode"; | ||
import {DictionaryNode} from "../types/DictionaryNode"; | ||
import {Node} from "../types/Node"; | ||
@@ -19,3 +18,3 @@ import {Path} from "../types/Path"; | ||
if (node instanceof Object) { | ||
node = (node as DictionaryNode<string, Node>)[path[i]]; | ||
node = (node as Record<string, Node>)[path[i]]; | ||
} else { | ||
@@ -22,0 +21,0 @@ node = undefined; |
import {ContainerNode} from "../types/ContainerNode"; | ||
import {DictionaryNode} from "../types/DictionaryNode"; | ||
import {Node} from "../types/Node"; | ||
@@ -20,3 +19,3 @@ import {NodeGenerator} from "../types/NodeGenerator"; | ||
const length = path.length - 1; | ||
let parent = tree as DictionaryNode<string, Node>; | ||
let parent = tree as Record<string, Node>; | ||
for (let i = 0; i < length; i++) { | ||
@@ -26,3 +25,3 @@ const key = path[i]; | ||
if (node instanceof Object) { | ||
parent = node as DictionaryNode<string, Node>; | ||
parent = node as Record<string, Node>; | ||
} else { | ||
@@ -39,3 +38,3 @@ // looking ahead to determine parent type | ||
parent[key] = node; | ||
parent = node as DictionaryNode<string, Node>; | ||
parent = node as Record<string, Node>; | ||
} | ||
@@ -42,0 +41,0 @@ } |
import {ContainerNode} from "../types/ContainerNode"; | ||
import {DictionaryNode} from "../types/DictionaryNode"; | ||
import {Node} from "../types/Node"; | ||
@@ -21,3 +20,3 @@ import {NodeGenerator} from "../types/NodeGenerator"; | ||
const length = path.length - 1; | ||
let parent = result as DictionaryNode<string, Node>; | ||
let parent = result as Record<string, Node>; | ||
for (let i = 0; i < length; i++) { | ||
@@ -42,3 +41,3 @@ const key = path[i]; | ||
parent[key] = node; | ||
parent = node as DictionaryNode<string, Node>; | ||
parent = node as Record<string, Node>; | ||
} | ||
@@ -45,0 +44,0 @@ const lastPathComponent = path[length]; |
@@ -1,7 +0,3 @@ | ||
import {DictionaryNode} from "./DictionaryNode"; | ||
import {ListNode} from "./ListNode"; | ||
import {Node} from "./Node"; | ||
export type ContainerNode<V extends Node> = | ||
ListNode<V> | ||
| DictionaryNode<any, V>; | ||
export type ContainerNode<V extends Node> = Array<V> | Record<any, V>; |
Sorry, the diff of this file is not supported yet
82593
9
52
1780