-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.js
33 lines (26 loc) · 906 Bytes
/
database.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
const Database = require('better-sqlite3');
const emojiRegex = require('emoji-regex');
const XRegExp = require('xregexp');
const punycode = require('punycode');
const getPunyCode = txt => {
try {
const punyCode = punycode.toUnicode(txt);
return punyCode !== txt && punyCode;
} catch (e) {
return false;
}
};
const databaseRegex = database => {
const emojiExpression = `(${emojiRegex().source})`;
XRegExp.addToken(/\\m/, () => emojiExpression, {
scope: 'default',
});
database.function('REGEXP', { deterministic: true }, function (regex, str) {
const r = XRegExp(decodeURIComponent(regex), 'i');
const puny = getPunyCode(str);
return r.test(str) || (puny && r.test(puny)) ? 1 : 0;
});
};
const database = new Database('database.db', { verbose: console.log });
databaseRegex(database);
module.exports = database;