Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 29x 27x 27x 27x 72x 72x 44x 72x 52x 27x 44x 42x 42x 42x 152x 94x 58x 42x 17x | import type { Defined, Truthy } from "./is-to-as";
import { isTruthy } from "./is-to-as";
/**
* Lazy filterMap that avoids creating a new array when possible.
*
* @returns the original array if `callbackfn` returns the same value for all items, otherwise a new array with the mapped items.
*
* @param arr
* @param callbackfn Return `undefined` to filter out the item, or return a value to map the item.
* @param thisArg
*/
export function inertFilterMap<T, U = T>(
arr: T[],
callbackfn: (value: T, index: number, arr: T[]) => U | undefined,
thisArg?: any
): Defined<U>[];
export function inertFilterMap<T, U = T>(
arr: T[] | undefined,
callbackfn: (value: T, index: number, arr: T[]) => U | undefined,
thisArg?: any
): Defined<U>[] | undefined;
export function inertFilterMap<T, U = T>(
arr: readonly T[],
callbackfn: (value: T, index: number, arr: readonly T[]) => U | undefined,
thisArg?: any
): readonly Defined<U>[];
export function inertFilterMap<T, U = T>(
arr: readonly T[] | undefined,
callbackfn: (value: T, index: number, arr: readonly T[]) => U | undefined,
thisArg?: any
): readonly Defined<U>[] | undefined;
export function inertFilterMap<T, U = T>(
arr: readonly T[] | undefined,
callbackfn: (value: T, index: number, arr: T[]) => U | undefined,
thisArg?: any
): readonly Defined<U>[] | undefined {
if (arr) {
let result: Defined<U>[] | undefined;
let index = -1;
let length = arr.length;
while (++index < length) {
const newItem = callbackfn.call(thisArg, arr[index], index, arr as T[]);
if (newItem === undefined || !Object.is(newItem, arr[index])) {
result ??= arr.slice(0, index) as Defined<U>[];
}
if (newItem !== undefined) {
result?.push(newItem as Defined<U>);
}
}
return result || (arr as readonly Defined<U>[]);
}
}
/**
* Lazy filter that avoids creating a new array when possible.
*
* @returns the original array if `predicate` returns `true` for all items, otherwise a new array with the filtered items.
*
* @param arr
* @param predicate Return `false` to filter out the item, or `true` to keep it.
* @param thisArg
*/
export function inertFilter<T, U extends T>(
arr: T[],
predicate: (value: T, index: number, arr: T[]) => value is U,
thisArg?: any
): U[];
export function inertFilter<T, U extends T>(
arr: T[] | undefined,
predicate: (value: T, index: number, arr: T[]) => value is U,
thisArg?: any
): U[] | undefined;
export function inertFilter<T, U extends T>(
arr: readonly T[],
predicate: (value: T, index: number, arr: readonly T[]) => value is U,
thisArg?: any
): readonly U[];
export function inertFilter<T, U extends T>(
arr: readonly T[] | undefined,
predicate: (value: T, index: number, arr: readonly T[]) => value is U,
thisArg?: any
): readonly U[] | undefined;
export function inertFilter<T>(arr: T[], predicate: (value: T, index: number, arr: T[]) => boolean, thisArg?: any): T[];
export function inertFilter<T>(
arr: T[] | undefined,
predicate: (value: T, index: number, arr: T[]) => boolean,
thisArg?: any
): T[] | undefined;
export function inertFilter<T>(
arr: readonly T[],
predicate: (value: T, index: number, arr: readonly T[]) => boolean,
thisArg?: any
): readonly T[];
export function inertFilter<T>(
arr: readonly T[] | undefined,
predicate: (value: T, index: number, arr: readonly T[]) => boolean,
thisArg?: any
): readonly T[] | undefined;
export function inertFilter<T>(
arr: readonly T[] | undefined,
predicate: (value: T, index: number, arr: T[]) => boolean,
thisArg?: any
): readonly T[] | undefined {
if (arr) {
let result: T[] | undefined;
let index = -1;
let length = arr.length;
while (++index < length) {
if (predicate.call(thisArg, arr[index], index, arr as T[])) {
result?.push(arr[index]);
} else {
result ??= arr.slice(0, index);
}
}
return result || arr;
}
}
export interface Coalesce {
<T>(arr: readonly T[]): readonly Truthy<T>[];
<T>(arr: T[]): Truthy<T>[];
}
/**
* @returns the same array if all its items are truthy, otherwise a new array with all falsy items removed.
*/
export const coalesce: Coalesce = arr => inertFilter(arr, isTruthy) as unknown[];
|