Skip to content

Commit ddbf05b

Browse files
committed
add-traverse
1 parent 10e5fc1 commit ddbf05b

File tree

6 files changed

+108
-16
lines changed

6 files changed

+108
-16
lines changed

.editorconfig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ insert_final_newline = true
1010

1111
# Matches multiple files with brace expansion notation
1212
# Set default charset
13-
[*.{js,css,html,less,jade}]
13+
[*.{js,ts,css,html,less,jade}]
1414
charset = utf-8
1515

1616
# 2 space indentation
17-
[*.{js,css,html,less,jade}]
17+
[*.{js,ts,css,html,less,jade}]
1818
indent_style = space
19-
indent_size = 2
19+
indent_size = 2

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ typings/
5959

6060
# next.js build output
6161
.next
62+
test.js

index.d.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,37 @@
22
/* @file ./src/log.js */
33

44
interface LogggerOptions {
5-
level: number; /* 0 | 1 | 2 | 3 | 4 */
6-
time: 'iso' | 'ru' | 'en';
5+
level: number; /* 0 | 1 | 2 | 3 | 4 */
6+
time: 'iso' | 'ru' | 'en';
77
}
88
interface Loggger {
9-
//(msg: string | object, category?: string, level?: number): void;
10-
setOptions(opts: LogggerOptions): void;
9+
//(msg: string | object, category?: string, level?: number): void;
10+
setOptions(opts: LogggerOptions): void;
1111

12-
debug(msg: string): void;
13-
info(msg: string): void;
14-
warn(msg: string): void;
15-
error(msg: string): void;
12+
debug(msg: string): void;
13+
info(msg: string): void;
14+
warn(msg: string): void;
15+
error(msg: string): void;
1616
}
1717

1818
export declare var log: Loggger;
1919

2020

2121
/* @file ./src/safe.js */
2222
interface SafeOps {
23-
bit(value: number, bit: number): boolean;
23+
bit(value: number, bit: number): boolean;
2424

25-
get<TObject extends object, TKey extends keyof TObject>(object: TObject, path: TKey): TObject[TKey];
26-
set<TObject extends object, T>(object: TObject, path: string, value:T): T;
27-
unset<TObject extends object>(object: TObject, path: string): void;
25+
get<TObject extends object, TKey extends keyof TObject>(object: TObject, path: TKey): TObject[TKey];
26+
set<TObject extends object, T>(object: TObject, path: string, value: T): T;
27+
unset<TObject extends object>(object: TObject, path: string): void;
2828
}
2929
export declare var safe: SafeOps;
30+
31+
32+
/* @file ./src/traverse.js */
33+
interface Traverse<T> {
34+
each(pred: (node: T, parent: T) => any): void;
35+
find(pred: (node: T) => boolean): T;
36+
filter(pred: (node: T) => boolean): T[];
37+
}
38+
export declare function traverse<T>(tree: T & { children: T[] }): Traverse<T & { children: T[] }>;

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
module.exports = Object.assign({}, {
44
node: require('./src/node'),
55
safe: require('./src/safe'),
6+
traverse: require('./src/traverse'),
67
log: require('./src/log')
78
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rightech/utils",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "",
55
"main": "index.js",
66
"private": false,

src/traverse.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* name : Rightech IoT Cloud ~.
3+
* description : ~
4+
* author : Oleg Prohazko
5+
*
6+
* LLC, Komnet, Russian Federation, Moscow, 2016
7+
*
8+
*/
9+
10+
11+
function walk(child, callback, parent) {
12+
callback(child, parent);
13+
14+
parent = child;
15+
const children = child.children || [];
16+
for (const child of children) {
17+
walk(child, callback, parent);
18+
}
19+
}
20+
21+
function walkFind(tree, callback) {
22+
if (callback(tree)) {
23+
return tree;
24+
}
25+
26+
for (const child of tree.children || []) {
27+
const result = walkFind(child, callback);
28+
if (result) {
29+
return result;
30+
}
31+
}
32+
}
33+
34+
function walkFindWithStats(tree, callback, stats = {}) {
35+
if (!stats.level) {
36+
stats.level = 0;
37+
}
38+
if (!stats.path) {
39+
stats.path = [tree];
40+
}
41+
stats.node = tree;
42+
if (callback(tree)) {
43+
stats.level = stats.path.length - 1;
44+
stats.path.pop();
45+
return stats;
46+
}
47+
48+
for (const child of tree.children || []) {
49+
stats.path.push(child);
50+
const result = walkFindWithStats(child, callback, stats);
51+
if (result) {
52+
return result;
53+
}
54+
stats.path.pop();
55+
}
56+
}
57+
58+
function walkFilter(child, callback, result) {
59+
if (callback(child)) {
60+
result.push(child);
61+
}
62+
63+
const children = child.children || [];
64+
for (const child of children) {
65+
walkFilter(child, callback, result);
66+
}
67+
}
68+
69+
70+
module.exports = function traverse(tree) {
71+
return {
72+
each: callback => walk(tree, callback),
73+
find: callback => walkFind(tree, callback),
74+
findWithStats: callback => walkFindWithStats(tree, callback, {}),
75+
filter: callback => {
76+
const result = [];
77+
walkFilter(tree, callback, result);
78+
return result;
79+
}
80+
};
81+
};

0 commit comments

Comments
 (0)