Use efficient filters
This commit is contained in:
parent
07cb8491e2
commit
3f2ecfb0b6
13 changed files with 82 additions and 47 deletions
|
|
@ -2,17 +2,20 @@ import { makeRe } from "minimatch";
|
|||
import type { Logger } from "../tracing/logger";
|
||||
|
||||
export function globsToRegexes(globs: string[], logger: Logger): RegExp[] {
|
||||
return globs
|
||||
.map((pattern) => {
|
||||
const result = makeRe(pattern, {
|
||||
dot: true
|
||||
});
|
||||
if (result === false) {
|
||||
logger.warn(
|
||||
`Failed to parse ${pattern}' as a glob pattern, skipping it`
|
||||
);
|
||||
}
|
||||
return result;
|
||||
})
|
||||
.filter((pattern) => pattern !== false);
|
||||
return (
|
||||
globs
|
||||
.map((pattern) => {
|
||||
const result = makeRe(pattern, {
|
||||
dot: true
|
||||
});
|
||||
if (result === false) {
|
||||
logger.warn(
|
||||
`Failed to parse ${pattern}' as a glob pattern, skipping it`
|
||||
);
|
||||
}
|
||||
return result;
|
||||
})
|
||||
// eslint-disable-next-line no-restricted-syntax -- Filtering out false values, not removing a specific item
|
||||
.filter((pattern) => pattern !== false)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
17
frontend/sync-client/src/utils/remove-from-array.ts
Normal file
17
frontend/sync-client/src/utils/remove-from-array.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
* Efficiently removes a specific item from an array by modifying it in place.
|
||||
* This is more efficient than using `.filter(item => item !== toRemove)` as it avoids creating a new array
|
||||
*
|
||||
* @param array The array to modify
|
||||
* @param item The item to remove
|
||||
* @returns true if the item was found and removed, false otherwise
|
||||
*/
|
||||
export function removeFromArray<T>(array: T[], item: T): boolean {
|
||||
const index = array.indexOf(item);
|
||||
if (index !== -1) {
|
||||
// eslint-disable-next-line no-restricted-syntax -- This is the implementation of the helper itself
|
||||
array.splice(index, 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue