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 | 8x 2x 4x 7x 3x 2x 1x 8x 2x | import { purry } from "./purry"
type PlainType = string | number | boolean | null | undefined
interface PlainObject {
readonly [key: string]: PlainType | PlainObject | ReadonlyArray<PlainType | PlainObject>
}
function isPlainObject(value: unknown): value is PlainObject {
return value != null && {}.toString.call(value) === '[object Object]'
}
function _compact<T>(a: T): T {
return Object.assign(
{},
...Object.entries(a)
.filter(([, v]) => v !== undefined && v !== null)
.map(([k, v]) => ({
[k]: Array.isArray(v)
? v
.filter((val) => val !== undefined && val !== null)
.map((val) => (isPlainObject(val) ? _compact(val) : val))
: isPlainObject(v)
? _compact(v)
: v,
}))
)
}
/**
* Removes all undefined and null values from object rreecursively
* @param data - the object to compact
* @signature P.compact(value)
* @example
* P.compact({foo: undefined}) // {}
* @category Object
*/
export function compact<T>(data: T): T
export function compact<T>(): (data: T) => T
export function compact() {
return purry(_compact, arguments)
} |