1.0.13 • Published 4 years ago

@trt2/gsm-charset-utils v1.0.13

Weekly downloads
1,340
License
MIT
Repository
github
Last release
4 years ago

GSM Character Set Utilities

Functions for counting SMS message parts and removing non-GSM alphabet characters.

Usage

Basic usage of the functions:

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 `');
// charCount = {"charCount":20,"msgCount":1,"charsPerSegment":70,"encoding":"UCS2"}

charCount = GsmCharsetUtils.getCharCount('Test UCS2 emoji 😀');
// charCount = {"charCount":18,"msgCount":1,"charsPerSegment":70,"encoding":"UCS2"}

GsmCharsetUtils.isGsmChar('a'); // returns true

// removeNonGsmChars() will replace non-GSM characters with '?',
// but it will convert a very limited number of characters to
// similar characters in the GSM alphabet.
let gsmText = GsmCharsetUtils.removeNonGsmChars("Hello world!");
// gsmText = "Hello world!"

let gsmText = GsmCharsetUtils.removeNonGsmChars("«Hello world!»");
// gsmText = "\"Hello world!\""

function getCharCount(text)

  • text

Returns an object:

{
    "charCount": <number>,          // Total character count
    "msgCount": <number>,           // Number of SMS messages
    "charsPerSegment": <number>,    // Characters per message segment
    "encoding": <string: "GSM" or "UCS2">
}

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.

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.11-beta.0

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago