// #region Functions /** * Compact an array _(removing all false-y values)_ * * @param array Array to compact * @param strict True to remove all false-y values * @returns Compacted array * * @example * ```typescript * compact([0, 1, '', 'hello', false, true, null, undefined]); // => [1, 'hello', true] * ``` */ export function compact( array: Item[], strict: true, ): Exclude[]; /** * Compact an array _(removing all `null` and `undefined` values)_ * * @param array Array to compact * @returns Compacted array * * @example * ```typescript * compact([0, 1, '', 'hello', false, true, null, undefined]); // => [0, 1, '', 'hello', false, true] * ``` */ export function compact(array: Item[]): Exclude[]; export function compact(array: Item[], strict?: unknown): Item[] { if (!Array.isArray(array)) { return []; } if (strict === true) { return array.filter(Boolean); } const {length} = array; const compacted: Item[] = []; for (let index = 0; index < length; index += 1) { const item = array[index]; if (item != null) { compacted.push(item); } } return compacted; } // #endregion