From 603f1580502605d6d13e9194e71a22c4fe9edc63 Mon Sep 17 00:00:00 2001 From: Sean Roberts Date: Sun, 15 Jan 2017 14:02:20 -0500 Subject: [PATCH 1/2] Support wildcard queries --- index.js | 10 +++++++++- package.json | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) 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" }, From 967bb03ea45503037d51b609870522276dd09069 Mon Sep 17 00:00:00 2001 From: Sean Roberts Date: Mon, 16 Jan 2017 12:06:15 -0500 Subject: [PATCH 2/2] Reorder logic for ignore wildcard to be opt-in --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 11fdef2..af8ae01 100644 --- a/index.js +++ b/index.js @@ -34,7 +34,7 @@ module.exports = { getAll: async function getAll(options) { const data = {} const keys = (await module.exports.keys()).filter((key) => { - return options.wildcard ? key.match(keyRegex(options.pathname)) : key.startsWith(options.pathname) + return options.ignoreWildcard ? key.startsWith(options.pathname) : key.match(keyRegex(options.pathname)) }) for (let key of keys) {