-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsms_encoding.js
More file actions
68 lines (55 loc) · 2.05 KB
/
sms_encoding.js
File metadata and controls
68 lines (55 loc) · 2.05 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
65
66
67
68
const GSM_7BIT_CHARS = "@£$¥èéùìòÇ\\nØø\\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\\\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà";
const GSM_7BIT_EX_CHARS = "\\^{}\\\\\\[~\\]|€";
const GSM_7BIT_RE = RegExp("^[" + GSM_7BIT_CHARS + "]*$");
const GSM_7BIT_EX_RE = RegExp("^[" + GSM_7BIT_CHARS + GSM_7BIT_EX_CHARS + "]*$");
const GSM_7BIT_EX_ONLY_RE = RegExp("^[\\" + GSM_7BIT_EX_CHARS + "]*$");
const GSM_7BIT = 'GSM_7BIT';
const GSM_7BIT_EX = 'GSM_7BIT_EX';
const UCS_2 = 'UCS_2';
const MESSAGE_LENGTH = {
GSM_7BIT: 160,
GSM_7BIT_EX: 160,
UCS_2: 70,
};
const MULTI_MESSAGE_LENGTH = {
// We use 154 characters, due to the implementation of the software for sending messages. But the standard has 153 characters.
GSM_7BIT: 154,
GSM_7BIT_EX: 154,
UCS_2: 67,
};
const detectEncoding = function (text) {
if (!!text.match(GSM_7BIT_RE)) {
return GSM_7BIT
} else if (!!text.match(GSM_7BIT_EX_RE)) {
return GSM_7BIT_EX
} else {
return UCS_2
}
};
const lengthGSM7BitEx = function(text) {
let gsm7BitChars = 0;
let gsm7BitExChars = 0;
for (const i in text) {
const char = text.charAt(i);
if (!!char.match(GSM_7BIT_EX_ONLY_RE)) {
gsm7BitExChars += 1
} else {
gsm7BitChars += 1
}
}
return gsm7BitChars + (gsm7BitExChars * 2)
};
const countParts = function (text, encoding, length) {
let parts = 1;
const isMultipart = length > MESSAGE_LENGTH[encoding];
if (!isMultipart) return parts;
const perMessageLength = MULTI_MESSAGE_LENGTH[encoding];
return Math.ceil(length / perMessageLength);
};
const encodingParser = function (text) {
const encoding = detectEncoding(text);
const charsCount = encoding === GSM_7BIT_EX ? lengthGSM7BitEx(text) : text.length;
const partsCount = countParts(text, encoding, charsCount);
return { encoding, charsCount, partsCount };
};
export { encodingParser, GSM_7BIT, GSM_7BIT_EX, UCS_2, MESSAGE_LENGTH, MULTI_MESSAGE_LENGTH };