11const path = require ( 'path' ) ;
22const flatfile = require ( 'flat-file-db' ) ;
33const promisify = require ( 'then-flat-file-db' ) ;
4- const escapeRegexp = require ( 'escape-regex' ) ;
54const Observable = require ( 'zen-observable' ) ;
5+ const { filterPaths, filterViews } = require ( 'micro-analytics-adapter-utils' ) ;
66
77let db ;
88
@@ -51,28 +51,19 @@ module.exports = {
5151 get : async ( key , options ) => {
5252 let value ;
5353 try {
54- value = await db . get ( key ) ;
54+ value = ( await db . get ( key ) ) || { } ;
5555 } catch ( err ) {
5656 value = { views : [ ] } ;
5757 }
5858
5959 return {
60- views : value . views . filter ( view => {
61- if ( options && options . before && view . time > options . before )
62- return false ;
63- if ( options && options . after && view . time < options . after ) return false ;
64- return true ;
65- } ) ,
60+ views : filterViews ( value . views , options ) ,
6661 } ;
6762 } ,
6863 // Get all values starting with a certain pathname and filter their views
6964 getAll : async function getAll ( options ) {
7065 const data = { } ;
71- const keys = ( await module . exports . keys ( ) ) . filter ( key => {
72- return options . ignoreWildcard
73- ? key . startsWith ( options . pathname )
74- : key . match ( keyRegex ( options . pathname ) ) ;
75- } ) ;
66+ const keys = filterPaths ( await module . exports . keys ( ) , options ) ;
7667
7768 for ( let key of keys ) {
7869 data [ key ] = await module . exports . get ( key , {
@@ -88,4 +79,7 @@ module.exports = {
8879 subscribe : cb => {
8980 return observable . subscribe ( cb ) ;
9081 } ,
82+ clear : ( ) => {
83+ db . clear ( ) ;
84+ } ,
9185} ;
0 commit comments