import { Path } from '../type/index' ; export function toString ( object ) { return Object . prototype . toString . call ( object ) ; } export function isPlainObject ( object ) { return toString ( object ) === '[object Object]' ; } export function isArray ( object ) { return toString ( object ) === '[object Array]' ; } export function isFunction ( object ) { return typeof object === 'function' ; } export function isPromise ( object ) { return object !== null && typeof object === 'object' && isFunction ( object . then ) ; } export function hasKey ( object , key ) { return object . hasOwnProperty ( key ) ; } interface Cache < T > { object : T ; copy : T ; } export function deepCopy < T > ( object , cache = [ ] ) { if ( object === null || ( isArray ( object ) === false && isPlainObject ( object ) === false ) ) { return object ; } const cachedIndex = cache . findIndex ( item => item . object === object ) ; if ( cachedIndex !== - 1 ) { return cache [ cachedIndex ] . copy ; } const copy = ( isPlainObject ( object ) ? { } : [ ] ) as T ; cache . push ( { object , copy , } ) ; Object . keys ( object ) . forEach ( key => { ; ( copy as any ) [ key ] = deepCopy ( ( object as any ) [ key ] , cache ) ; } ) ; return copy as T ; } export function getByPath ( object , path ) { if ( path . length === 0 ) return object ; let deepestParent = object ; path . slice ( 0 , - 1 ) . forEach ( p => { deepestParent = deepestParent [ p ] ; } ) ; return deepestParent [ path [ path . length - 1 ] ] ; } export function setByPath ( object , path , value ) { if ( path . length === 0 ) return value ; const deepestParent = getByPath ( object , path . slice ( 0 , - 1 ) ) ; deepestParent [ path [ path . length - 1 ] ] = value ; return value ; } export function noop ( ... args ) { } export function identity < T > ( any ) { return any ; } type Fn = ( ... args : Args ) => any ; type Args = any [ ] ; export function curry ( fn , ... args1 ) { return fn . length === args1 . length ? fn ( ... args1 ) : ( ... args2 ) => curry ( fn , ... args1 , ... args2 ) ; }