]> git.r.bdr.sh - rbdr/corona-regeln/blame - lib/rule_generator.js
Capitalize businesses
[rbdr/corona-regeln] / lib / rule_generator.js
CommitLineData
920e58a2 1const { readdir } = require('fs');
07d8db2b
RBR
2const { join, resolve } = require('path');
3
4const internals = {
5
6 dates: [
7 'today',
8 'tomorrow',
9 'next friday',
10 'next wednesday',
11 'last thursday',
12 'last tuesday',
13 'the next day divisible by 7',
14 'this afternoon',
15 'next fiscal quarter',
16 ],
17
18 rules: null,
19 currentIndex: 0,
20
f331d8dc 21 kRulesDirectory: resolve(__dirname, './rules'),
07d8db2b
RBR
22
23 async readRules() {
24
920e58a2 25 const ruleFiles = await internals.readdir(internals.kRulesDirectory);
07d8db2b
RBR
26 const rules = ruleFiles
27 .filter((file) => file.match(/.js$/))
28 .map((file) => require(join(internals.kRulesDirectory, file)));
29
30 internals.rules = rules.sort(() => Math.random() - 0.5)
920e58a2
RBR
31 },
32
33 readdir(path) {
34 return new Promise((resolve, reject) => {
35
36 readdir(path, (err, files) => {
37
38 if (err) {
39 return reject(err);
40 }
41
42 return resolve(files);
43 })
44 });
07d8db2b
RBR
45 }
46}
47
48module.exports = {
49 async generate() {
50
51 if (!internals.rules) {
52 await internals.readRules();
53 }
54
55 const rule = internals.rules[internals.currentIndex];
56 const time = internals.dates[Math.floor(Math.random() * internals.dates.length)];
57 internals.currentIndex = (internals.currentIndex + 1) % internals.rules.length
58 return {
59 rule: rule(),
60 time
61 }
62 }
63}