Skip to content

Commit 7ed2d7a

Browse files
committed
fix: change fuzzySearchTree to pure function
1 parent 48dbfd5 commit 7ed2d7a

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "sculp-js",
33
"version": "1.11.1-alpha.3",
4-
"packageManager": "npm@8.19.2",
54
"description": "js utils library, includes function library、class library",
65
"scripts": {
76
"prepare": "husky install",
@@ -38,8 +37,8 @@
3837
],
3938
"keywords": [
4039
"sculp-js",
41-
"js-utils",
42-
"typescript"
40+
"es6",
41+
"util"
4342
],
4443
"engines": {
4544
"node": ">=16"

src/tree.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ export function fuzzySearchTree<V>(
299299
}
300300
const result: V[] = [];
301301

302-
arrayEach(nodes, node => {
302+
for (let i = 0, len = nodes.length; i < len; i++) {
303+
const node = nodes[i];
303304
// 递归检查子节点是否匹配
304305
const matchedChildren =
305306
node[options.childField] && node[options.childField].length > 0
@@ -323,24 +324,20 @@ export function fuzzySearchTree<V>(
323324
[options.childField]: matchedChildren // 包含匹配的子节点
324325
});
325326
} else if (options.removeEmptyChild) {
326-
node[options.childField] && delete node[options.childField];
327-
result.push({
328-
...node
329-
});
327+
const { [options.childField]: _, ...other } = node as any;
328+
result.push(other);
330329
} else {
331330
result.push({
332331
...node,
333-
...{ [options.childField]: [] }
332+
[options.childField]: []
334333
});
335334
}
336335
} else {
337-
node[options.childField] && delete node[options.childField];
338-
339-
result.push({
340-
...node
341-
});
336+
const { [options.childField]: _, ...other } = node as any;
337+
result.push(other);
342338
}
343339
}
344-
});
340+
}
341+
345342
return result;
346343
}

test/tree.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ test('mapDeep', () => {
532532
]);
533533
});
534534

535-
describe('fuzzySearchTree', () => {
535+
describe('fuzzySearchTree: keywords', () => {
536536
test('search by keyword', () => {
537537
// 定义树结构
538538
const tree = [
@@ -566,6 +566,7 @@ describe('fuzzySearchTree', () => {
566566
]
567567
}
568568
];
569+
const clone = cloneDeep(tree);
569570

570571
// 测试keyword 1
571572
const query = 'apr';
@@ -677,9 +678,10 @@ describe('fuzzySearchTree', () => {
677678
]
678679
}
679680
]);
681+
expect(tree).toEqual(clone);
680682
});
681683

682-
test('search by filter function, include not ignore case', () => {
684+
test('fuzzySearchTree: search by filter function, include not ignore case', () => {
683685
const res1 = [];
684686
const res2 = [];
685687
// 定义树结构
@@ -697,7 +699,7 @@ describe('fuzzySearchTree', () => {
697699
{
698700
id: 3,
699701
name: 'Anpricot',
700-
children: [{ id: 5, name: 'butterfly' }]
702+
children: [{ id: 35, name: 'butterfly' }]
701703
},
702704
{
703705
id: 4,
@@ -731,6 +733,7 @@ describe('fuzzySearchTree', () => {
731733
]
732734
}
733735
];
736+
const clone = cloneDeep(tree);
734737
const query = 'An';
735738
// keyword模式匹配,不忽略大小写
736739
const result1 = fuzzySearchTree(
@@ -821,5 +824,7 @@ describe('fuzzySearchTree', () => {
821824
// keyword匹配模式,且keyword为空串
822825
const result4 = fuzzySearchTree(tree, { keyword: '' });
823826
expect(result4).toBe(tree);
827+
828+
expect(tree).toEqual(clone);
824829
});
825830
});

0 commit comments

Comments
 (0)