import { CreateCache , CacheStorage , Cache , CreateMap , MapItem , AppMap } from './type' ; export class ReqError extends Error { status ? : number ; constructor ( message ? , status ? ) { super ( message ) ; this . status = status ; } } export const createCache = < T > ( amount = 10 ) => { let cache = { } ; const keys = ( ) => { return Object . keys ( cache ) ; } ; const checkAmount = ( ) => { let cacheKeys = keys ( ) ; if ( cacheKeys . length > amount ) { remove ( cacheKeys [ 0 ] ) ; } } ; const set = ( key , value ) => { remove ( key ) ; cache [ key ] = value ; checkAmount ( ) ; } ; const get = ( key ) => { return < T > cache [ key ] ; } ; const remove = ( key ) => { if ( cache . hasOwnProperty ( key ) ) { delete cache [ key ] ; } } ; const getAll = ( ) => { return cache ; } ; return { keys , get , set , remove , getAll } as Cache < T > ; } ; export const createMap = < K , V > ( ) => { let list = [ ] ; const find = ( key ) => { return list . filter ( item => item . key === key ) ; } ; const has = ( key ) => { let result = find ( key ) ; return result . length > 0 ; } ; const get = ( key ) => { let result = find ( key ) ; return result . length ? result [ 0 ] . value : undefined ; } ; const set = ( key , value ) => { let result = find ( key ) ; if ( result . length === 0 ) { let item = { key , value } ; list . push ( item ) ; } else { result [ 0 ] . value = value ; } } ; const remove = ( key ) => { list = list . filter ( item => item . key !== key ) ; } ; return { get , set , has , remove } as AppMap < K , V > ; } ;