UNPKG

sms-parse

Version:
57 lines (46 loc) 1.38 kB
import {Transliterate} from './TransliterateGSM'; import {UnicodeToGsm} from './UnicodeToGSM'; /** * Encoded Character Classes * Utility classes to represent a character in a given encoding. */ export class EncodedChar { // Raw character (grapheme) as passed in the constructor raw: string; // Array of 8 bits number rapresenting the encoded character codeUnits: number[]; // True if the character is a GSM7 one isGSM7: boolean; // Which encoding to use for this char encoding: 'GSM-7' | 'UCS-2'; constructor( char: string, encoding: 'GSM-7' | 'UCS-2', forceGsmEcoding = false, ) { this.raw = char; this.encoding = encoding; this.isGSM7 = Boolean(char && UnicodeToGsm[char.charCodeAt(0)]); if (this.isGSM7) { this.codeUnits = UnicodeToGsm[char.charCodeAt(0)]; } else { this.codeUnits = []; for (let i = 0; i < char.length; i++) { this.codeUnits.push(char.charCodeAt(i)); } } if (forceGsmEcoding) { const transliterate = Transliterate[char.charCodeAt(0)]; if (transliterate) { this.codeUnits = transliterate; } } } codeUnitSizeInBits(): number { return this.encoding === 'GSM-7' ? 7 : 8; } sizeInBits(): number { const bitsPerUnits = this.encoding === 'GSM-7' ? 7 : 16; return bitsPerUnits * this.codeUnits.length; } }