diff --git a/README.md b/README.md index 96b6437..250eda9 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,10 @@ Total size of the message in bits (excluding User Data Header if present) Number of segment(s) +### [`getNonGsmCharacters()`] + +Return an array with the non GSM-7 characters in the body. It can be used to replace character and reduce the number of segments + ## Try the library If you want to test the library you can use the script provided in `playground/index.js`. Install the dependencies (`npm install`) and then run: diff --git a/dist/libs/GSM7EncodedChar.js b/dist/libs/GSM7EncodedChar.js deleted file mode 100644 index c89be27..0000000 --- a/dist/libs/GSM7EncodedChar.js +++ /dev/null @@ -1,50 +0,0 @@ -"use strict"; -var __extends = (this && this.__extends) || (function () { - var extendStatics = function (d, b) { - extendStatics = Object.setPrototypeOf || - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || - function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; - return extendStatics(d, b); - }; - return function (d, b) { - if (typeof b !== "function" && b !== null) - throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); - extendStatics(d, b); - function __() { this.constructor = d; } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; -})(); -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -var EncodedChar_1 = __importDefault(require("./EncodedChar")); -var UnicodeToGSM_1 = __importDefault(require("./UnicodeToGSM")); -/* - * Represent a GSM-7 encoded character - * GSM-7 is of variable length and requires 1 or 2 code units per character - * a GSM-7 code unit is a septet (7bits) - */ -var GSM7EncodedChar = /** @class */ (function (_super) { - __extends(GSM7EncodedChar, _super); - function GSM7EncodedChar(char) { - var _this = _super.call(this, char, 'GSM-7') || this; - _this.graphemeSize = 1; - if (char.length === 1) { - _this.codeUnits = UnicodeToGSM_1.default[char.charCodeAt(0)]; - } - return _this; - } - GSM7EncodedChar.codeUnitSizeInBits = function () { - return 7; // GSM-7 code units are 7bits long - }; - GSM7EncodedChar.prototype.sizeInBits = function () { - if (this.codeUnits) { - return this.codeUnits.length * 7; // GSM-7 can be composed of 1 or 2 code units - } - return 0; // Some characters do not exist in GSM-7 thus making their length 0 - }; - return GSM7EncodedChar; -}(EncodedChar_1.default)); -exports.default = GSM7EncodedChar; -//# sourceMappingURL=GSM7EncodedChar.js.map \ No newline at end of file diff --git a/dist/libs/GSM7EncodedChar.js.map b/dist/libs/GSM7EncodedChar.js.map deleted file mode 100644 index 95248bf..0000000 --- a/dist/libs/GSM7EncodedChar.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"GSM7EncodedChar.js","sourceRoot":"","sources":["../../src/libs/GSM7EncodedChar.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,8DAAwC;AACxC,gEAA0C;AAE1C;;;;GAIG;AACH;IAA8B,mCAAW;IAGvC,yBAAY,IAAY;QAAxB,YACE,kBAAM,IAAI,EAAE,OAAO,CAAC,SAKrB;QAJC,KAAI,CAAC,YAAY,GAAG,CAAC,CAAC;QACtB,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;YACrB,KAAI,CAAC,SAAS,GAAG,sBAAY,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;SACnD;;IACH,CAAC;IAEM,kCAAkB,GAAzB;QACE,OAAO,CAAC,CAAC,CAAC,kCAAkC;IAC9C,CAAC;IAED,oCAAU,GAAV;QACE,IAAI,IAAI,CAAC,SAAS,EAAE;YAClB,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,6CAA6C;SAChF;QACD,OAAO,CAAC,CAAC,CAAC,mEAAmE;IAC/E,CAAC;IACH,sBAAC;AAAD,CAAC,AArBD,CAA8B,qBAAW,GAqBxC;AAED,kBAAe,eAAe,CAAC"} \ No newline at end of file diff --git a/dist/libs/SegmentedMessage.d.ts b/dist/libs/SegmentedMessage.d.ts index 294a742..a046005 100644 --- a/dist/libs/SegmentedMessage.d.ts +++ b/dist/libs/SegmentedMessage.d.ts @@ -12,6 +12,7 @@ export declare class SegmentedMessage { encodingName: SmsEncoding; numberOfUnicodeScalars: number; numberOfCharacters: number; + encodedChars: EncodedChars; /** * * Create a new segmented message from a string @@ -53,17 +54,22 @@ export declare class SegmentedMessage { */ _encodeChars(graphemes: string[]): EncodedChars; /** - * @return {number} Total size of the message in bits (including User Data Header if present) + * @returns {number} Total size of the message in bits (including User Data Header if present) */ get totalSize(): number; /** - * @return {number} Total size of the message in bits (excluding User Data Header if present) + * @returns {number} Total size of the message in bits (excluding User Data Header if present) */ get messageSize(): number; /** * - * @return {number} Number of segments + * @returns {number} Number of segments */ get segmentsCount(): number; + /** + * + * @returns {string[]} Array of characters representing the non GSM-7 characters in the message body + */ + getNonGsmCharacters(): string[]; } export {}; diff --git a/dist/libs/SegmentedMessage.js b/dist/libs/SegmentedMessage.js index 4e8e9a7..55fd6f0 100644 --- a/dist/libs/SegmentedMessage.js +++ b/dist/libs/SegmentedMessage.js @@ -90,11 +90,14 @@ var SegmentedMessage = /** @class */ (function () { else { this.encodingName = 'GSM-7'; } - var encodedChars = this._encodeChars(this.graphemes); + /** + * @property {string[]} encodedChars Array of encoded characters + */ + this.encodedChars = this._encodeChars(this.graphemes); /** * @property {object[]} segments Array of segment(s) the message have been segmented into */ - this.segments = this._buildSegments(encodedChars); + this.segments = this._buildSegments(this.encodedChars); } /** * Internal method to check if the message has any non-GSM7 characters @@ -196,7 +199,7 @@ var SegmentedMessage = /** @class */ (function () { }; Object.defineProperty(SegmentedMessage.prototype, "totalSize", { /** - * @return {number} Total size of the message in bits (including User Data Header if present) + * @returns {number} Total size of the message in bits (including User Data Header if present) */ get: function () { var e_4, _a; @@ -221,7 +224,7 @@ var SegmentedMessage = /** @class */ (function () { }); Object.defineProperty(SegmentedMessage.prototype, "messageSize", { /** - * @return {number} Total size of the message in bits (excluding User Data Header if present) + * @returns {number} Total size of the message in bits (excluding User Data Header if present) */ get: function () { var e_5, _a; @@ -247,7 +250,7 @@ var SegmentedMessage = /** @class */ (function () { Object.defineProperty(SegmentedMessage.prototype, "segmentsCount", { /** * - * @return {number} Number of segments + * @returns {number} Number of segments */ get: function () { return this.segments.length; @@ -255,6 +258,13 @@ var SegmentedMessage = /** @class */ (function () { enumerable: false, configurable: true }); + /** + * + * @returns {string[]} Array of characters representing the non GSM-7 characters in the message body + */ + SegmentedMessage.prototype.getNonGsmCharacters = function () { + return this.encodedChars.filter(function (encodedChar) { return !encodedChar.isGSM7; }).map(function (encodedChar) { return encodedChar.raw; }); + }; return SegmentedMessage; }()); exports.SegmentedMessage = SegmentedMessage; diff --git a/dist/libs/SegmentedMessage.js.map b/dist/libs/SegmentedMessage.js.map index b967a1d..bf26f1a 100644 --- a/dist/libs/SegmentedMessage.js.map +++ b/dist/libs/SegmentedMessage.js.map @@ -1 +1 @@ -{"version":3,"file":"SegmentedMessage.js","sourceRoot":"","sources":["../../src/libs/SegmentedMessage.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wEAAiD;AAEjD,sDAAgC;AAChC,8DAAwC;AACxC,gEAA0C;AAM1C,IAAM,mBAAmB,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;AAEvD;;GAEG;AACH;IAaE;;;;;;;;OAQG;IACH,0BAAY,OAAe,EAAE,QAAuC;QAAvC,yBAAA,EAAA,iBAAuC;QAClE,IAAM,QAAQ,GAAG,IAAI,2BAAgB,EAAE,CAAC;QAExC,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;YAC3C,MAAM,IAAI,KAAK,CACb,cAAY,QAAQ,sDAAiD,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAG,CACtG,CAAC;SACH;QAED;;WAEG;QACH,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QAClD;;;WAGG;QACH,IAAI,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AAI,CAAC,YAAY,CAAC,CAAC,CAAC;aACjE;;;;;;;;;QACD,OAAO,YAAY,CAAC;IACtB,CAAC;IAKD,sBAAI,uCAAS;QAHb;;WAEG;aACH;;YACE,IAAI,IAAI,GAAG,CAAC,CAAC;;gBACb,KAAsB,IAAA,KAAA,SAAA,IAAI,CAAC,QAAQ,CAAA,gBAAA,4BAAE;oBAAhC,IAAM,OAAO,WAAA;oBAChB,IAAI,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;iBAC9B;;;;;;;;;YACD,OAAO,IAAI,CAAC;QACd,CAAC;;;OAAA;IAKD,sBAAI,yCAAW;QAHf;;WAEG;aACH;;YACE,IAAI,IAAI,GAAG,CAAC,CAAC;;gBACb,KAAsB,IAAA,KAAA,SAAA,IAAI,CAAC,QAAQ,CAAA,gBAAA,4BAAE;oBAAhC,IAAM,OAAO,WAAA;oBAChB,IAAI,IAAI,OAAO,CAAC,iBAAiB,EAAE,CAAC;iBACrC;;;;;;;;;YACD,OAAO,IAAI,CAAC;QACd,CAAC;;;OAAA;IAMD,sBAAI,2CAAa;QAJjB;;;WAGG;aACH;YACE,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;QAC9B,CAAC;;;OAAA;IAED;;;OAGG;IACH,8CAAmB,GAAnB;QACE,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,UAAC,WAAW,IAAK,OAAA,CAAC,WAAW,CAAC,MAAM,EAAnB,CAAmB,CAAC,CAAC,GAAG,CAAC,UAAC,WAAW,IAAK,OAAA,WAAW,CAAC,GAAG,EAAf,CAAe,CAAC,CAAC;IAC9G,CAAC;IACH,uBAAC;AAAD,CAAC,AA1LD,IA0LC;AA1LY,4CAAgB"} diff --git a/dist/libs/TwilioReservedChar.js b/dist/libs/TwilioReservedChar.js deleted file mode 100644 index bcfacf1..0000000 --- a/dist/libs/TwilioReservedChar.js +++ /dev/null @@ -1,44 +0,0 @@ -"use strict"; -var __extends = (this && this.__extends) || (function () { - var extendStatics = function (d, b) { - extendStatics = Object.setPrototypeOf || - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || - function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; - return extendStatics(d, b); - }; - return function (d, b) { - if (typeof b !== "function" && b !== null) - throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); - extendStatics(d, b); - function __() { this.constructor = d; } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; -})(); -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -var EncodedChar_1 = __importDefault(require("./EncodedChar")); -/* - * Represent a Twilio reserved octet - * Twilio messages reserve 6 of this per segment - */ -var TwilioReservedChar = /** @class */ (function (_super) { - __extends(TwilioReservedChar, _super); - function TwilioReservedChar() { - var _this = - // content and encoding are not relevant for headers - _super.call(this, '', 'GSM-7') || this; - _this.isReservedChar = true; - return _this; - } - TwilioReservedChar.codeUnitSizeInBits = function () { - return 8; - }; - TwilioReservedChar.prototype.sizeInBits = function () { - return 8; - }; - return TwilioReservedChar; -}(EncodedChar_1.default)); -exports.default = TwilioReservedChar; -//# sourceMappingURL=TwilioReservedChar.js.map \ No newline at end of file diff --git a/dist/libs/TwilioReservedChar.js.map b/dist/libs/TwilioReservedChar.js.map deleted file mode 100644 index 3f7a097..0000000 --- a/dist/libs/TwilioReservedChar.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"TwilioReservedChar.js","sourceRoot":"","sources":["../../src/libs/TwilioReservedChar.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,8DAAwC;AAExC;;;GAGG;AACH;IAAiC,sCAAW;IAG1C;QAAA;QACE,oDAAoD;QACpD,kBAAM,EAAE,EAAE,OAAO,CAAC,SAEnB;QADC,KAAI,CAAC,cAAc,GAAG,IAAI,CAAC;;IAC7B,CAAC;IAEM,qCAAkB,GAAzB;QACE,OAAO,CAAC,CAAC;IACX,CAAC;IAED,uCAAU,GAAV;QACE,OAAO,CAAC,CAAC;IACX,CAAC;IACH,yBAAC;AAAD,CAAC,AAhBD,CAAiC,qBAAW,GAgB3C;AAED,kBAAe,kBAAkB,CAAC"} \ No newline at end of file diff --git a/dist/libs/UCS2EncodedChar.js b/dist/libs/UCS2EncodedChar.js deleted file mode 100644 index 3c3004c..0000000 --- a/dist/libs/UCS2EncodedChar.js +++ /dev/null @@ -1,50 +0,0 @@ -"use strict"; -var __extends = (this && this.__extends) || (function () { - var extendStatics = function (d, b) { - extendStatics = Object.setPrototypeOf || - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || - function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; - return extendStatics(d, b); - }; - return function (d, b) { - if (typeof b !== "function" && b !== null) - throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); - extendStatics(d, b); - function __() { this.constructor = d; } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; -})(); -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -var EncodedChar_1 = __importDefault(require("./EncodedChar")); -/* - * Represent a UCS-2 encoded character - * UCS-2 is of fixed length and requires 2 code units per character - * a UCS-2 code unit is an octet (8bits) - */ -var UCS2EncodedChar = /** @class */ (function (_super) { - __extends(UCS2EncodedChar, _super); - function UCS2EncodedChar(char, graphemeSize) { - if (graphemeSize === void 0) { graphemeSize = 1; } - var _this = _super.call(this, char, 'UCS-2') || this; - _this.graphemeSize = graphemeSize === undefined ? 1 : graphemeSize; - if (char.length === 2) { - _this.codeUnits = [char.charCodeAt(0), char.charCodeAt(1)]; - } - else { - _this.codeUnits = [char.charCodeAt(0)]; - } - return _this; - } - UCS2EncodedChar.codeUnitSizeInBits = function () { - return 16; // UCS-2 code units are 8bits long - }; - UCS2EncodedChar.prototype.sizeInBits = function () { - return 16 * this.raw.length; - }; - return UCS2EncodedChar; -}(EncodedChar_1.default)); -exports.default = UCS2EncodedChar; -//# sourceMappingURL=UCS2EncodedChar.js.map \ No newline at end of file diff --git a/dist/libs/UCS2EncodedChar.js.map b/dist/libs/UCS2EncodedChar.js.map deleted file mode 100644 index 7a13d3c..0000000 --- a/dist/libs/UCS2EncodedChar.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"UCS2EncodedChar.js","sourceRoot":"","sources":["../../src/libs/UCS2EncodedChar.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,8DAAwC;AAExC;;;;GAIG;AACH;IAA8B,mCAAW;IAGvC,yBAAY,IAAY,EAAE,YAAwB;QAAxB,6BAAA,EAAA,gBAAwB;QAAlD,YACE,kBAAM,IAAI,EAAE,OAAO,CAAC,SAQrB;QAPC,KAAI,CAAC,YAAY,GAAG,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC;QAElE,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;YACrB,KAAI,CAAC,SAAS,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;SAC3D;aAAM;YACL,KAAI,CAAC,SAAS,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;SACvC;;IACH,CAAC;IAEM,kCAAkB,GAAzB;QACE,OAAO,EAAE,CAAC,CAAC,kCAAkC;IAC/C,CAAC;IAED,oCAAU,GAAV;QACE,OAAO,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC;IAC9B,CAAC;IACH,sBAAC;AAAD,CAAC,AArBD,CAA8B,qBAAW,GAqBxC;AAED,kBAAe,eAAe,CAAC"} \ No newline at end of file diff --git a/src/libs/SegmentedMessage.ts b/src/libs/SegmentedMessage.ts index 97a61f8..f44f7b1 100644 --- a/src/libs/SegmentedMessage.ts +++ b/src/libs/SegmentedMessage.ts @@ -26,6 +26,8 @@ export class SegmentedMessage { numberOfCharacters: number; + encodedChars: EncodedChars; + /** * * Create a new segmented message from a string @@ -75,11 +77,15 @@ export class SegmentedMessage { this.encodingName = 'GSM-7'; } - const encodedChars: EncodedChars = this._encodeChars(this.graphemes); + /** + * @property {string[]} encodedChars Array of encoded characters composing the message + */ + this.encodedChars = this._encodeChars(this.graphemes); + /** * @property {object[]} segments Array of segment(s) the message have been segmented into */ - this.segments = this._buildSegments(encodedChars); + this.segments = this._buildSegments(this.encodedChars); } /** @@ -157,7 +163,7 @@ export class SegmentedMessage { } /** - * @return {number} Total size of the message in bits (including User Data Header if present) + * @returns {number} Total size of the message in bits (including User Data Header if present) */ get totalSize(): number { let size = 0; @@ -168,7 +174,7 @@ export class SegmentedMessage { } /** - * @return {number} Total size of the message in bits (excluding User Data Header if present) + * @returns {number} Total size of the message in bits (excluding User Data Header if present) */ get messageSize(): number { let size = 0; @@ -180,9 +186,17 @@ export class SegmentedMessage { /** * - * @return {number} Number of segments + * @returns {number} Number of segments */ get segmentsCount(): number { return this.segments.length; } + + /** + * + * @returns {string[]} Array of characters representing the non GSM-7 characters in the message body + */ + getNonGsmCharacters(): string[] { + return this.encodedChars.filter((encodedChar) => !encodedChar.isGSM7).map((encodedChar) => encodedChar.raw); + } } diff --git a/tests/index.test.js b/tests/index.test.js index d9a21e6..efd57af 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -143,40 +143,4 @@ describe('GSM-7 Escape Characters', () => { expect(segmentedMessage.totalSize).toBe(1223); }); }); -}); - -describe('GSM-7 Segements analysis', () => { - const testMessage = - '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567'; - const segmentedMessage = new SegmentedMessage(testMessage); - test('Check User Data Header', () => { - for (var segmentIndex = 0; segmentIndex <= 2; segmentIndex++) { - for (var index = 0; index < 6; index++) { - expect(segmentedMessage.segments[segmentIndex][index].isUserDataHeader).toBe(true); - } - } - }); - - test('Check last segment has only 1 character', () => { - expect(segmentedMessage.segments[2].length).toBe(7); - expect(segmentedMessage.segments[2][6].raw).toBe('7'); - }); -}); - -describe('UCS-2 Segements analysis', () => { - const testMessage = - '😜2345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234'; - const segmentedMessage = new SegmentedMessage(testMessage); - test('Check User Data Header', () => { - for (var segmentIndex = 0; segmentIndex <= 2; segmentIndex++) { - for (var index = 0; index < 6; index++) { - expect(segmentedMessage.segments[segmentIndex][index].isUserDataHeader).toBe(true); - } - } - }); - - test('Check last segment has only 1 character', () => { - expect(segmentedMessage.segments[2].length).toBe(7); - expect(segmentedMessage.segments[2][6].raw).toBe('4'); - }); -}); +}); \ No newline at end of file diff --git a/tests/methods.test.js b/tests/methods.test.js new file mode 100644 index 0000000..19ce3c2 --- /dev/null +++ b/tests/methods.test.js @@ -0,0 +1,9 @@ +const { SegmentedMessage } = require('../dist'); + +describe('Test SegmentedMessage methods', () => { + it('getNonGsmCharacters()', () => { + const testMessage = 'más'; + const segmentedMessage = new SegmentedMessage(testMessage); + expect(segmentedMessage.getNonGsmCharacters()).toEqual(['á']); + }) +}); diff --git a/tests/segments.test.js b/tests/segments.test.js new file mode 100644 index 0000000..50f8743 --- /dev/null +++ b/tests/segments.test.js @@ -0,0 +1,38 @@ + +const { SegmentedMessage } = require('../dist'); + +describe('GSM-7 Segements analysis', () => { + const testMessage = + '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567'; + const segmentedMessage = new SegmentedMessage(testMessage); + test('Check User Data Header', () => { + for (var segmentIndex = 0; segmentIndex <= 2; segmentIndex++) { + for (var index = 0; index < 6; index++) { + expect(segmentedMessage.segments[segmentIndex][index].isUserDataHeader).toBe(true); + } + } + }); + + test('Check last segment has only 1 character', () => { + expect(segmentedMessage.segments[2].length).toBe(7); + expect(segmentedMessage.segments[2][6].raw).toBe('7'); + }); + }); + + describe('UCS-2 Segements analysis', () => { + const testMessage = + '😜2345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234'; + const segmentedMessage = new SegmentedMessage(testMessage); + test('Check User Data Header', () => { + for (var segmentIndex = 0; segmentIndex <= 2; segmentIndex++) { + for (var index = 0; index < 6; index++) { + expect(segmentedMessage.segments[segmentIndex][index].isUserDataHeader).toBe(true); + } + } + }); + + test('Check last segment has only 1 character', () => { + expect(segmentedMessage.segments[2].length).toBe(7); + expect(segmentedMessage.segments[2][6].raw).toBe('4'); + }); +}); \ No newline at end of file