import set from 'lodash.set' ; import Model from './' ; import Collection from '../collection' ; export const STORE_OPTION = 'store' ; const isStoreOption = ( option ) => option === STORE_OPTION ; const DATE_PATTERN = '____AS-Date____' ; export const splitPath = ( path ) => { return path . replace ( /^[\|]+|[\|]+$/g , "." ) . split ( '.' ) . filter ( function ( x ) { return ( x !== ( undefined || null || '' ) ) ; } ) ; } ; export const getValueAtPath = ( pathSplited , to ) : Model => { for ( let i = 0 ; i < pathSplited . length ; i ++ ) to = to . state [ pathSplited [ i ] ] ; return to ; } ; const updateInState = ( value , path , to ) => { const pathSplited = splitPath ( path ) ; const lastKey = pathSplited [ pathSplited . length - 1 ] ; pathSplited . splice ( pathSplited . length - 1 , 1 ) ; const v = getValueAtPath ( pathSplited , to ) ; if ( Model . IsModel ( v ) && v . is ( ) . collection ( ) ) { v . setState ( v . to ( ) . listClass ( value ) ) ; } else if ( Model . IsModel ( v ) && ! Model . IsCollection ( v . state [ lastKey ] ) ) { v . setState ( { [ lastKey ] : value } ) ; } else if ( v && ! ! v . state && Model . IsCollection ( v . state [ lastKey ] ) ) { const collec = v . state [ lastKey ] as Collection ; collec . setState ( collec . to ( ) . listClass ( value ) ) ; } else { if ( lastKey && v ) ( v as any ) [ lastKey ] = value ; } } ; export const hydrate = ( from , to ) => { const recur = ( o , path ) => { if ( Model . _isArray ( o ) ) updateInState ( o , path , to ) ; else if ( Model . _isObject ( o ) ) for ( let key in o ) recur ( o [ key ] , path + '.' + key ) ; else updateInState ( o , path , to ) ; } ; recur ( from , '' ) ; } ; export type TObjectStringAny = { [ char ] : any ; } ; export const toPlain = ( m , option ) : any => { const ret = { } ; const recur = ( o , path ) => { if ( Model . _isObject ( o ) && Object . keys ( o ) . length > 0 ) { for ( var key in o ) recur ( o [ key ] , ! path ? key : path + '.' + key ) ; return ; } if ( Model . _isArray ( o ) && o . length > 0 ) { for ( let i = 0 ; i < o . length ; i ++ ) recur ( o [ i ] , path + '.' + i . toString ( ) ) ; return ; } if ( Model . IsModel ( o ) ) { recur ( o . state , path ) ; return ; } if ( option && isStoreOption ( option ) && o instanceof Date ) { o = `${ DATE_PATTERN }${ o . getTime ( ) . toString ( ) }` ; } set ( ret , path , o ) ; } ; recur ( m , '' ) ; if ( Model . _isArray ( m ) ) { if ( ! ret [ '' ] ) return [ ] ; return ret [ '' ] ; } return ret ; } ; export const ParseJSONLocallyStored = ( data ) => { const ret = { } ; const recur = ( o , path ) => { if ( Model . _isObject ( o ) && Object . keys ( o ) . length > 0 ) { for ( var key in o ) recur ( o [ key ] , ! path ? key : path + '.' + key ) ; return ; } if ( Model . _isArray ( o ) && o . length > 0 ) { for ( let i = 0 ; i < o . length ; i ++ ) recur ( o [ i ] , path + '.' + i . toString ( ) ) ; return ; } if ( typeof o === 'string' && o . startsWith ( DATE_PATTERN ) ) { o = new Date ( parseInt ( o . replace ( DATE_PATTERN , '' ) ) ) ; } set ( ret , path , o ) ; } ; recur ( JSON . parse ( data ) , '' ) ; return ret ; } ;