Commit aa95b090816555c5f40e23eb9018f01480dc40ea
1 parent
00208dea14
Exists in
master
pattern-rules optimized
Showing 2 changed files with 7 additions and 5 deletions Side-by-side Diff
lib/message-extractor.js
... | ... | @@ -22,9 +22,11 @@ module.exports = (rules, originalMessage, isDebug) => { |
22 | 22 | const rule = rules[i]; |
23 | 23 | |
24 | 24 | // eslint-disable-next-line no-continue |
25 | - if (rule.disable || rule.disabled) continue; | |
25 | + if (!rule || !rule.pattern || rule.disable || rule.disabled) continue; | |
26 | 26 | |
27 | - const re = new RegExp(rule.pattern, rule.flags || ''); | |
27 | + const patternConstructorName = rule.pattern.constructor.name; | |
28 | + const re = patternConstructorName === 'RegExp' ? rule.pattern | |
29 | + : new RegExp(rule.pattern, rule.flags || ''); | |
28 | 30 | |
29 | 31 | const matches = msg.match(re); |
30 | 32 | let result = matches && matches[rule.idx]; |
lib/pattern-rules.js
... | ... | @@ -4,14 +4,14 @@ |
4 | 4 | module.exports = { |
5 | 5 | sn: [ |
6 | 6 | { |
7 | - pattern: '(?:^|,|\\.|;|\\s)SN\\s*[=: ]\\s*(.+?)(?:;|$)', | |
7 | + pattern: /(?:^|,|\.|;|\s)SN\s*[=: ]\s*(.+?)(?:;|$)/, | |
8 | 8 | idx: 1, |
9 | 9 | disable: false, |
10 | 10 | }, |
11 | 11 | ], |
12 | 12 | price: [ |
13 | 13 | { |
14 | - pattern: '(?:,|\\.|;|\\s)(?:HRG|HARGA)\\s*[=: ]\\s*(?:RP)*\\s*(([0-9]|\\.||,)+)', | |
14 | + pattern: /(?:,|\.|;|\s)(?:HRG|HARGA)\s*[=: ]\s*(?:(?:RP|Rp)\.*)*\s*(([0-9]|\.||,)+)/, | |
15 | 15 | idx: 1, |
16 | 16 | disable: false, |
17 | 17 | postprocessing: (val) => (val || '').replace(/[,.]/g, ''), |
... | ... | @@ -19,7 +19,7 @@ module.exports = { |
19 | 19 | ], |
20 | 20 | balance: [ |
21 | 21 | { |
22 | - pattern: '(?:,|\\.|;|\\s)SAL(?:DO)*\\s*[=: ]\\s*(?:RP)*\\s*(([0-9]|\\.||,)+)', | |
22 | + pattern: /(?:,|\.|;|\s)SAL(?:DO)*\s*[=: ]\s*(?:(?:RP|Rp)\.*)*\s*(([0-9]|\.||,)+)/, | |
23 | 23 | idx: 1, |
24 | 24 | disable: false, |
25 | 25 | postprocessing: (val) => (val || '').replace(/[,.]/g, ''), |