|
1 | | -const path = require('path') |
2 | | -const flatfile = require('flat-file-db') |
3 | | -const promisify = require('then-flat-file-db') |
4 | | -const escapeRegexp = require('escape-regex') |
5 | | -const Observable = require("zen-observable") |
| 1 | +const path = require('path'); |
| 2 | +const flatfile = require('flat-file-db'); |
| 3 | +const promisify = require('then-flat-file-db'); |
| 4 | +const escapeRegexp = require('escape-regex'); |
| 5 | +const Observable = require('zen-observable'); |
6 | 6 |
|
7 | | -let db |
| 7 | +let db; |
8 | 8 |
|
9 | 9 | function init(options) { |
10 | | - db = promisify(flatfile.sync(path.resolve(process.cwd(), options.dbName || 'views.db'))) |
| 10 | + db = promisify( |
| 11 | + flatfile.sync(path.resolve(process.cwd(), options.dbName || 'views.db')) |
| 12 | + ); |
11 | 13 | } |
12 | 14 |
|
13 | 15 | // This is here for backwards compatability should be removed at some point |
14 | | -init({dbName: process.env.DB_NAME}) |
15 | | - |
16 | | -const keyRegex = (str) => { |
17 | | - str = str.split('*').map( s => escapeRegexp(s)).join('*') |
18 | | - return new RegExp('^' + str.replace('*','.*')) |
19 | | -} |
| 16 | +init({ dbName: process.env.DB_NAME }); |
20 | 17 |
|
| 18 | +const keyRegex = str => { |
| 19 | + str = str.split('*').map(s => escapeRegexp(s)).join('*'); |
| 20 | + return new RegExp('^' + str.replace('*', '.*')); |
| 21 | +}; |
21 | 22 |
|
22 | 23 | let handlers = []; |
23 | 24 |
|
24 | | -const observable = new Observable((observer) => { |
25 | | - handlers.push((data) => observer.next(data)) |
| 25 | +const observable = new Observable(observer => { |
| 26 | + handlers.push(data => observer.next(data)); |
26 | 27 | let index = handlers.length; |
27 | 28 | return () => { |
28 | | - handlers = [...handlers.slice(0, index), ...handlers.slice(index)] |
29 | | - } |
| 29 | + handlers = [...handlers.slice(0, index), ...handlers.slice(index)]; |
| 30 | + }; |
30 | 31 | }); |
31 | 32 |
|
32 | 33 | module.exports = { |
33 | 34 | options: [ |
34 | 35 | { |
35 | 36 | name: 'db-name', |
36 | 37 | description: 'The name of the flat-file-db file.', |
37 | | - defaultValue: process.env.DB_NAME || 'views.db' |
38 | | - } |
| 38 | + defaultValue: process.env.DB_NAME || 'views.db', |
| 39 | + }, |
39 | 40 | ], |
40 | 41 | init, |
41 | 42 | put: (key, value) => { |
42 | 43 | handlers.forEach(handler => { |
43 | | - handler({key, value}); |
44 | | - }) |
45 | | - return db.put(key, value) |
| 44 | + handler({ key, value }); |
| 45 | + }); |
| 46 | + return db.put(key, value); |
46 | 47 | }, |
47 | | - has: (key) => Promise.resolve(db.has(key)), |
48 | | - keys: () => Promise.resolve(db.keys()), |
49 | | - // Get a value and filter it |
| 48 | + has: key => Promise.resolve(db.has(key)), |
| 49 | + keys: () => Promise.resolve(db.keys()), |
| 50 | + // Get a value and filter it |
50 | 51 | get: async (key, options) => { |
51 | | - let value |
52 | | - try { |
53 | | - value = await db.get(key) |
54 | | - } catch (err) { |
55 | | - value = { views: [] } |
56 | | - } |
| 52 | + let value; |
| 53 | + try { |
| 54 | + value = await db.get(key); |
| 55 | + } catch (err) { |
| 56 | + value = { views: [] }; |
| 57 | + } |
57 | 58 |
|
58 | 59 | return { |
59 | 60 | views: value.views.filter(view => { |
60 | | - if (options && options.before && view.time > options.before) return false |
61 | | - if (options && options.after && view.time < options.after) return false |
62 | | - return true |
63 | | - }) |
64 | | - } |
| 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 | + }), |
| 66 | + }; |
65 | 67 | }, |
66 | | - // Get all values starting with a certain pathname and filter their views |
| 68 | + // Get all values starting with a certain pathname and filter their views |
67 | 69 | getAll: async function getAll(options) { |
68 | | - const data = {} |
69 | | - const keys = (await module.exports.keys()).filter((key) => { |
70 | | - return options.ignoreWildcard ? key.startsWith(options.pathname) : key.match(keyRegex(options.pathname)) |
71 | | - }) |
| 70 | + 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 | + }); |
72 | 76 |
|
73 | | - for (let key of keys) { |
74 | | - data[key] = await module.exports.get(key, { before: options.before, after: options.after }) |
75 | | - } |
| 77 | + for (let key of keys) { |
| 78 | + data[key] = await module.exports.get(key, { |
| 79 | + before: options.before, |
| 80 | + after: options.after, |
| 81 | + }); |
| 82 | + } |
76 | 83 |
|
77 | | - await Promise.all(keys) |
| 84 | + await Promise.all(keys); |
78 | 85 |
|
79 | | - return data |
| 86 | + return data; |
80 | 87 | }, |
81 | | - subscribe: (cb) => { |
| 88 | + subscribe: cb => { |
82 | 89 | return observable.subscribe(cb); |
83 | | - } |
84 | | -} |
| 90 | + }, |
| 91 | +}; |
0 commit comments