tree-visit
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -140,9 +140,26 @@ "use strict"; | ||
it('binds options', () => { | ||
const { find, access } = withOptions_1.withOptions({ getChildren }); | ||
const node = find(example, { | ||
var _a; | ||
const { find, access, visit } = withOptions_1.withOptions({ getChildren }); | ||
let enterNames = []; | ||
visit(example, { | ||
onEnter: (child) => { | ||
enterNames.push(child.name); | ||
}, | ||
}); | ||
expect(enterNames).toEqual(['a', 'b', 'b1', 'b2', 'c', 'c1', 'c2']); | ||
expect((_a = find(example, { | ||
predicate: (node) => node.name === 'b1', | ||
}); | ||
expect(node === null || node === void 0 ? void 0 : node.name).toEqual('b1'); | ||
})) === null || _a === void 0 ? void 0 : _a.name).toEqual('b1'); | ||
expect(access(example, [0, 0]).name).toEqual('b1'); | ||
}); | ||
it('supports overloaded calls', () => { | ||
var _a; | ||
const { find, visit } = withOptions_1.withOptions({ getChildren }); | ||
let enterNames = []; | ||
visit(example, (child) => { | ||
enterNames.push(child.name); | ||
}); | ||
expect(enterNames).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'); | ||
}); | ||
}); |
@@ -6,8 +6,18 @@ import { BaseOptions } from './options'; | ||
export declare type WithOptions<T> = { | ||
visit(node: T, options: Omit<VisitOptions<T>, keyof BaseOptions<T>>): void; | ||
access(node: T, indexPath: IndexPath): T; | ||
accessPath(node: T, indexPath: IndexPath): T[]; | ||
find(node: T, predicate: FindOptions<T>['predicate']): T | undefined; | ||
find(node: T, options: Omit<FindOptions<T>, keyof BaseOptions<T>>): T | undefined; | ||
findAll(node: T, predicate: FindOptions<T>['predicate']): T[]; | ||
findAll(node: T, options: Omit<FindOptions<T>, keyof BaseOptions<T>>): T[]; | ||
findIndexPath(node: T, predicate: FindOptions<T>['predicate']): IndexPath | undefined; | ||
findIndexPath(node: T, options: Omit<FindOptions<T>, keyof BaseOptions<T>>): IndexPath | undefined; | ||
/** | ||
* Visit each node using preorder DFS traversal. | ||
*/ | ||
visit(node: T, onEnter: NonNullable<VisitOptions<T>>['onEnter']): void; | ||
/** | ||
* Visit each node using DFS traversal. | ||
*/ | ||
visit(node: T, options: Omit<VisitOptions<T>, keyof BaseOptions<T>>): void; | ||
}; | ||
@@ -14,0 +24,0 @@ /** |
@@ -14,10 +14,18 @@ "use strict"; | ||
return { | ||
visit: (node, options) => visit_1.visit(node, Object.assign(Object.assign({}, baseOptions), options)), | ||
access: (node, indexPath) => access_1.access(node, indexPath, baseOptions), | ||
accessPath: (node, indexPath) => access_1.accessPath(node, indexPath, baseOptions), | ||
find: (node, options) => find_1.find(node, Object.assign(Object.assign({}, baseOptions), options)), | ||
findAll: (node, options) => find_1.findAll(node, Object.assign(Object.assign({}, baseOptions), options)), | ||
findIndexPath: (node, options) => find_1.findIndexPath(node, Object.assign(Object.assign({}, baseOptions), options)), | ||
find: (node, predicateOrOptions) => typeof predicateOrOptions === 'function' | ||
? find_1.find(node, Object.assign(Object.assign({}, baseOptions), { predicate: predicateOrOptions })) | ||
: find_1.find(node, Object.assign(Object.assign({}, baseOptions), predicateOrOptions)), | ||
findAll: (node, predicateOrOptions) => typeof predicateOrOptions === 'function' | ||
? find_1.findAll(node, Object.assign(Object.assign({}, baseOptions), { predicate: predicateOrOptions })) | ||
: find_1.findAll(node, Object.assign(Object.assign({}, baseOptions), predicateOrOptions)), | ||
findIndexPath: (node, predicateOrOptions) => typeof predicateOrOptions === 'function' | ||
? find_1.findIndexPath(node, Object.assign(Object.assign({}, baseOptions), { predicate: predicateOrOptions })) | ||
: find_1.findIndexPath(node, Object.assign(Object.assign({}, baseOptions), predicateOrOptions)), | ||
visit: (node, onEnterOrOptions) => typeof onEnterOrOptions === 'function' | ||
? visit_1.visit(node, Object.assign(Object.assign({}, baseOptions), { onEnter: onEnterOrOptions })) | ||
: visit_1.visit(node, Object.assign(Object.assign({}, baseOptions), onEnterOrOptions)), | ||
}; | ||
} | ||
exports.withOptions = withOptions; |
{ | ||
"name": "tree-visit", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"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
15077
371