2017-09-27 09:33:20 -07:00
|
|
|
/**
|
|
|
|
* Checks if a type is an object
|
|
|
|
* @param {any} candidate the entity to check
|
|
|
|
*/
|
|
|
|
const isObject = (candidate: any): candidate is object =>
|
|
|
|
candidate && Object.prototype.toString.call(candidate) === '[object Object]';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that an object has it own enumerable props
|
2017-12-11 13:13:34 -08:00
|
|
|
* @param {any} object the object to the be tested
|
2017-09-27 09:33:20 -07:00
|
|
|
* @return {boolean} true if enumerable keys are present
|
|
|
|
*/
|
2017-12-11 13:13:34 -08:00
|
|
|
const hasEnumerableKeys = (object: any): boolean => isObject(object) && !!Object.keys(object).length;
|
2017-09-27 09:33:20 -07:00
|
|
|
|
2018-02-26 01:30:43 -08:00
|
|
|
/**
|
|
|
|
* Function interface for a identity or partial return function
|
|
|
|
* @interface IPartialOrIdentityTypeFn
|
|
|
|
* @template T
|
|
|
|
*/
|
|
|
|
interface IPartialOrIdentityTypeFn<T> {
|
|
|
|
(o: T): Partial<T> | T;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Non mutative object attribute deletion. Removes the specified keys from a copy of the object and returns the copy.
|
|
|
|
* @template T the object type to drop keys from
|
|
|
|
* @template K the keys to be dropped from the object
|
|
|
|
* @param {Array<K>} [droppedKeys=[]] the list of attributes on T to be dropped
|
|
|
|
* @returns {IPartialOrIdentityTypeFn<T>}
|
|
|
|
*/
|
2018-07-30 11:20:00 -07:00
|
|
|
const fleece = <T, K extends keyof T = keyof T>(droppedKeys: Array<K> = []): IPartialOrIdentityTypeFn<T> => (
|
|
|
|
o: T
|
|
|
|
): Partial<T> | T => {
|
2018-02-26 01:30:43 -08:00
|
|
|
const partialResult = Object.assign({}, o);
|
|
|
|
|
|
|
|
return droppedKeys.reduce((partial, key) => {
|
|
|
|
delete partial[key];
|
|
|
|
return partial;
|
|
|
|
}, partialResult);
|
|
|
|
};
|
|
|
|
|
|
|
|
export { isObject, hasEnumerableKeys, fleece };
|