Skip to content

Commit ff5703f

Browse files
committed
fix: 优化removeItem和isPathMatched
1 parent 61da93b commit ff5703f

File tree

2 files changed

+79
-37
lines changed

2 files changed

+79
-37
lines changed
Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,67 @@
1-
21
/**
3-
*
2+
*
43
* 判断path是否与pattern匹配
5-
*
4+
*
65
* isPathMatched("a.b.c","a.b.c") == true
76
* isPathMatched("a.b.c","a.b.*") == true
87
* isPathMatched("a.b.c","a.*.*") == true
98
* isPathMatched("a.b.c","*.*.*") == true
109
* isPathMatched("a.b.c",".b.*") == true
1110
* isPathMatched("a.b.c.d","a.**") == true
12-
*
11+
*
1312
* - '**' 匹配后续的
1413
* - '*' 匹配任意数量的字符,包括零个字符
15-
*
16-
* @param path
17-
* @param pattern
14+
*
15+
* @param path
16+
* @param pattern
1817
*/
19-
export function isPathMatched(path:string[],pattern:string[]):boolean{
20-
if(path.length !== pattern.length && (path.length>0 && pattern[pattern.length-1]!=='**') ){
18+
export function isPathMatched(path: string[], pattern: string[]): boolean {
19+
const pathLen = path.length;
20+
const patternLen = pattern.length;
21+
22+
// 快速失败: 长度不匹配(除非以 ** 结尾)
23+
if (pathLen !== patternLen && (patternLen === 0 || pattern[patternLen - 1] !== '**')) {
2124
return false;
2225
}
23-
let fPattern = [...pattern]
24-
if(pattern.length >0 && pattern[pattern.length-1] === '**'){
25-
fPattern.splice(pattern.length-1,1,...Array.from<string>({
26-
length: path.length-pattern.length+1
27-
}).fill('*'))
28-
}
29-
for(let i=0;i<path.length;i++){
30-
if(fPattern[i]==='*'){
31-
continue
26+
27+
// 处理 ** 通配符: 直接检查前缀匹配
28+
if (patternLen > 0 && pattern[patternLen - 1] === '**') {
29+
// 检查前 n-1 个元素是否匹配
30+
for (let i = 0; i < patternLen - 1; i++) {
31+
if (pattern[i] !== '*' && pattern[i] !== path[i]) {
32+
return false;
33+
}
3234
}
33-
if(fPattern[i]!==path[i]){
34-
return false
35+
return true;
36+
}
37+
38+
// 普通匹配: 逐元素比较
39+
for (let i = 0; i < pathLen; i++) {
40+
if (pattern[i] !== '*' && pattern[i] !== path[i]) {
41+
return false;
3542
}
3643
}
37-
return true
44+
45+
return true;
3846
}
3947

40-
48+
// export function isPathMatched(path:string[],pattern:string[]):boolean{
49+
// if(path.length !== pattern.length && (path.length>0 && pattern[pattern.length-1]!=='**') ){
50+
// return false;
51+
// }
52+
// let fPattern = [...pattern]
53+
// if(pattern.length >0 && pattern[pattern.length-1] === '**'){
54+
// fPattern.splice(pattern.length-1,1,...Array.from<string>({
55+
// length: path.length-pattern.length+1
56+
// }).fill('*'))
57+
// }
58+
// for(let i=0;i<path.length;i++){
59+
// if(fPattern[i]==='*'){
60+
// continue
61+
// }
62+
// if(fPattern[i]!==path[i]){
63+
// return false
64+
// }
65+
// }
66+
// return true
67+
// }
Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1-
2-
3-
export function removeItem(arr:any[],condition:(item:any)=>boolean){
4-
let index:number[] = []
5-
while (true) {
6-
const i = arr.findIndex((item)=>{
7-
return condition(item)
8-
})
9-
if(i === -1) {
10-
index.push(i)
11-
break
12-
}
13-
arr.splice(i,1)
1+
/**
2+
* 从数组中移除满足条件的元素并返回被移除元素的索引
3+
* @param {any[]} arr - 需要操作的数组
4+
* @param {(item: any) => boolean} condition - 判断元素是否应该被移除的条件函数
5+
* @returns {number[]} 被移除元素在原数组中的索引集合
6+
*/
7+
export function removeItem(arr: any[], condition: (item: any) => boolean) {
8+
const removedIndices: number[] = [];
9+
// 从后往前遍历,避免删除元素时索引变化的问题
10+
for (let i = arr.length - 1; i >= 0; i--) {
11+
if (condition(arr[i])) {
12+
removedIndices.push(i); // 记录原始索引
13+
arr.splice(i, 1); // 删除元素
14+
}
1415
}
15-
return index
16-
}
16+
17+
// 因为是从后往前遍历的,所以索引是降序的,需要反转得到升序结果
18+
return removedIndices.reverse();
19+
}
20+
// let index: number[] = [];
21+
// while (true) {
22+
// const i = arr.findIndex((item) => {
23+
// return condition(item);
24+
// });
25+
// if (i === -1) {
26+
// index.push(i);
27+
// break;
28+
// }
29+
// arr.splice(i, 1);
30+
// }
31+
// return index;

0 commit comments

Comments
 (0)