import { ResourceRequestBodyType } from './Declarations' ; export class ResourceHelper { static cleanDataFields : string [ ] = [ '$resolved' , '$promise' , '$abort' , '$resource' ] ; private static isBrowser : boolean = null ; static isRunningInBrowser ( ) { if ( this . isBrowser !== null ) { return this . isBrowser ; } try { this . isBrowser = ! ! window ; } catch ( e ) { this . isBrowser = false ; } return this . isBrowser ; } static getRealTypeOf ( data ) { if ( ! data ) { return ResourceRequestBodyType . NONE ; } if ( this . isRunningInBrowser ( ) ) { if ( FormData && data instanceof FormData ) { return ResourceRequestBodyType . FORM_DATA ; } if ( Blob && data instanceof Blob ) { return ResourceRequestBodyType . BLOB ; } } if ( data instanceof ArrayBuffer ) { return ResourceRequestBodyType . ARRAY_BUFFER ; } if ( [ 'string' , 'number' ] . indexOf ( typeof data ) > - 1 ) { return ResourceRequestBodyType . TEXT ; } return ResourceRequestBodyType . JSON ; } static defaults ( dst , src ) { if ( ! dst ) { dst = { } ; } Object . keys ( src ) . forEach ( ( key ) => { if ( dst [ key ] === undefined ) { dst [ key ] = src [ key ] ; } } ) ; return dst ; } static isNullOrUndefined ( value ) { return value === null || value === undefined ; } static cleanData ( obj ) { if ( Array . isArray ( obj ) ) { return this . cleanDataArray ( obj ) ; } else { return this . cleanDataObject ( obj ) ; } } static cleanDataArray ( obj ) { for ( const propName in obj ) { if ( typeof obj [ propName ] === 'function' || this . cleanDataFields . indexOf ( propName ) > - 1 ) { delete obj [ propName ] ; } } return obj ; } static cleanDataObject ( obj ) { const cleanedObj = { } ; for ( const propName in obj ) { if ( typeof obj [ propName ] !== 'function' && this . cleanDataFields . indexOf ( propName ) === - 1 ) { cleanedObj [ propName ] = obj [ propName ] ; } } return cleanedObj ; } }