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.0 to 0.1.1

lib/flat.d.ts

28

lib/__tests__/index.js

@@ -8,2 +8,3 @@ "use strict";

const diagram_1 = require("../diagram");
const flat_1 = require("../flat");
function getChildren(node) {

@@ -202,2 +203,18 @@ var _a;

});
describe('flat', () => {
it('flattens a tree', () => {
const nodes = (0, flat_1.flat)(example, {
getChildren,
});
expect(nodes.map((node) => node.name)).toEqual([
'a',
'b',
'b1',
'b2',
'c',
'c1',
'c2',
]);
});
});
describe('diagram', () => {

@@ -366,3 +383,3 @@ const getLabel = (node) => node.name;

var _a;
const { find, findAllIndexPaths, visit, diagram } = (0, withOptions_1.withOptions)({
const { find, findAllIndexPaths, visit, diagram, flat } = (0, withOptions_1.withOptions)({
getChildren,

@@ -375,2 +392,11 @@ });

expect(enterNames).toEqual(['a', 'b', 'b1', 'b2', 'c', 'c1', 'c2']);
expect(flat(example).map((node) => node.name)).toEqual([
'a',
'b',
'b1',
'b2',
'c',
'c1',
'c2',
]);
expect((_a = find(example, (node) => node.name === 'b1')) === null || _a === void 0 ? void 0 : _a.name).toEqual('b1');

@@ -377,0 +403,0 @@ expect(findAllIndexPaths(example, (node) => node.name.startsWith('b'))).toEqual([[0], [0, 0], [0, 1]]);

@@ -7,2 +7,3 @@ export * from './indexPath';

export * from './find';
export * from './flat';
export * from './withOptions';

@@ -19,2 +19,3 @@ "use strict";

__exportStar(require("./find"), exports);
__exportStar(require("./flat"), exports);
__exportStar(require("./withOptions"), exports);

@@ -8,6 +8,22 @@ import { BaseOptions } from './options';

export declare type WithOptions<T> = {
/**
* Returns a node by its `IndexPath`.
*
* The first node is implicitly included in the `IndexPath` (i.e. no need to pass a `0` first in every `IndexPath`).
*/
access(node: T, indexPath: IndexPath): T;
/**
* Returns an array of each node in an `IndexPath`.
*
* The first node is implicitly included in the `IndexPath` (i.e. no need to pass a `0` first in every `IndexPath`).
*/
accessPath(node: T, indexPath: IndexPath): T[];
/**
* Generate a diagram of the tree, as a string.
*/
diagram(node: T, getLabel: DiagramOptions<T>['getLabel']): string;
diagram(node: T, options: WithoutBase<DiagramOptions<T>>): string;
/**
* Find a node matching a predicate function.
*/
find(node: T, predicate: FindOptions<T>['predicate']): T | undefined;

@@ -17,2 +33,5 @@ find(node: T, options: WithoutBase<FindOptions<T>>): T | undefined;

find<S extends T>(node: T, options: WithoutBase<FindOptionsTyped<T, S>>): S | undefined;
/**
* Find all nodes matching a predicate function.
*/
findAll(node: T, predicate: FindOptions<T>['predicate']): T[];

@@ -22,7 +41,19 @@ findAll(node: T, options: WithoutBase<FindOptions<T>>): T[];

findAll<S extends T>(node: T, options: WithoutBase<FindOptionsTyped<T, S>>): S[];
/**
* Find the `IndexPath` of a node matching a predicate function.
*/
findIndexPath(node: T, predicate: FindOptions<T>['predicate']): IndexPath | undefined;
findIndexPath(node: T, options: WithoutBase<FindOptions<T>>): IndexPath | undefined;
/**
* Find the `IndexPath` of all nodes matching a predicate function.
*/
findAllIndexPaths(node: T, predicate: FindOptions<T>['predicate']): IndexPath[];
findAllIndexPaths(node: T, options: WithoutBase<FindOptions<T>>): IndexPath[];
/**
* Returns an array containing the root node and all of its descendants.
*
* This is analogous to `Array.prototype.flat` for flattening arrays.
*/
flat(node: T): T[];
/**
* Visit each node using preorder DFS traversal.

@@ -29,0 +60,0 @@ */

@@ -6,2 +6,3 @@ "use strict";

const access_1 = require("./access");
const flat_1 = require("./flat");
const find_1 = require("./find");

@@ -30,2 +31,3 @@ const diagram_1 = require("./diagram");

: (0, find_1.findIndexPath)(node, Object.assign(Object.assign({}, baseOptions), predicateOrOptions)),
flat: (node) => (0, flat_1.flat)(node, baseOptions),
findAllIndexPaths: (node, predicateOrOptions) => typeof predicateOrOptions === 'function'

@@ -32,0 +34,0 @@ ? (0, find_1.findAllIndexPaths)(node, Object.assign(Object.assign({}, baseOptions), { predicate: predicateOrOptions }))

2

package.json
{
"name": "tree-visit",
"version": "0.1.0",
"version": "0.1.1",
"description": "A tree traversal library.",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -27,2 +27,3 @@ # tree-visit

- [findIndexPath](#findIndexPath)
- [flat](#flat)
- [visit](#visit)

@@ -87,3 +88,3 @@ - [withOptions](#withOptions)

access(rootNode, [1, 0], { getChildren })
accessPath(rootNode, [1, 0], { getChildren })
// #=> [{ name: 'a', children: [...] }, { name: 'c', children: [...] }, { name: 'd' }]

@@ -296,2 +297,34 @@ ```

### `flat`
Returns an array containing the root node and all of its descendants.
This is analogous to `Array.prototype.flat` for flattening arrays.
**Type**: `function flat<T>(node: T, options: BaseOptions<T>): T[]`
#### Example
```js
import { flat } from 'tree-visit'
const getChildren = (node) => node.children || []
const rootNode = {
name: 'a',
children: [
{ name: 'b' },
{
name: 'c',
children: [{ name: 'd' }],
},
],
}
flat(rootNode, { getChildren }).map((node) => node.name)
// #=> ['a', 'b', 'c', 'd']
```
---
### `withOptions`

@@ -298,0 +331,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