-
Notifications
You must be signed in to change notification settings - Fork 34
/
SegmentedMessage.ts
187 lines (160 loc) · 5.26 KB
/
SegmentedMessage.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import GraphemeSplitter from 'grapheme-splitter';
import Segment from './Segment';
import EncodedChar from './EncodedChar';
type SmsEncoding = 'GSM-7' | 'UCS-2';
type EncodedChars = Array<EncodedChar>;
const validEncodingValues = ['GSM-7', 'UCS-2', 'auto'];
/**
* Class representing a segmented SMS
*/
export class SegmentedMessage {
encoding: SmsEncoding | 'auto';
segments: Segment[];
graphemes: string[];
encodingName: SmsEncoding;
numberOfUnicodeScalars: number;
numberOfCharacters: number;
/**
*
* Create a new segmented message from a string
*
* @param {string} message Body of the message
* @param {boolean} [encoding] Optional: encoding. It can be 'GSM-7', 'UCS-2', 'auto'. Default value: 'auto'
* @property {number} numberOfUnicodeScalars Number of Unicode Scalars (i.e. unicode pairs) the message is made of
*
*/
constructor(message: string, encoding: SmsEncoding | 'auto' = 'auto') {
const splitter = new GraphemeSplitter();
if (!validEncodingValues.includes(encoding)) {
throw new Error(
`Encoding ${encoding} not supported. Valid values for encoding are ${validEncodingValues.join(', ')}`,
);
}
/**
* @property {string[]} graphemes Graphemes (array of strings) the message have been split into
*/
this.graphemes = splitter.splitGraphemes(message);
/**
* @property {number} numberOfUnicodeScalars Number of Unicode Scalars (i.e. unicode pairs) the message is made of
* Some characters (e.g. extended emoji) can be made of more than one unicode pair
*/
this.numberOfUnicodeScalars = [...message].length;
/**
* @property {number} numberOfCharacters Number of characters in the message. Each character count as 1, regardless of the encoding.
*/
this.numberOfCharacters = this.graphemes.length;
/**
* @property {string} encoding Encoding set in the constructor for the message. Allowed values: 'GSM-7', 'UCS-2', 'auto'.
* @private
*/
this.encoding = encoding;
if (this._hasAnyUCSCharacters(this.graphemes)) {
if (encoding === 'GSM-7') {
throw new Error('The string provided is incompatible with GSM-7 encoding');
}
/**
* @property {string} encodingName Calculated encoding name. It can be: "GSM-7" or "UCS-2"
*/
this.encodingName = 'UCS-2';
} else {
this.encodingName = 'GSM-7';
}
const encodedChars: EncodedChars = this._encodeChars(this.graphemes);
/**
* @property {object[]} segments Array of segment(s) the message have been segmented into
*/
this.segments = this._buildSegments(encodedChars);
}
/**
* Internal method to check if the message has any non-GSM7 characters
*
* @param {string[]} graphemes Message body
* @returns {boolean} True if there are non-GSM-7 characters
* @private
*/
_hasAnyUCSCharacters(graphemes: string[]): boolean {
let result = false;
for (const grapheme of graphemes) {
if (grapheme.length >= 2) {
result = true;
break;
}
}
return result;
}
/**
* Internal method used to build message's segment(s)
*
* @param {object[]} encodedChars Array of EncodedChar
* @returns {object[]} Array of Segment
* @private
*/
_buildSegments(encodedChars: EncodedChars): Segment[] {
const segments: Segment[] = [];
segments.push(new Segment());
let currentSegment = segments[0];
for (const encodedChar of encodedChars) {
if (currentSegment.freeSizeInBits() < encodedChar.sizeInBits()) {
segments.push(new Segment(true));
currentSegment = segments[segments.length - 1];
const previousSegment = segments[segments.length - 2];
if (!previousSegment.hasUserDataHeader) {
const removedChars = previousSegment.addHeader();
// eslint-disable-next-line no-loop-func
removedChars.forEach((char) => currentSegment.push(char));
}
}
currentSegment.push(encodedChar);
}
return segments;
}
/**
* Return the encoding of the message segment
*
* @returns {string} Encoding for the message segment(s)
*/
getEncodingName(): string {
return this.encodingName;
}
/**
* Internal method to create an array of EncodedChar from a string
*
* @param {string[]} graphemes Array of graphemes representing the message
* @returns {object[]} Array of EncodedChar
* @private
*/
_encodeChars(graphemes: string[]): EncodedChars {
const encodedChars: EncodedChars = [];
for (const grapheme of graphemes) {
encodedChars.push(new EncodedChar(grapheme, this.encodingName));
}
return encodedChars;
}
/**
* @return {number} Total size of the message in bits (including User Data Header if present)
*/
get totalSize(): number {
let size = 0;
for (const segment of this.segments) {
size += segment.sizeInBits();
}
return size;
}
/**
* @return {number} Total size of the message in bits (excluding User Data Header if present)
*/
get messageSize(): number {
let size = 0;
for (const segment of this.segments) {
size += segment.messageSizeInBits();
}
return size;
}
/**
*
* @return {number} Number of segments
*/
get segmentsCount(): number {
return this.segments.length;
}
}