tree-visit
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -16,2 +16,6 @@ "use strict"; | ||
} | ||
// Create a new child object for each node to test that we're not relying on object identity | ||
function getChildrenUnstable(node) { | ||
return getChildren(node).map((item) => (Object.assign({}, item))); | ||
} | ||
const example = { | ||
@@ -274,2 +278,14 @@ name: 'a', | ||
}); | ||
it('maps a tree with unstable object identity', () => { | ||
const result = (0, map_1.map)(example, { | ||
getChildren: getChildrenUnstable, | ||
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']); | ||
}); | ||
}); | ||
@@ -276,0 +292,0 @@ describe('diagram', () => { |
@@ -13,4 +13,4 @@ import { IndexPath } from './indexPath'; | ||
* The shape of the tree remains the same. You can omit nodes from the tree by | ||
* filtering them out of the `children` argument. The root can't be omitted. | ||
* filtering them out of the `transformedChildren` argument. The root can't be omitted. | ||
*/ | ||
export declare function map<T, U>(node: T, options: MapOptions<T, U>): U; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.map = void 0; | ||
const access_1 = require("./access"); | ||
const visit_1 = require("./visit"); | ||
@@ -10,18 +9,18 @@ /** | ||
* The shape of the tree remains the same. You can omit nodes from the tree by | ||
* filtering them out of the `children` argument. The root can't be omitted. | ||
* filtering them out of the `transformedChildren` argument. The root can't be omitted. | ||
*/ | ||
function map(node, options) { | ||
const childrenMap = new Map(); | ||
const childrenMap = {}; | ||
(0, visit_1.visit)(node, Object.assign(Object.assign({}, options), { onLeave: (child, indexPath) => { | ||
var _a, _b; | ||
const transformed = options.transform(child, (_a = childrenMap.get(child)) !== null && _a !== void 0 ? _a : [], indexPath); | ||
const parent = indexPath.length > 0 | ||
? (0, access_1.access)(node, indexPath.slice(0, -1), options) | ||
: undefined; | ||
const parentChildren = (_b = childrenMap.get(parent)) !== null && _b !== void 0 ? _b : []; | ||
indexPath = [0, ...indexPath]; | ||
const key = indexPath.join(); | ||
const transformed = options.transform(child, (_a = childrenMap[key]) !== null && _a !== void 0 ? _a : [], indexPath); | ||
const parentKey = indexPath.slice(0, -1).join(); | ||
const parentChildren = (_b = childrenMap[parentKey]) !== null && _b !== void 0 ? _b : []; | ||
parentChildren.push(transformed); | ||
childrenMap.set(parent, parentChildren); | ||
childrenMap[parentKey] = parentChildren; | ||
} })); | ||
return childrenMap.get(undefined)[0]; | ||
return childrenMap[''][0]; | ||
} | ||
exports.map = map; |
{ | ||
"name": "tree-visit", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "A tree traversal library.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -87,3 +87,3 @@ # tree-visit | ||
accessPath(rootNode, [1, 0], { getChildren }) | ||
access(rootNode, [1, 0], { getChildren }) | ||
// #=> [{ name: 'a', children: [...] }, { name: 'c', children: [...] }, { name: 'd' }] | ||
@@ -90,0 +90,0 @@ ``` |
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
56726
33
1262