string-breaker
Version:
Breaks string into string array with many options
384 lines (293 loc) • 7.86 kB
JavaScript
import _extends from '@babel/runtime/helpers/esm/extends';
import { codePointFullWidth } from 'utf16-char-codes';
var splitByOpt;
(function (splitByOpt) {
splitByOpt[splitByOpt["width"] = 0] = "width";
splitByOpt[splitByOpt["word"] = 1] = "word";
splitByOpt[splitByOpt["line"] = 2] = "line";
})(splitByOpt || (splitByOpt = {}));
var lnEndOpt;
(function (lnEndOpt) {
lnEndOpt[lnEndOpt["none"] = 0] = "none";
lnEndOpt[lnEndOpt["noLnBr"] = 1] = "noLnBr";
lnEndOpt[lnEndOpt["encode"] = 2] = "encode";
})(lnEndOpt || (lnEndOpt = {}));
var widthFlags;
(function (widthFlags) {
widthFlags[widthFlags["none"] = 0] = "none";
widthFlags[widthFlags["fullwidth"] = 1] = "fullwidth";
widthFlags[widthFlags["surrogatePair"] = 2] = "surrogatePair";
widthFlags[widthFlags["nearestWord"] = 4] = "nearestWord";
})(widthFlags || (widthFlags = {}));
var stringBreaker = function stringBreaker(str, opt) {
if (typeof str !== 'string') {
throw new TypeError('stringBreaker: str parmeter must be of type string');
}
var options = getOptions({
width: 80,
lnEnd: lnEndOpt.noLnBr,
noExSp: false,
noBOM: true,
lenOpt: widthFlags.none,
splitOpt: splitByOpt.width
}, opt);
if (options.splitOpt === splitByOpt.width) {
switch (options.lnEnd) {
case lnEndOpt.encode:
str = encodeLnBr(str);
break;
case lnEndOpt.noLnBr:
str = removeLnBr(str);
break;
}
}
var result;
if (options.noExSp === true) {
str = removeExSp(str);
}
switch (options.splitOpt) {
case splitByOpt.word:
result = breakStrByEolWord(str, options);
break;
case splitByOpt.line:
result = breakStrByEolWord(str, options);
break;
default:
result = breakStrByCodePoint(str, options);
break;
}
return result;
};
var getOptions = function getOptions(defaultOptions, options) {
if (options === null || options === undefined || typeof options === 'function') {
return defaultOptions;
}
if (typeof options === 'number') {
defaultOptions = _extends({}, defaultOptions);
defaultOptions.width = options;
options = defaultOptions;
}
if (options.width === undefined) {
options.width = 80;
}
if (options.lnEnd === undefined) {
options.lnEnd = lnEndOpt.noLnBr;
}
if (options.noExSp === undefined) {
options.noExSp = false;
}
if (options.noBOM === undefined) {
options.noBOM = true;
}
if (options.lenOpt === undefined) {
options.lenOpt = widthFlags.none;
}
if (options.splitOpt === undefined) {
options.splitOpt = splitByOpt.width;
}
return options;
};
var breakStrByEolWord = function breakStrByEolWord(str, opt) {
var results = [];
if (str.length === 0) {
return results;
}
var noBom = false;
if (opt.noBOM === true) {
noBom = true;
}
if (noBom === true) {
var cp = Number(str.codePointAt(0));
if (isBom(cp) === true) {
str = str.substr(1);
if (str.length === 0) {
return results;
}
}
}
str = cleanLnBr(str);
if (opt.splitOpt === splitByOpt.word) {
str = whiteSpToSp(str);
str = str.trim();
if (str.length === 0) {
return results;
}
results = str.split(' ');
} else {
if (str.length === 0) {
return results;
}
results = str.split(/\n/);
}
return results;
};
var breakStrByCodePoint = function breakStrByCodePoint(str, opt) {
var maxWidth = Math.round(Number(opt.width));
if (maxWidth < 1) {
throw new RangeError('stringBreaker: Width must be greater than zero');
}
var lines = [];
var ln = [];
var noBom = false;
var respectWidth = false;
var respectSurrogagePair = false;
var respectNearstWord = false;
if (opt.lenOpt !== undefined) {
var fullMask = 7;
if (opt.lenOpt < widthFlags.none || opt.lenOpt > fullMask) {
throw new RangeError("stringBreaker: widthflags enum out of range. Expected value to be from " + widthFlags.none + " to " + fullMask);
}
if ((opt.lenOpt & widthFlags.fullwidth) === widthFlags.fullwidth) {
respectWidth = true;
}
if ((opt.lenOpt & widthFlags.surrogatePair) === widthFlags.surrogatePair) {
respectSurrogagePair = true;
}
if ((opt.lenOpt & widthFlags.nearestWord) === widthFlags.nearestWord) {
respectNearstWord = true;
}
}
if (opt.noBOM === true) {
noBom = true;
}
var width = 0;
for (var i = 0; i < str.length; i++) {
var code = str.codePointAt(i);
if (code === undefined) {
throw new Error("stringBreaker Error: No code point exist in first parameter str at postion " + i);
}
var cp = Number(code);
var _char = String.fromCodePoint(cp);
if (i === 0) {
if (noBom === true) {
if (isBom(cp) === true) {
continue;
}
} else {
if (isBom(cp) === true) {
ln.push(_char);
continue;
}
}
}
if (isSurrogatePair(cp) === true) {
i++;
}
if (respectNearstWord === true && ln.length === 0) {
if (isWhiteSpace(cp) === true && isPrintableWhiteSpace(cp) === false) {
continue;
}
}
width++;
if (respectWidth === true && codePointFullWidth(cp) === true) {
width++;
}
if (respectSurrogagePair === true && isSurrogatePair(cp) === true) {
width++;
}
if (respectNearstWord === true) {
if (isWhiteSpace(cp) === false) {
ln.push(_char);
continue;
} else {
if (isNonBreakSpace(cp) === true) {
ln.push(_char);
continue;
}
if (isPrintableWhiteSpace(cp) === true) {
ln.push(_char);
} else if (width < maxWidth) {
ln.push(_char);
}
}
} else {
ln.push(_char);
}
if (width >= maxWidth) {
lines.push(ln.join(''));
ln = [];
width = 0;
}
}
if (ln.length > 0) {
lines.push(ln.join(''));
ln = [];
}
return lines;
};
var isZSpace = function isZSpace(num) {
var z = [0x0020, 0x00A0, 0x1680, 0x202F, 0x205F, 0x3000];
if (z.indexOf(num) !== -1) {
return true;
}
if (num >= 0x2000 && num <= 0x200A) {
return true;
}
return false;
};
var isWhiteSpace = function isWhiteSpace(num) {
var w = [0x0085, 0x2029];
if (w.indexOf(num) !== -1) {
return true;
}
if (num >= 0x0009 && num <= 0x000D) {
return true;
}
if (isZSpace(num) === true) {
return true;
}
return false;
};
var isNonBreakSpace = function isNonBreakSpace(num) {
if (num === 0x00A0 || num === 0x202F) {
return true;
}
return false;
};
var isPrintableWhiteSpace = function isPrintableWhiteSpace(num) {
if (num === 0x1680) {
return true;
}
return false;
};
var removeLnBr = function removeLnBr(str) {
if (str.length === 0) {
return '';
}
return str.replace(/(\r\n|\n|\r)/gm, '');
};
var whiteSpToSp = function whiteSpToSp(str) {
if (str.length === 0) {
return '';
}
return str.replace(/\s+/gm, ' ');
};
var cleanLnBr = function cleanLnBr(str) {
if (str.length === 0) {
return '';
}
return str.replace(/(\r\n|\r)/gm, '\n');
};
var removeExSp = function removeExSp(str) {
if (str.length === 0) {
return '';
}
return str.replace(/\x20\x20+/g, ' ');
};
var encodeLnBr = function encodeLnBr(str) {
return str.replace(/(\r\n|\n|\r)/gm, '\\n');
};
var isSurrogatePair = function isSurrogatePair(cp) {
if (cp >= 0x10000) {
return true;
}
return false;
};
var isBom = function isBom(cp) {
if (cp === 0xFEFF || cp === 0xFFFE || cp === 0xEFBBBF || cp === 0x2B2F76382D || cp === 0x2B2F7638 || cp === 0x2B2F7639 || cp === 0x2B2F7626) {
return true;
}
return false;
};
export { lnEndOpt, splitByOpt, stringBreaker, widthFlags };
//# sourceMappingURL=index.es.js.map