import { isArrayLike , isDefined } from './inspect' ; import { _ } from './constants' ; export interface IList < T > { [ key ] : T ; length : number ; } export function includes < T > ( items , item ) { return getIndex ( items , item ) !== - 1 ; } export function getIndex < T > ( items , item ) { return items . indexOf ( item ) ; } /**
 * Returns the first object in the list or undefined
 */ export function find < T > ( indexed , predicate ? , reverse ? ) { const ilen = indexed && indexed . length ; if ( ! ilen ) { return _ ; } if ( predicate === _ ) { return indexed [ reverse ? ilen - 1 : 0 ] ; } if ( reverse ) { for ( let i = ilen - 1 ; i > - 1 ; i -- ) { if ( predicate ( indexed [ i ] ) ) { return indexed [ i ] ; } } } else { for ( let i = 0 ; i < ilen ; i ++ ) { if ( predicate ( indexed [ i ] ) ) { return indexed [ i ] ; } } } return _ ; } /**
 * Returns the index of the first matching item or -1
 */ export function indexOf < T > ( items , predicate ) { for ( var i = 0 , ilen = items . length ; i < ilen ; i ++ ) { var item = items [ i ] ; if ( predicate ( item ) ) { return i ; } } return - 1 ; } export function remove < T > ( items , item ) { const index = items . indexOf ( item ) ; return index !== - 1 ? items . splice ( index , 1 ) : _ ; } export function sortBy < T > ( fieldName ) { return ( a , b ) => { const a1 = a [ fieldName ] ; const b1 = b [ fieldName ] ; return a1 < b1 ? - 1 : a1 > b1 ? 1 : 0 ; } ; } export template T param { ( IList < T > | T ) } indexed returns { T [ ] } export function list < T > ( indexed ) { return ! isDefined ( indexed ) ? [ ] : isArrayLike ( indexed ) ? ( indexed as T [ ] ) : [ indexed as T ] ; } param indexed param item export function push < T > ( indexed , item ) { if ( item !== _ ) { Array . prototype . push . call ( indexed , item ) ; } return item ; } export function pushDistinct < T > ( indexed , item ) { if ( ! includes ( indexed , item ) ) { push ( indexed , item ) ; } return item ; } export function mapFlatten < TInput , TOutput > ( items , mapper ) { var results = [ ] ; all ( items , item => { var result = mapper ( item ) ; if ( isArrayLike ( result ) ) { all ( result as TOutput [ ] , item2 => push ( results , item2 ) ) ; } else { push ( results , result as TOutput ) ; } } ) ; return results ; } export type Action < T1 > = ( input : T1 , index : number , len : number ) => void ; param items param action param reverse export function all < T1 > ( items , action ) { const items2 = list ( items ) ; for ( let i = 0 , ilen = items2 . length ; i < ilen ; i ++ ) { action ( items2 [ i ] , i , ilen ) ; } }