function test(fn, str1, str2, str3) {
let count = 100000;
const Ts = performance.now();
while (count) {
fn(str1);
fn(str2);
fn(str3);
count -= 1;
}
const Te = performance.now();
console.log(`name: ${fn.name} time: ${Math.ceil(Te - Ts)}`);
}
function tokenise(str, delimiter) {
return str.split(delimiter);
}
function countCharacter(str, character) {
var first = character.charAt(0);
return first === '' ? Infinity : Math.min(Math.max(tokenise(str, first).length - 1, 0), Infinity);
}
function splitAtNth(str, nth, delimiter) {
const tokens = tokenise(str, delimiter);
return [tokens.slice(0, nth).join(delimiter), tokens.slice(nth).join(delimiter)];
}
function chunk(arr, size) {
const chunks = [];
const length = arr.length;
let index = 0;
while (index < length) {
chunks.push(arr.slice(index, index + size));
index += size;
}
return chunks;
}
function pairs(arr) {
return chunk(arr, 2);
}
function isEven(num) {
return num % 2 === 0;
}
function camelCase(a, b) {
return `${a}${b.charAt(0).toUpperCase()}${b.slice(1)}`;
}
function countSpaces(str) {
return countCharacter(str, ' ');
}
function spacesSpare(sms) {
const spaces = (sms.length - 160) - countSpaces(sms);
return spaces < 0 ? Math.abs(spaces) : 0;
}
function splitAtNthSpace(str, nth) {
return splitAtNth(str, nth, ' ');
}
function stringToEvenPadPairs(str) {
const tokens = tokenise(str, ' ');
if (!isEven(tokens.length)) {
tokens.push('');
}
return pairs(tokens);
}
function camelAcumulate(acc, pair) {
return camelCase(acc, camelCase(...pair));
}
function shortenSMS(sms) {
const atNth = spacesSpare(sms);
if (atNth) {
const parts = splitAtNthSpace(sms, atNth);
return stringToEvenPadPairs(parts[1]).reduce(camelAcumulate, parts[0]);
}
return stringToEvenPadPairs(sms).reduce(camelAcumulate, '');
}
const testSMS1 = 'Short meSSages can be encoded using a variety of alphabets: the default GSM 7-bit alphabet, the 8-bit data alphabet, and the 16-bit UCS-2 alphabet.';
const testSMS2 = '(octets * Eight bits / octet = 1120 bits). Short meSSages can be encoded using a variety of alphabets: the default GSM 7-bit alphabet, the 8-bit data alphabet, and the 16-bit UCS-2 alphabet.';
const testSMS3 = '(blah blah blah octets * Eight bits / octet = 1120 bits). Short meSSages can be encoded using a variety of alphabets: the default GSM 7-bit alphabet, the 8-bit data alphabet, and the 16-bit UCS-2 alphabet.';
test(shortenSMS, testSMS1, testSMS2, testSMS3);
var shortener = function(sms) {
let n = sms.match(/\s/g) ? sms.match(/\s/g).length : 0;
n = sms.length <= 160 ? 0 : (sms.length - 160) > n ? n : sms.length - 160;
const re = new RegExp("([^ ][ ][^ ]*){" + n + "}$");
return !n ? sms : sms.replace(re, (ext) => {
return ext.replace(/ [^ ]/g, (m, p1) => {
return m.trim().toUpperCase();
})
});
};
test(shortener, testSMS1, testSMS2, testSMS3);