Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/compat/array/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
* If a function is provided, it is invoked for each element in the collection.
*
* @template T
* @param source - The array or object to iterate over.
* @param collection - The array or object to iterate over.
* @param [predicate=identity] - The function invoked per iteration.
* @returns Returns a new array of filtered elements that satisfy the predicate.
*
Expand All @@ -110,25 +110,25 @@
* // => [{ a: 2 }]
*/
export function filter<T>(
source: ArrayLike<T> | Record<any, any> | null | undefined,
collection: ArrayLike<T> | Record<any, any> | null | undefined,

Check warning on line 113 in src/compat/array/filter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 113 in src/compat/array/filter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
predicate: ((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | PropertyKey = identity

Check warning on line 114 in src/compat/array/filter.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
): T[] {
if (!source) {
if (!collection) {
return [];
}

predicate = iteratee(predicate);

if (!Array.isArray(source)) {
if (!Array.isArray(collection)) {
const result: T[] = [];
const keys = Object.keys(source) as Array<keyof T>;
const length = isArrayLike(source) ? source.length : keys.length;
const keys = Object.keys(collection) as Array<keyof T>;
const length = isArrayLike(collection) ? collection.length : keys.length;

for (let i = 0; i < length; i++) {
const key = keys[i];
const value = source[key] as T;
const value = collection[key] as T;

if (predicate(value, key as number, source)) {
if (predicate(value, key as number, collection)) {
result.push(value);
}
}
Expand All @@ -137,11 +137,11 @@
}

const result: T[] = [];
const length = source.length;
const length = collection.length;

for (let i = 0; i < length; i++) {
const value = source[i];
if (predicate(value, i, source)) {
const value = collection[i];
if (predicate(value, i, collection)) {
result.push(value);
}
}
Expand Down