module Sfx { export class CollectionUtils { param collection param newCollection param keySelector param newKeySelector param create param update public static updateCollection < T , P > ( collection , newCollection , keySelector , newKeySelector , create , update , appendOnly = false ) { let oldCollectionMap = _ . keyBy ( collection , keySelector ) ; if ( ! appendOnly ) { let newCollectionMap = _ . keyBy ( newCollection , newKeySelector ) ; _ . remove ( collection , ( item ) => ! newCollectionMap [ keySelector ( item ) ] ) ; } _ . forEach ( newCollection , ( newItem ) => { let id = newKeySelector ( newItem ) ; let oldItem = oldCollectionMap [ id ] ; if ( oldItem ) { update ( oldItem , newItem ) ; } else if ( create ) { collection . push ( create ( oldItem , newItem ) ) ; } } ) ; } param collection param newCollection param keySelector param newKeySelector public static compareCollectionsByKeys < T , P > ( collection , newCollection , keySelector , newKeySelector ) { if ( collection . length !== newCollection . length ) { return false ; } let oldCollectionMap = _ . keyBy ( collection , keySelector ) ; return _ . every ( newCollection , item => { let id = newKeySelector ( item ) ; return ! ! oldCollectionMap [ id ] ; } ) ; } param collection param newCollection public static updateDataModelCollection < T > ( collection , newCollection , appendOnly = false ) { CollectionUtils . updateCollection ( collection , newCollection , item => item . uniqueId , item => item . uniqueId , ( item , newItem ) => newItem , ( item , newItem ) => item . update ( newItem . raw ) , appendOnly ) ; } } }