All files / src/display/sms gsmsplitter.ts

98.88% Statements 89/90
86.95% Branches 20/23
100% Functions 3/3
98.88% Lines 89/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 91134x 134x 4x 4x 4x 134x 134x 560x 560x 560x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 505x 505x 505x 505x 505x 505x 505x 505x 505x 1287x 1287x 1287x 1287x 1287x 1287x 1287x 1287x 1287x 1287x 1287x 1287x 1287x 505x 560x 132975x 132975x 132975x 4x 1x 1x 4x 132975x 247x 247x 247x 132975x 132975x 132975x 132975x 132975x 132975x 132975x 132975x 505x 505x 505x 560x 3x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x 502x 502x 502x 502x 502x 502x 502x  
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
  };
};