Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tree-visit

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tree-visit - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

LICENSE.md

16

lib/__tests__/index.js

@@ -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', () => {

2

lib/map.d.ts

@@ -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 @@ ```

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc