Commit aa95b090816555c5f40e23eb9018f01480dc40ea

Authored by Adhidarma Hadiwinoto
1 parent 00208dea14
Exists in master

pattern-rules optimized

Showing 2 changed files with 7 additions and 5 deletions Inline Diff

lib/message-extractor.js
1 /* eslint-disable no-console */ 1 /* eslint-disable no-console */
2 const MODULE_NAME = 'MESSAGE-PARSER.FINDER'; 2 const MODULE_NAME = 'MESSAGE-PARSER.FINDER';
3 3
4 /** 4 /**
5 * Mencari nilai berdasarkan array of pattern 5 * Mencari nilai berdasarkan array of pattern
6 * @date 2021-07-05 6 * @date 2021-07-05
7 * @param {Array} rules 7 * @param {Array} rules
8 * @param {string} originalMessage 8 * @param {string} originalMessage
9 * @param {any} isDebug 9 * @param {any} isDebug
10 * @returns {string} string yang sesuai pattern, null jika tidak ditemukan 10 * @returns {string} string yang sesuai pattern, null jika tidak ditemukan
11 */ 11 */
12 module.exports = (rules, originalMessage, isDebug) => { 12 module.exports = (rules, originalMessage, isDebug) => {
13 if (!Array.isArray(rules)) return null; 13 if (!Array.isArray(rules)) return null;
14 if (typeof originalMessage !== 'string') return null; 14 if (typeof originalMessage !== 'string') return null;
15 15
16 const msg = originalMessage.trim(); 16 const msg = originalMessage.trim();
17 17
18 if (!rules || !msg) return null; 18 if (!rules || !msg) return null;
19 19
20 const ruleCount = rules.length; 20 const ruleCount = rules.length;
21 for (let i = 0; i < ruleCount; i += 1) { 21 for (let i = 0; i < ruleCount; i += 1) {
22 const rule = rules[i]; 22 const rule = rules[i];
23 23
24 // eslint-disable-next-line no-continue 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 const matches = msg.match(re); 31 const matches = msg.match(re);
30 let result = matches && matches[rule.idx]; 32 let result = matches && matches[rule.idx];
31 33
32 if (result && rule.postprocessing && typeof rule.postprocessing === 'function') { 34 if (result && rule.postprocessing && typeof rule.postprocessing === 'function') {
33 result = rule.postprocessing(result); 35 result = rule.postprocessing(result);
34 } 36 }
35 37
36 if (result) { 38 if (result) {
37 if (isDebug) { 39 if (isDebug) {
38 console.log(`${MODULE_NAME} 9FAF9286: Found matches`, { isDebug, result, rule }); 40 console.log(`${MODULE_NAME} 9FAF9286: Found matches`, { isDebug, result, rule });
39 } 41 }
40 42
41 return result; 43 return result;
42 } 44 }
43 } 45 }
44 46
45 if (isDebug) { 47 if (isDebug) {
46 console.log(`${MODULE_NAME} 0240860D: Matches not found`, { isDebug }); 48 console.log(`${MODULE_NAME} 0240860D: Matches not found`, { isDebug });
47 } 49 }
48 50
49 return null; 51 return null;
50 }; 52 };
51 53
lib/pattern-rules.js
1 /** 1 /**
2 * Rule pattern to be used by message extractor 2 * Rule pattern to be used by message extractor
3 */ 3 */
4 module.exports = { 4 module.exports = {
5 sn: [ 5 sn: [
6 { 6 {
7 pattern: '(?:^|,|\\.|;|\\s)SN\\s*[=: ]\\s*(.+?)(?:;|$)', 7 pattern: /(?:^|,|\.|;|\s)SN\s*[=: ]\s*(.+?)(?:;|$)/,
8 idx: 1, 8 idx: 1,
9 disable: false, 9 disable: false,
10 }, 10 },
11 ], 11 ],
12 price: [ 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 idx: 1, 15 idx: 1,
16 disable: false, 16 disable: false,
17 postprocessing: (val) => (val || '').replace(/[,.]/g, ''), 17 postprocessing: (val) => (val || '').replace(/[,.]/g, ''),
18 }, 18 },
19 ], 19 ],
20 balance: [ 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 idx: 1, 23 idx: 1,
24 disable: false, 24 disable: false,
25 postprocessing: (val) => (val || '').replace(/[,.]/g, ''), 25 postprocessing: (val) => (val || '').replace(/[,.]/g, ''),
26 }, 26 },
27 ], 27 ],
28 }; 28 };
29 29