tree-visit
Advanced tools
Comparing version 0.1.2 to 0.1.3
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const index_1 = require("../index"); | ||
const access_1 = require("../access"); | ||
const diagram_1 = require("../diagram"); | ||
const find_1 = require("../find"); | ||
const flat_1 = require("../flat"); | ||
const flatMap_1 = require("../flatMap"); | ||
const index_1 = require("../index"); | ||
const map_1 = require("../map"); | ||
const reduce_1 = require("../reduce"); | ||
const withOptions_1 = require("../withOptions"); | ||
const diagram_1 = require("../diagram"); | ||
const flat_1 = require("../flat"); | ||
function getChildren(node) { | ||
@@ -218,2 +221,55 @@ var _a; | ||
}); | ||
describe('flatMap', () => { | ||
it('flatMaps a tree', () => { | ||
const items = (0, flatMap_1.flatMap)(example, { | ||
getChildren, | ||
transform: (node) => [{ name: node.name, depth: node.indexPath.length }], | ||
}); | ||
expect(items).toEqual([ | ||
{ depth: 0, name: 'a' }, | ||
{ depth: 1, name: 'b' }, | ||
{ depth: 2, name: 'b1' }, | ||
{ depth: 2, name: 'b2' }, | ||
{ depth: 1, name: 'c' }, | ||
{ depth: 2, name: 'c1' }, | ||
{ depth: 2, name: 'c2' }, | ||
]); | ||
}); | ||
}); | ||
describe('reduce', () => { | ||
it('reduces a tree', () => { | ||
const result = (0, reduce_1.reduce)(example, { | ||
getChildren, | ||
initialResult: 0, | ||
nextResult: (result) => result + 1, | ||
}); | ||
expect(result).toEqual(7); | ||
}); | ||
}); | ||
describe('map', () => { | ||
it('maps a tree', () => { | ||
const result = (0, map_1.map)(example, { | ||
getChildren, | ||
transform: (node, children) => ({ | ||
id: node.name, | ||
items: children, | ||
}), | ||
}); | ||
expect((0, flat_1.flat)(result, { | ||
getChildren: (node) => node.items, | ||
}).map((node) => node.id)).toEqual(['a', 'b', 'b1', 'b2', 'c', 'c1', 'c2']); | ||
}); | ||
it('maps a tree and omits nodes', () => { | ||
const result = (0, map_1.map)(example, { | ||
getChildren, | ||
transform: (node, children) => ({ | ||
id: node.name, | ||
items: children.filter((child) => child.id !== 'b1' && child.id !== 'c'), | ||
}), | ||
}); | ||
expect((0, flat_1.flat)(result, { | ||
getChildren: (node) => node.items, | ||
}).map((node) => node.id)).toEqual(['a', 'b', 'b2']); | ||
}); | ||
}); | ||
describe('diagram', () => { | ||
@@ -357,3 +413,5 @@ const getLabel = (node) => node.name; | ||
var _a; | ||
const { find, access, visit } = (0, withOptions_1.withOptions)({ getChildren }); | ||
const { find, access, visit, reduce, flatMap, map } = (0, withOptions_1.withOptions)({ | ||
getChildren, | ||
}); | ||
let enterNames = []; | ||
@@ -370,2 +428,19 @@ visit(example, { | ||
expect(access(example, [0, 0]).name).toEqual('b1'); | ||
expect(reduce(example, (result, node) => (result ? result + ' ' + node.name : node.name), '')).toEqual('a b b1 b2 c c1 c2'); | ||
expect(flatMap(example, (node) => node.name.split(''))).toEqual([ | ||
'a', | ||
'b', | ||
'b', | ||
'1', | ||
'b', | ||
'2', | ||
'c', | ||
'c', | ||
'1', | ||
'c', | ||
'2', | ||
]); | ||
expect(map(example, (node, transformedChildren) => ({ | ||
id: `${node.name}:${transformedChildren.length}`, | ||
})).id).toEqual('a:2'); | ||
}); | ||
@@ -372,0 +447,0 @@ it('supports typed finding', () => { |
@@ -7,4 +7,3 @@ "use strict"; | ||
let found; | ||
(0, visit_1.visit)(node, { | ||
onEnter: (child, indexPath) => { | ||
(0, visit_1.visit)(node, Object.assign(Object.assign({}, options), { onEnter: (child, indexPath) => { | ||
if (options.predicate(child, indexPath)) { | ||
@@ -14,5 +13,3 @@ found = child; | ||
} | ||
}, | ||
getChildren: options.getChildren, | ||
}); | ||
} })); | ||
return found; | ||
@@ -19,0 +16,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.flat = void 0; | ||
const visit_1 = require("./visit"); | ||
const reduce_1 = require("./reduce"); | ||
/** | ||
@@ -11,11 +11,7 @@ * Returns an array containing the root node and all of its descendants. | ||
function flat(node, options) { | ||
let nodes = []; | ||
(0, visit_1.visit)(node, { | ||
onEnter: (child) => { | ||
nodes.push(child); | ||
}, | ||
getChildren: options.getChildren, | ||
}); | ||
return nodes; | ||
return (0, reduce_1.reduce)(node, Object.assign(Object.assign({}, options), { initialResult: [], nextResult: (result, child) => { | ||
result.push(child); | ||
return result; | ||
} })); | ||
} | ||
exports.flat = flat; |
@@ -21,3 +21,4 @@ "use strict"; | ||
function visit(node, options) { | ||
const normalizedOptions = Object.assign({ onEnter: () => { }, onLeave: () => { } }, options); | ||
var _a, _b; | ||
const normalizedOptions = Object.assign(Object.assign({}, options), { onEnter: (_a = options.onEnter) !== null && _a !== void 0 ? _a : (() => { }), onLeave: (_b = options.onLeave) !== null && _b !== void 0 ? _b : (() => { }) }); | ||
visitInternal(node, normalizedOptions); | ||
@@ -24,0 +25,0 @@ } |
@@ -0,6 +1,9 @@ | ||
import { DiagramOptions } from './diagram'; | ||
import { FindOptions, FindOptionsTyped } from './find'; | ||
import { FlatMapOptions } from './flatMap'; | ||
import { IndexPath } from './indexPath'; | ||
import { MapOptions } from './map'; | ||
import { BaseOptions } from './options'; | ||
import { ReduceOptions } from './reduce'; | ||
import { VisitOptions } from './visit'; | ||
import { IndexPath } from './indexPath'; | ||
import { FindOptions, FindOptionsTyped } from './find'; | ||
import { DiagramOptions } from './diagram'; | ||
declare type WithoutBase<O> = Omit<O, keyof BaseOptions<unknown>>; | ||
@@ -56,2 +59,10 @@ export declare type WithOptions<T> = { | ||
/** | ||
* Map each node into an array of values, which are then flattened into a single array. | ||
* | ||
* This is analogous to `Array.prototype.flatMap` for arrays. | ||
*/ | ||
flatMap<R>(node: T, transform: FlatMapOptions<T, R>['transform']): R[]; | ||
reduce<R>(node: T, nextResult: ReduceOptions<T, R>['nextResult'], initialResult: R): R; | ||
map<R>(node: T, transform: MapOptions<T, R>['transform']): R; | ||
/** | ||
* Visit each node using preorder DFS traversal. | ||
@@ -58,0 +69,0 @@ */ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.withOptions = void 0; | ||
const visit_1 = require("./visit"); | ||
const access_1 = require("./access"); | ||
const diagram_1 = require("./diagram"); | ||
const find_1 = require("./find"); | ||
const flat_1 = require("./flat"); | ||
const find_1 = require("./find"); | ||
const diagram_1 = require("./diagram"); | ||
const flatMap_1 = require("./flatMap"); | ||
const map_1 = require("./map"); | ||
const reduce_1 = require("./reduce"); | ||
const visit_1 = require("./visit"); | ||
/** | ||
@@ -31,2 +34,5 @@ * Return every tree utility function with options partially applied. | ||
flat: (node) => (0, flat_1.flat)(node, baseOptions), | ||
flatMap: (node, transform) => (0, flatMap_1.flatMap)(node, Object.assign(Object.assign({}, baseOptions), { transform })), | ||
reduce: (node, nextResult, initialResult) => (0, reduce_1.reduce)(node, Object.assign(Object.assign({}, baseOptions), { nextResult, initialResult })), | ||
map: (node, transform) => (0, map_1.map)(node, Object.assign(Object.assign({}, baseOptions), { transform })), | ||
findAllIndexPaths: (node, predicateOrOptions) => typeof predicateOrOptions === 'function' | ||
@@ -33,0 +39,0 @@ ? (0, find_1.findAllIndexPaths)(node, Object.assign(Object.assign({}, baseOptions), { predicate: predicateOrOptions })) |
{ | ||
"name": "tree-visit", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "A tree traversal library.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55033
32
1247