diff --git a/index.js b/index.js index 7323861..11fdef2 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,14 @@ const flatfile = require('flat-file-db') const promisify = require('then-flat-file-db') +const escapeRegexp = require('escape-regex') const db = promisify(flatfile.sync(process.env.DB_NAME || 'views.db')) +const keyRegex = (str) => { + str = str.split('*').map( s => escapeRegexp(s)).join('*') + return new RegExp('^' + str.replace('*','.*')) +} + module.exports = { put: db.put.bind(db), has: (key) => Promise.resolve(db.has(key)), @@ -27,7 +33,9 @@ module.exports = { // Get all values starting with a certain pathname and filter their views getAll: async function getAll(options) { const data = {} - const keys = (await module.exports.keys()).filter(key => key.startsWith(options.pathname)) + const keys = (await module.exports.keys()).filter((key) => { + return options.wildcard ? key.match(keyRegex(options.pathname)) : key.startsWith(options.pathname) + }) for (let key of keys) { data[key] = await module.exports.get(key, { before: options.before, after: options.after }) diff --git a/package.json b/package.json index aadcca9..f706c7d 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ }, "homepage": "https://github.com/mxstbr/micro-analytics-adapter-flat-file-db#readme", "dependencies": { + "escape-regex": "^1.0.7", "flat-file-db": "^1.0.0", "then-flat-file-db": "^1.0.0" },