All files / src/display/sms unicodesplitter.ts

70.93% Statements 61/86
64.28% Branches 9/14
100% Functions 3/3
70.93% Lines 61/86

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 87292x 292x 292x 98x 98x 3x 3x 3x                         3x 3x 3x 3x 3x 3x 3x 3x 3x 6x 6x   6x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 292x 292x 292x 292x 3x 3x 3x 3x 292x 292x 292x 292x 292x 292x 3x 3x 3x 3x                         3x 3x 3x 3x 3x 3x 3x  
function isHighSurrogate(code) {
  return code >= 0xd800 && code <= 0xdbff;
}
 
export const unicodeSplit = 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 partStart = 0;
 
  function bank(partEnd = undefined) {
    const msg = {
      content: options.summary
        ? undefined
        : partEnd
          ? message.substring(partStart, partEnd + 1)
          : message.substring(partStart),
      length: length,
      bytes: bytes
    };
    messages.push(msg);
 
    partStart = partEnd + 1;
 
    totalLength += length;
    length = 0;
    totalBytes += bytes;
    bytes = 0;
  }
 
  for (let i = 0, count = message.length; i < count; i++) {
    const code = message.charCodeAt(i);
    const highSurrogate = isHighSurrogate(code);
 
    if (highSurrogate) {
      if (bytes === 132) bank(i - 1);
      bytes += 2;
      i++;
    }
 
    bytes += 2;
    length++;
 
    if (bytes === 134) bank(i);
  }
 
  if (bytes > 0) bank();
 
  if (messages[1] && totalBytes <= 140) {
    return {
      parts: [
        {
          content: options.summary ? undefined : message,
          length: totalLength,
          bytes: totalBytes
        }
      ],
      totalLength: totalLength,
      totalBytes: totalBytes
    };
  }
 
  return {
    parts: messages,
    totalLength: totalLength,
    totalBytes: totalBytes
  };
};