Utility functions for SMS/GSM charset handling
import * as GsmCharsetUtils from '@trt2/gsm-charset-utils';
// This imports:
// - GsmCharsetUtils.getCharCount(text)
// - GsmCharsetUtils.isGsmChar(c)
// - GsmCharsetUtils.removeNonGsmChars(msgText)
let charCount = GsmCharsetUtils.getCharCount('Hello world!');
// charCount = {"charCount":12,"msgCount":1,"charsPerSegment":160,"encoding":"GSM"}
charCount = GsmCharsetUtils.getCharCount('Test UCS2 backtick ');
function getCharCount(text)
- text
Returns an object:
`
{
"charCount": , // Total character count
"msgCount": , // Number of SMS messages
"charsPerSegment": , // Characters per message segment
"encoding":
}
`
The function will not split messages on an extended GSM character or a UCS2 surrogate pair, the whole character is moved to the next segment.
function isGsmChar( c )
- c - Check if this character is part of either the GSM alphabet or the extended GSM alphabet.
function removeNonGsmChars(msgText, replacementChar='?')
- msgText - Text from which non-GSM characters will be removed.
- replacementChar - The character to replace non-GSM characters with
Non-GSM characters will be replaced by replacementChar`, a very limited number of characters will be converted to similar characters in the GSM alphabet.