import IDictionary from './interfaces/IDictionary' ; import * as JsonApi from './interfaces/JsonApi' ; param { object } obj param { Function } fn export function objectForEach ( obj , fn ) { for ( const key in obj ) { if ( obj . hasOwnProperty ( key ) ) { fn ( key ) ; } } } export template T param { ( object | Array < object > ) } data param { Function } fn returns { ( T | Array < T > ) } export function mapItems < T > ( data , fn ) { return data instanceof Array ? data . map ( ( item ) => fn ( item ) ) : fn ( data ) ; } export param { IJsonApiRecord } record returns { IDictionary < any > } export function flattenRecord ( record ) { const data = { __internal : { id : record . id , type : record . type , } , } ; objectForEach ( record . attributes , ( key ) => { data [ key ] = record . attributes [ key ] ; } ) ; objectForEach ( record . relationships , ( key ) => { const rel = record . relationships [ key ] ; if ( rel . meta ) { data [ `${ key }Meta` ] = rel . meta ; } if ( rel . links ) { data . __internal . relationships = data . __internal . relationships || { } ; data . __internal . relationships [ key ] = rel . links ; } } ) ; objectForEach ( record . links , ( key ) => { if ( record . links [ key ] ) { data . __internal . links = data . __internal . links || { } ; data . __internal . links [ key ] = record . links [ key ] ; } } ) ; objectForEach ( record . meta , ( key ) => { if ( record . meta [ key ] !== undefined ) { data . __internal . meta = data . __internal . meta || { } ; data . __internal . meta [ key ] = record . meta [ key ] ; } } ) ; return data ; } export const isBrowser = ( typeof window !== 'undefined' ) ; private param { object } target param { Array < object > } args returns export function assign ( target , ... args ) { args . forEach ( ( nextSource ) => { if ( nextSource != null ) { for ( const nextKey in nextSource ) { if ( Object . prototype . hasOwnProperty . call ( nextSource , nextKey ) ) { target [ nextKey ] = nextSource [ nextKey ] ; } } } } ) ; return target ; } export template T param { ( T | ( ( ) => T ) ) } target returns { T } export function getValue < T > ( target ) { if ( typeof target === 'function' ) { return target ( ) ; } return target ; } export param { object } obj returns { Array < string > } export function keys ( obj ) { const keyList = [ ] ; for ( const key in obj ) { if ( obj . hasOwnProperty ( key ) ) { keyList . push ( key ) ; } } return keyList ; }