import { PropsWithType } from './types' ; param array returns public export function isArrayEmpty ( array ) { return array == null || array . length === 0 ; } param array returns example public export function arrayToObject ( array ) ; param array param key returns public export function arrayToObject < ArrayType , KeyType extends string | number > ( array , key ) ; param array param key returns public export function arrayToObject < ArrayType , KeyType extends string | number > ( array , key ? ) { return array . reduce < Record < string , ArrayType > > ( ( accumulator , current ) => { if ( key ) { accumulator [ current [ key ] as unknown as string ] = current ; } else if ( typeof current === 'string' ) { accumulator [ current ] = current ; } return accumulator ; } , { } ) ; } param array param groupBy returns public export function groupItemsBy < ArrayType , ReturnType extends string | number > ( array , groupBy ) { return array . reduce < Record < ReturnType , ArrayType [ ] > > ( ( accumulator , current , index ) => { const keyValue = groupBy ( current , index ) ; if ( ! accumulator [ keyValue ] ) { accumulator [ keyValue ] = [ ] ; } accumulator [ keyValue ] . push ( current ) ; return accumulator ; } , { } as Record < ReturnType , ArrayType [ ] > ) ; } param array param filter param childrenKey example returns public export function deepFilter < ArrayType , Key extends PropsWithType < ArrayType , ArrayType [ ] > > ( array , filter , childrenKey ) { const filterArray = function ( currentArray , initialArray = [ ] ) : ArrayType [ ] { return currentArray . reduce ( ( result , currentItem ) => { if ( filter ( currentItem ) ) { result . push ( currentItem ) ; filterArray ( currentItem [ childrenKey ] , result ) ; } return result ; } , initialArray ) ; } ; return filterArray ( array ) ; } param array param childrenKey returns public export function deepFlat < ArrayType , Key extends PropsWithType < ArrayType , ArrayType [ ] | undefined > > ( array , childrenKey ) { param resultantArray param currentItem returns internal function flatArray ( resultantArray , currentItem ) { if ( ! currentItem ) { return resultantArray ; } if ( Array . isArray ( currentItem ) ) { return currentItem . reduce ( flatArray , resultantArray ) ; } resultantArray . push ( currentItem ) ; return flatArray ( resultantArray , currentItem [ childrenKey ] ) ; } return flatArray ( [ ] , array ) ; } param comparator returns internal export function createEmitterArrayFilter < T > ( comparator ) { const comparatorFn = typeof comparator === 'function' ? comparator : ( newItem , oldItem ) => newItem [ comparator ] === oldItem [ comparator ] ; return function ( newCollection , oldCollection ) : boolean { return ( newCollection . length !== oldCollection . length || newCollection . some ( newItem => ! oldCollection . find ( oldItem => comparatorFn ( newItem , oldItem ) ) ) ) ; } ; }