All files / src/display/sms index.ts

100% Statements 57/57
100% Branches 9/9
100% Functions 2/2
100% Lines 57/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 5898x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 98x 419x 419x 419x 419x 98x 98x 419x 419x 419x 419x 419x 419x 419x 419x 3x 419x 419x 419x 416x 416x 416x 416x 419x 3x 3x 3x 3x 3x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x  
/**
 * This is the same SMS splitter we've been using but we've
 * updated it here to use it with ESM imports
 * See https://github.com/Codesleuth/split-sms
 */
 
import { gsmSplit } from './gsmsplitter';
import { validateMessage } from './gsmvalidator';
import { unicodeSplit } from './unicodesplitter';
 
export const UNICODE = 'Unicode';
export const GSM = 'GSM';
 
function calculateRemaining(parts, singleBytes, multiBytes, charBytes) {
  const max = parts.length === 1 ? singleBytes : multiBytes;
  return (max - parts[parts.length - 1].bytes) / charBytes;
}
 
export const splitSMS = function (message: string, options: any = {}) {
  const characterset = options && options.characterset;
 
  options = {
    summary: options && options.summary
  };
 
  const isGsm =
    (characterset === undefined && validateMessage(message)) ||
    characterset === GSM;
  let splitResult, singleBytes, multiBytes, charBytes;
 
  if (isGsm) {
    splitResult = gsmSplit(message, options);
    singleBytes = 160;
    multiBytes = 153;
    charBytes = 1;
  } else {
    splitResult = unicodeSplit(message, options);
    singleBytes = 140;
    multiBytes = 134;
    charBytes = 2;
  }
 
  const remainingInPart = calculateRemaining(
    splitResult.parts,
    singleBytes,
    multiBytes,
    charBytes
  );
 
  return {
    characterSet: isGsm ? GSM : UNICODE,
    parts: splitResult.parts,
    bytes: splitResult.totalBytes,
    length: splitResult.totalLength,
    remainingInPart: remainingInPart
  };
};