st24.js
1.69 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"use strict";
function extractFromMessage(msg, default_pattern, default_match_idx, custom_rule) {
if (!msg || typeof msg !== 'string') {
return;
}
let pattern;
let match_idx;
if (custom_rule && custom_rule.pattern) {
pattern = custom_rule.pattern;
match_idx = custom_rule.match_idx;
}
else {
pattern = default_pattern;
match_idx = default_match_idx;
}
const re = new RegExp(pattern);
const matches = msg.match(re);
if (!matches) return;
if (match_idx < matches.length) {
return matches[match_idx] || null;
} else {
return;
}
}
function extractSnFromMessage(msg, custom_rule) {
const default_pattern = "SN=(.*?);";
const default_match_idx = 1;
return extractFromMessage(msg, default_pattern, default_match_idx, custom_rule);
}
function extractPriceFromMsg(msg, custom_rule) {
const default_pattern = "\\d,HRG= *(?:RP)*\\.* *(.*?),ID=";
const default_match_idx = 1;
let price = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule);
price = (typeof price === 'string') ? Number(price.replace(/[^\d]/g, '')) : null;
return price;
}
function extractBalanceFromMsg(msg, custom_rule) {
const default_pattern = 'SAL= *(?:RP)*\\.* *([\\d\\.,]+)';
const default_match_idx = 1;
let balance = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule);
if (!balance || typeof balance !== 'string') {
return;
}
return Number(balance.replace(/[^\d]/g, ''));
}
exports.extractSnFromMessage = extractSnFromMessage;
exports.extractPriceFromMsg = extractPriceFromMsg;
exports.extractBalanceFromMsg = extractBalanceFromMsg;