-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupdate-regexes.js
42 lines (38 loc) · 1.16 KB
/
update-regexes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const Applications = require('../src/data/Applications');
const types = ['BOTS', 'BROWSERS', 'OTHERS'];
/**
* Write to file the regex for the specified type
* @param {string} type
*/
function updateRegexes(type) {
console.log(`Updating ${type} regexs`);
let ids = [];
if (type === 'BOTS') {
ids = getSet(Applications[type]);
} else {
Object.keys(Applications[type]).forEach((subtype) => {
ids = new Set([...ids, ...getSet(Applications[type][subtype])]);
});
}
const regex = new RegExp(`(${[...ids].join('|')})`, 'i').toString();
fs.writeFileSync(
path.join(__dirname, `../data/regexes/applications-${type.toLowerCase()}.js`),
'/* This file is automatically generated, do not edit manually! */\n\n' +
'/* eslint-disable */\n\n' +
`exports.${type}_REGEX = ${regex};`
);
console.log(`${type} regexes contains ${regex.split('|').length + 1} entries\n`);
}
/**
* @param {array} list
* @return {Set}
*/
function getSet(list) {
const set = new Set();
list.forEach(({ id }) => set.add(id));
return set;
}
types.forEach(updateRegexes);