d-forest
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -11,3 +11,4 @@ declare const _exports: { | ||
readonly REDUCE: string; | ||
readonly FIND_PATH: string; | ||
}; | ||
export = _exports; |
@@ -1,5 +0,1 @@ | ||
declare var depthFirst: any; | ||
declare var breadthFirst: any; | ||
declare var Actions: any; | ||
declare var isObject: any, copyByPath: any; | ||
declare type Path = Array<string | number>; | ||
@@ -9,3 +5,2 @@ declare type Callback<T> = (node: any, depth: number, path: Path) => T; | ||
declare type PureFn<T> = (value: any) => T; | ||
declare type NodeType = 'node' | 'leaf'; | ||
declare class Forest { | ||
@@ -27,4 +22,3 @@ forEachLeaf: (data: any, callback: Callback<void>) => void; | ||
findLevel: (data: any, predicate: Callback<boolean>) => number; | ||
findPath: (data: any, predicate: Callback<boolean>, type?: NodeType) => Path; | ||
findPathAll: (data: any, predicate: Callback<boolean>, type?: NodeType) => Path[]; | ||
findPath: (data: any, predicate: Callback<boolean>) => Path; | ||
findByPath: <Type>(data: any, path: Path) => Type; | ||
@@ -37,4 +31,5 @@ removeByPath: (data: any, path: Path) => any; | ||
updateLeaves: <T>(data: any, predicate: Callback<boolean>, callback: PureFn<T>) => any; | ||
removeByLevel: (data: any, level: number) => any; | ||
} | ||
declare const _default: Forest; | ||
export default _default; |
{ | ||
"name": "d-forest", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "Find nested object in a tree-like structure", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -164,3 +164,3 @@ ## d-forest | ||
- #### updateNode | updateLeaves | ||
- #### updateNodes | updateLeaves | ||
@@ -171,9 +171,20 @@ ```javascript | ||
(node, depth) => depth === 1 && node.active, | ||
(node) => ({ ...node, products: [] }) | ||
(node) => ({ ...node, products: null }) | ||
); | ||
// { | ||
// c1: { name: 'category1', active: false }, | ||
// c2: { name: 'category2', active: true, products: [] }, | ||
// c3: { name: 'category3', active: true, products: [] } | ||
// c2: { name: 'category2', active: true, products: null }, | ||
// c3: { name: 'category3', active: true, products: null } | ||
// } | ||
``` | ||
- #### removeByLevel | ||
```javascript | ||
df.removeByLevel(data, 2); | ||
// { | ||
// c1: { name: 'category1', active: false }, | ||
// c2: { name: 'category2', active: true }, | ||
// c3: { name: 'category3', active: true } | ||
// } | ||
``` |
@@ -11,2 +11,3 @@ module.exports = Object.freeze({ | ||
REDUCE: 'reduce', | ||
FIND_PATH: 'find-path', | ||
}); |
@@ -53,5 +53,3 @@ const Actions = require('../actions'); | ||
let value = callback(node, depth, path); | ||
if (value) { | ||
response = [...response, node]; | ||
} | ||
if (value) response.push(node); | ||
return iterate(node, depth, path); | ||
@@ -76,3 +74,4 @@ }; | ||
if (payload.level === depth) { | ||
response = [...response, node]; | ||
let value = callback({ node, path }); | ||
response.push(value); | ||
return []; | ||
@@ -83,2 +82,10 @@ } | ||
break; | ||
case Actions.FIND_PATH: | ||
response = []; | ||
next = (node, depth, path) => { | ||
let value = callback(node, depth, path); | ||
if (value) response.push(path); | ||
return iterate(node, depth, path); | ||
}; | ||
break; | ||
default: | ||
@@ -85,0 +92,0 @@ next = (node, depth, path) => { |
@@ -96,1 +96,12 @@ const df = require('../index'); | ||
}); | ||
test('remove by level', () => { | ||
let c1 = { name: 'category1', active: false }; | ||
let c2 = { name: 'category2', active: true, products: [] }; | ||
let c3 = { name: 'category3', active: true, products: [] }; | ||
expect(df.removeByLevel(data, 1)).toStrictEqual([c1, c2, c3]); | ||
delete c2.products; | ||
delete c3.products; | ||
expect(df.removeByLevel(data2, 2)).toStrictEqual({ c1, c2, c3 }); | ||
expect(df.removeByLevel(data3, 1)).toStrictEqual([[], []]); | ||
}); |
@@ -48,3 +48,3 @@ const Actions = require('../actions'); | ||
let value = callback(node, depth, path); | ||
response = [...response, value]; | ||
response.push(value); | ||
} | ||
@@ -72,5 +72,3 @@ } | ||
let value = callback(node, depth, path); | ||
if (value) { | ||
response = [...response, node]; | ||
} | ||
if (value) response.push(node); | ||
} | ||
@@ -119,4 +117,12 @@ }; | ||
let hasChildren = iterate(node, depth, path, value); | ||
if (!hasChildren) response.push(value); | ||
}; | ||
break; | ||
case Actions.FIND_PATH: | ||
response = []; | ||
next = (node, depth, path) => { | ||
let hasChildren = iterate(node, depth, path); | ||
if (!hasChildren) { | ||
response = [...response, value]; | ||
let value = callback(node, depth, path); | ||
if (value) response.push(path); | ||
} | ||
@@ -123,0 +129,0 @@ }; |
@@ -45,3 +45,6 @@ var depthFirst = require('./algorithms/depth-first'); | ||
return data; | ||
return breadthFirst(data, function () { }, Actions.BY_LEVEL, { level: level }); | ||
return breadthFirst(data, function (_a) { | ||
var node = _a.node; | ||
return node; | ||
}, Actions.BY_LEVEL, { level: level }); | ||
}; | ||
@@ -67,6 +70,5 @@ this.reduce = function (data, callback, initial) { | ||
}; | ||
this.findPath = function (data, predicate, type) { | ||
this.findPath = function (data, predicate) { | ||
var _path = []; | ||
var findNode = type === 'leaf' ? _this.findLeaf : _this.findNode; | ||
findNode(data, function (node, depth, path) { | ||
_this.findNode(data, function (node, depth, path) { | ||
var value = predicate(node, depth, path); | ||
@@ -79,13 +81,2 @@ if (value) | ||
}; | ||
this.findPathAll = function (data, predicate, type) { | ||
var _paths = []; | ||
var findNodes = type === 'leaf' ? _this.findLeaves : _this.findNodes; | ||
findNodes(data, function (node, depth, path) { | ||
var value = predicate(node, depth, path); | ||
if (value) | ||
_paths.push(path); | ||
return value; | ||
}); | ||
return _paths; | ||
}; | ||
this.findByPath = function (data, path) { | ||
@@ -103,3 +94,3 @@ return path.reduce(function (acc, key) { | ||
this.removeNodes = function (data, predicate) { | ||
var _paths = _this.findPathAll(data, predicate, 'node'); | ||
var _paths = breadthFirst(data, predicate, Actions.FIND_PATH); | ||
var response = data; | ||
@@ -112,3 +103,3 @@ _paths.reverse().forEach(function (path) { | ||
this.removeLeaves = function (data, predicate) { | ||
var _paths = _this.findPathAll(data, predicate, 'leaf'); | ||
var _paths = depthFirst(data, predicate, Actions.FIND_PATH); | ||
var response = data; | ||
@@ -126,3 +117,3 @@ _paths.reverse().forEach(function (path) { | ||
this.updateNodes = function (data, predicate, callback) { | ||
var _paths = _this.findPathAll(data, predicate, 'node'); | ||
var _paths = breadthFirst(data, predicate, Actions.FIND_PATH); | ||
var response = data; | ||
@@ -135,3 +126,3 @@ _paths.reverse().forEach(function (path) { | ||
this.updateLeaves = function (data, predicate, callback) { | ||
var _paths = _this.findPathAll(data, predicate, 'leaf'); | ||
var _paths = depthFirst(data, predicate, Actions.FIND_PATH); | ||
var response = data; | ||
@@ -143,2 +134,14 @@ _paths.forEach(function (path) { | ||
}; | ||
this.removeByLevel = function (data, level) { | ||
var callback = function (_a) { | ||
var path = _a.path; | ||
return path; | ||
}; | ||
var _paths = breadthFirst(data, callback, Actions.BY_LEVEL, { level: level }); | ||
var response = data; | ||
_paths.reverse().forEach(function (path) { | ||
response = _this.removeByPath(response, path); | ||
}); | ||
return response; | ||
}; | ||
} | ||
@@ -145,0 +148,0 @@ return Forest; |
@@ -14,4 +14,2 @@ var depthFirst = require('./algorithms/depth-first'); | ||
type NodeType = 'node' | 'leaf'; | ||
class Forest { | ||
@@ -64,3 +62,3 @@ forEachLeaf = (data, callback: Callback<void>) => { | ||
if (level <= 0) return data; | ||
return breadthFirst(data, () => {}, Actions.BY_LEVEL, { level }); | ||
return breadthFirst(data, ({ node }) => node, Actions.BY_LEVEL, { level }); | ||
}; | ||
@@ -89,6 +87,5 @@ | ||
findPath = (data, predicate: Callback<boolean>, type?: NodeType): Path => { | ||
findPath = (data, predicate: Callback<boolean>): Path => { | ||
var _path = []; | ||
var findNode = type === 'leaf' ? this.findLeaf : this.findNode; | ||
findNode(data, (node, depth, path) => { | ||
this.findNode(data, (node, depth, path) => { | ||
var value = predicate(node, depth, path); | ||
@@ -101,13 +98,2 @@ if (value) _path = path; | ||
findPathAll = (data, predicate: Callback<boolean>, type?: NodeType): Path[] => { | ||
var _paths = []; | ||
var findNodes = type === 'leaf' ? this.findLeaves : this.findNodes; | ||
findNodes(data, (node, depth, path) => { | ||
var value = predicate(node, depth, path); | ||
if (value) _paths.push(path); | ||
return value; | ||
}); | ||
return _paths; | ||
}; | ||
findByPath = <Type>(data, path: Path): Type => { | ||
@@ -126,3 +112,3 @@ return path.reduce((acc, key) => { | ||
removeNodes = (data, predicate: Callback<boolean>) => { | ||
var _paths = this.findPathAll(data, predicate, 'node'); | ||
var _paths = breadthFirst(data, predicate, Actions.FIND_PATH); | ||
var response = data; | ||
@@ -136,3 +122,3 @@ _paths.reverse().forEach((path) => { | ||
removeLeaves = (data, predicate: Callback<boolean>) => { | ||
var _paths = this.findPathAll(data, predicate, 'leaf'); | ||
var _paths = depthFirst(data, predicate, Actions.FIND_PATH); | ||
var response = data; | ||
@@ -152,3 +138,3 @@ _paths.reverse().forEach((path) => { | ||
updateNodes = <T>(data, predicate: Callback<boolean>, callback: PureFn<T>) => { | ||
var _paths = this.findPathAll(data, predicate, 'node'); | ||
var _paths = breadthFirst(data, predicate, Actions.FIND_PATH); | ||
var response = data; | ||
@@ -162,3 +148,3 @@ _paths.reverse().forEach((path) => { | ||
updateLeaves = <T>(data, predicate: Callback<boolean>, callback: PureFn<T>) => { | ||
var _paths = this.findPathAll(data, predicate, 'leaf'); | ||
var _paths = depthFirst(data, predicate, Actions.FIND_PATH); | ||
var response = data; | ||
@@ -170,4 +156,14 @@ _paths.forEach((path) => { | ||
}; | ||
removeByLevel = (data, level: number) => { | ||
var callback = ({ path }) => path; | ||
var _paths = breadthFirst(data, callback, Actions.BY_LEVEL, { level }); | ||
var response = data; | ||
_paths.reverse().forEach((path) => { | ||
response = this.removeByPath(response, path); | ||
}); | ||
return response; | ||
}; | ||
} | ||
module.exports = new Forest(); |
45274
969
189