import type { IStringifier , IParser } from '../lib/jsonCache.types' ; export enum TYPE { OBJECT = '0' , STRING = '1' , NUMBER = '2' , BOOLEAN = '3' , FUNCTION = '4' , UNDEFINED = '5' , SYMBOL = '6' } type Primitives = 'object' | 'string' | 'number' | 'boolean' | 'function' | 'undefined' | 'symbol' ; /**
 * Returns true if the constructor name is known
 * to us. Ex: Object, Array
 */ const isKnownContructor = ( ( ) => { const knownConstructors = { Object : true , Array : true , } ; return ( constructorName ) : boolean => knownConstructors [ constructorName ] ; } ) ( ) ; /**
 * Returns true if the given value's
 * type need to be skipped during storage.
 * For ex: Symbol -> Since symbols are private,
 * we DO NOT encourage them to be stored, hence
 * we are skipping from storing the same.
 *
 * In case you've forked this library and want to
 * add more type, then this is the place for you 🙂
 */ export const isSkippedType = ( ( ) => { const skippedType = { symbol : true , } ; return ( val ) : boolean => ! ! skippedType [ typeof val ] ; } ) ( ) ; param val export const getTypeOf = ( ( ) => { const shortTypes = { object : TYPE . OBJECT , string : TYPE . STRING , number : TYPE . NUMBER , boolean : TYPE . BOOLEAN , function : TYPE . FUNCTION , undefined : TYPE . UNDEFINED , symbol : TYPE . SYMBOL , } ; return ( val ) : ( TYPE | string ) => { if ( typeof val === 'object' ) { if ( ! val ) { return TYPE . OBJECT ; } const constructorName = val . constructor . name ; return isKnownContructor ( constructorName ) ? TYPE . OBJECT : constructorName ; } return shortTypes [ typeof val ] || TYPE . STRING ; } ; } ) ( ) ; param val param stringifier returns export const getValueOf = ( val , stringifier = { } ) : string => { if ( typeof val === 'object' ) { if ( ! val ) { return 'null' ; } const constructorName = val ?. constructor ?. name ; return isKnownContructor ( constructorName ) ? JSON . stringify ( val ) : stringifier [ constructorName ] ? stringifier [ constructorName ] ( val ) : String ( val ) ; } return String ( val ) ; } ; /**
 * Converts the given value to the specified type.
 *   Also note that, if a custom className type is
 * detected, then the provided custom Parser will
 * be called (if any available), else will return
 * the value as is.
 */ export const getTypedVal = ( ( ) => { const internalParsers = { [ TYPE . STRING ] : val => val , [ TYPE . NUMBER ] : Number , [ TYPE . BOOLEAN ] : ( val ) => val === 'true' , [ TYPE . FUNCTION ] : val => val , [ TYPE . UNDEFINED ] : ( ) => undefined , [ TYPE . SYMBOL ] : ( val ) => val , [ TYPE . OBJECT ] : ( ( ) => { const valMap = { '{}' : ( ) => ( { } ) , '[]' : ( ) => [ ] , 'null' : ( ) => null , } ; return ( val ) => valMap [ val ] ( ) ; } ) ( ) , } ; return ( type , val , parser = { } ) : any => { return internalParsers [ type ] ? internalParsers [ type ] ( val ) : parser [ type ] ? parser [ type ] ( val ) : val ; } ; } ) ( ) ;