All files / src/display/sms gsmsplitter.ts

76.66% Statements 69/90
62.5% Branches 10/16
66.66% Functions 2/3
76.66% Lines 69/90

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 9198x 98x       98x 98x 416x 416x 416x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 401x 401x 401x 401x 401x 401x 401x 401x 401x 1174x 1174x 1174x 1174x 1174x 1174x 1174x 1174x 1174x 1174x 1174x 1174x 1174x 401x 416x 131050x 131050x 131050x         131050x 8x 8x 8x 131050x 131050x 131050x 131050x 131050x 131050x 131050x 131050x 401x 401x 401x 416x                             401x 401x 401x 401x 401x 401x 401x  
import { validateCharacter, validateExtendedCharacter } from './gsmvalidator';
 
function isHighSurrogate(code) {
  return code >= 0xd800 && code <= 0xdbff;
}
 
export const gsmSplit = function (message, options) {
  options = options || { summary: false };
 
  if (message === '') {
    return {
      parts: [
        {
          content: options.summary ? undefined : '',
          length: 0,
          bytes: 0
        }
      ],
      totalLength: 0,
      totalBytes: 0
    };
  }
 
  const messages = [];
  let length = 0;
  let bytes = 0;
  let totalBytes = 0;
  let totalLength = 0;
  let messagePart = '';
 
  function bank() {
    const msg = {
      content: options.summary ? undefined : messagePart,
      length: length,
      bytes: bytes
    };
    messages.push(msg);
 
    totalLength += length;
    length = 0;
    totalBytes += bytes;
    bytes = 0;
    messagePart = '';
  }
 
  for (let i = 0, count = message.length; i < count; i++) {
    let c = message.charAt(i);
 
    if (!validateCharacter(c)) {
      if (isHighSurrogate(c.charCodeAt(0))) {
        i++;
      }
      c = '\u0020';
    } else if (validateExtendedCharacter(c)) {
      if (bytes === 152) bank();
      bytes++;
    }
 
    bytes++;
    length++;
 
    if (!options.summary) messagePart += c;
 
    if (bytes === 153) bank();
  }
 
  if (bytes > 0) bank();
 
  if (messages[1] && totalBytes <= 160) {
    return {
      parts: [
        {
          content: options.summary
            ? undefined
            : messages[0].content + messages[1].content,
          length: totalLength,
          bytes: totalBytes
        }
      ],
      totalLength: totalLength,
      totalBytes: totalBytes
    };
  }
 
  return {
    parts: messages,
    totalLength: totalLength,
    totalBytes: totalBytes
  };
};