type AnyMapOrEmpty = { [ key ] : any ; } | { } ; export interface AnyMap { [ key ] : any ; } export function asMap ( value ) ; export function asMap ( value , defaultValue ) ; export function asMap ( value , defaultValue ? ) { return typeof value === 'object' && value !== null ? ( value as AnyMap ) : defaultValue ; } export function asArray < T = any > ( value ) ; export function asArray < T = any > ( value , defaultValue ) ; export function asArray < T = any > ( value , defaultValue ? ) { return Array . isArray ( value ) ? ( value as T [ ] ) : defaultValue ; } export function asNumber ( value ) ; export function asNumber ( value , defaultValue ) ; export function asNumber ( value , defaultValue ? ) { if ( typeof value === 'number' ) { return ! isNaN ( value ) ? value : defaultValue ; } if ( typeof value === 'string' ) { try { const parsedValue = parseFloat ( value ) ; return ! isNaN ( parsedValue ) ? parsedValue : defaultValue ; } catch ( e ) { return defaultValue ; } } return defaultValue ; } export function asBoolean ( value ) ; export function asBoolean ( value , defaultValue ) ; export function asBoolean ( value , defaultValue ? ) { if ( typeof value === 'boolean' ) { return value ; } if ( typeof value === 'number' ) { return ! isNaN ( value ) ? value !== 0 : false ; } if ( typeof value === 'string' ) { if ( value === '0' || value === 'false' ) { return false ; } else if ( value === '1' || value === 'true' ) { return true ; } else { return defaultValue ; } } return defaultValue ; } export function asString ( value ) ; export function asString ( value , defaultValue ) ; export function asString ( value , defaultValue ? ) { return typeof value === 'string' ? value : defaultValue ; } export function asNumberString ( value ) ; export function asNumberString ( value , defaultValue ) ; export function asNumberString ( value , defaultValue ? ) { return typeof value === 'string' ? value : typeof value === 'number' ? String ( value ) : defaultValue ; } export function asFunction ( value ) ; export function asFunction ( value , defaultValue ) ; export function asFunction ( value , defaultValue ? ) { return typeof value === 'function' ? ( value as Function ) : defaultValue ; } export function asDate ( value ) ; export function asDate ( value , defaultValue ) ; export function asDate ( value , defaultValue ? ) { if ( typeof value === 'number' ) { return new Date ( value ) ; } return value instanceof Date ? value : defaultValue ; } export function asStringMap ( value ) { const map = asMap ( value ) ; if ( map === undefined ) { return undefined ; } const res = new Map < string , string > ( ) ; Object . keys ( map ) . forEach ( ( key ) => res . set ( key , asString ( map [ key ] , '' ) ) ) ; return res ; } export function errorAsString ( err ) { let errorString = asString ( err ) ; if ( errorString === undefined ) { if ( typeof err === 'object' && err !== null ) { errorString = ( err as object ) . toString ( ) ; } errorString = errorString === undefined ? String ( err ) : errorString ; } return errorString ; } export function asParsedJSON < T > ( json ) { try { return JSON . parse ( json ) as T ; } catch { const defaultValue = { } ; return defaultValue as T ; } } export function notUndefined < T > ( val ) { return val !== undefined ; } export function removeUndefined < T > ( arr ) { return arr . filter ( notUndefined ) ; } export type Omit < T , K extends keyof T > = Pick < T , Exclude < keyof T , K > > ; export type Brand < Type , Name > = Type & { __brand : Name ; } ; export interface FlatObject { [ key ] : string | number | boolean | undefined ; } export type Untrusted < T > = { [ P in keyof T ] : any ; } ; export function hasOwnProperty < X extends { } , Y extends PropertyKey > ( obj , prop ) { return obj . hasOwnProperty ( prop ) ; }