UNPKG

split-sms

Version:

An SMS message splitter with support for both GSM and Unicode.

80 lines (65 loc) 1.65 kB
function isHighSurrogate(code) { return code >= 0xD800 && code <= 0xDBFF; } module.exports.split = function (message, options) { options = options || { summary: false }; if (message === '') { return { parts: [{ content: options.summary ? undefined : '', length: 0, bytes: 0 }], totalLength: 0, totalBytes: 0 }; } var messages = []; var length = 0; var bytes = 0; var totalBytes = 0; var totalLength = 0; var partStart = 0; function bank(partEnd) { var 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 (var i = 0, count = message.length; i < count; i++) { var code = message.charCodeAt(i); var 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 }; };