import { ICache , IQuery } from "." ; export const match = ( item , query ) => { const keys = Object . keys ( query ) ; for ( const key of keys ) { if ( typeof item [ key ] !== typeof query [ key ] ) { return false ; } if ( typeof item [ key ] === "object" ) { if ( ! match ( item [ key ] , query [ key ] ) ) { return false ; } } else { if ( item [ key ] !== query [ key ] ) { return false ; } } } return true ; } ; export class LocalCache < T > implements ICache < T > { public items : { [ key ] : T ; } ; constructor ( ) { this . items = { } ; } public async set ( key , data ) { return new Promise ( ( resolve ) => { this . items [ key ] = data ; resolve ( ) ; } ) ; } public async get ( key ) { return new Promise ( ( resolve ) => { resolve ( this . items [ key ] || null ) ; } ) ; } public async remove ( key ) { return new Promise ( ( resolve ) => { const index = Object . keys ( this . items ) . indexOf ( key ) ; if ( index < 0 ) { return ; } delete this . items [ key ] ; resolve ( ) ; } ) ; } public async findOne ( query ? ) { return new Promise ( ( resolve ) => { const items = Object . keys ( this . items ) . map ( ( key ) => this . items [ key ] ) ; resolve ( query ? items . find ( ( item ) => match ( item , query ) ) || null : items [ 0 ] ) ; } ) ; } public async findMany ( query ? ) { return new Promise ( ( resolve ) => { const items = Object . keys ( this . items ) . map ( ( key ) => this . items [ key ] ) ; resolve ( query ? items . filter ( ( item ) => match ( item , query ) ) : items ) ; } ) ; } }