forked from matteogatti/sms-counter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (70 loc) · 2.18 KB
/
index.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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const SmsCounter = () => {};
SmsCounter.gsm7bitChars =
'@£$¥èéùìòÇ\\nØø\\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\\"#¤%&\'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà';
SmsCounter.gsm7bitExChar = '\\^{}\\\\\\[~\\]|€';
SmsCounter.gsm7bitRegExp = RegExp('^[' + SmsCounter.gsm7bitChars + ']*$');
SmsCounter.gsm7bitExRegExp = RegExp('^[' + SmsCounter.gsm7bitChars + SmsCounter.gsm7bitExChar + ']*$');
SmsCounter.gsm7bitExOnlyRegExp = RegExp('^[\\' + SmsCounter.gsm7bitExChar + ']*$');
SmsCounter.GSM_7BIT = 'GSM_7BIT';
SmsCounter.GSM_7BIT_EX = 'GSM_7BIT_EX';
SmsCounter.UTF16 = 'UTF16';
SmsCounter.messageLength = {
GSM_7BIT: 160,
GSM_7BIT_EX: 160,
UTF16: 70,
};
SmsCounter.multiMessageLength = {
GSM_7BIT: 153,
GSM_7BIT_EX: 153,
UTF16: 67,
};
SmsCounter.count = text => {
let encoding, length, messages, per_message, remaining;
encoding = SmsCounter.detectEncoding(text);
length = text.length;
if (encoding === SmsCounter.GSM_7BIT_EX) {
length += SmsCounter.countGsm7bitEx(text);
}
per_message = SmsCounter.messageLength[encoding];
if (length > per_message) {
per_message = SmsCounter.multiMessageLength[encoding];
}
messages = Math.ceil(length / per_message);
remaining = per_message * messages - length;
if (remaining === 0 && messages === 0) {
remaining = per_message;
}
return {
encoding: encoding,
length: length,
per_message: per_message,
remaining: remaining,
messages: messages,
};
};
SmsCounter.detectEncoding = text => {
switch (false) {
case text.match(SmsCounter.gsm7bitRegExp) == null:
return SmsCounter.GSM_7BIT;
case text.match(SmsCounter.gsm7bitExRegExp) == null:
return SmsCounter.GSM_7BIT_EX;
default:
return SmsCounter.UTF16;
}
};
SmsCounter.countGsm7bitEx = text => {
let char2, chars;
chars = () => {
let _i, _len, _results;
_results = [];
for (_i = 0, _len = text.length; _i < _len; _i++) {
char2 = text[_i];
if (char2.match(SmsCounter.gsm7bitExOnlyRegExp) != null) {
_results.push(char2);
}
}
return _results;
};
return chars.length;
};
module.exports = SmsCounter;