/** * Pick a list of properties from an object * into a new object */ export const pick = (obj: T, keys: TKeys[]): Pick => { if (!obj) return {} as Pick; return keys.reduce( (acc, key) => { if (Object.prototype.hasOwnProperty.call(obj, key)) acc[key] = obj[key]; return acc; }, {} as Pick ); }; /** * Removes (shakes out) undefined entries from an * object. Optional second argument shakes out values * by custom evaluation. */ export const shake = ( obj: T, filter: (value: unknown) => boolean = (x) => x === undefined || x === null ): Omit => { if (!obj) return {} as T; const keys = Object.keys(obj) as (keyof T)[]; return keys.reduce((acc, key) => { if (filter(obj[key])) { return acc; } acc[key] = obj[key]; return acc; }, {} as T); };