import { AbstractControl } from '@angular/forms' ; import { ParamDefType , ResolveParamsOption } from './types' ; import { QueryParamDef } from './QueryParamDef' ; function isEmptyValue ( def , value ) { switch ( def . type ) { case 'array' : return ! value || ! value . length ; case 'object' : return ! value || ! Object . keys ( value ) . length ; case 'string' : return ! value || value === '' ; default : return false ; } } export function parse ( value , type ) { switch ( type ) { case 'string' : return value ; case 'boolean' : return value === 'false' ? false : ! ! value ; case 'array' : return value ?. split ( ',' ) ; case 'number' : return + value ; case 'object' : return JSON . parse ( value ) ; default : return value ; } } export function get ( obj , path ) { let current = obj ; path . split ( '.' ) . forEach ( ( p ) => ( current = current [ p ] ) ) ; return current ; } export function resolveParams ( params ) { const toArray = coerceArray ( params ) ; const result = { } ; toArray . forEach ( ( { def , value } ) => { let defValue ; if ( def . removeEmptyValue ) { defValue = isEmptyValue ( def , value ) ? null : def . serialize ( value ) ; } else { defValue = def . serialize ( value ) ; } result [ def . queryKey ] = defValue ; } ) ; return result ; } export function coerceArray < T > ( value ) { return Array . isArray ( value ) ? value : [ value ] ; } export function defsToParams ( defs , group ) { return defs . map ( ( def ) => { return { queryKey : def . queryKey , value : group . get ( def . path ) ! . value , } ; } ) ; }