utils/text.js
A module for ac-bundle-app, published by ac-bundle-module.
Available Methods: encodeEntities, decodeEntities, document, numeral, parse, strip, clean
encodeEntities
function encode(text, _a) {
var _b = _a === void 0 ? defaultEncodeOptions : _a, _c = _b.mode, mode = _c === void 0 ? 'specialChars' : _c, _d = _b.numeric, numeric = _d === void 0 ? 'decimal' : _d, _e = _b.level, level = _e === void 0 ? 'all' : _e;
if (!text) {
return '';
}
var encodeRegExp = encodeRegExps[mode];
var references = allNamedReferences[level].characters;
var isHex = numeric === 'hexadecimal';
encodeRegExp.lastIndex = 0;
var _b = encodeRegExp.exec(text);
var _c;
if (_b) {
_c = '';
var _d = 0;
do {
if (_d !== _b.index) {
_c += text.substring(_d, _b.index);
}
var _e = _b[0];
var result_1 = references[_e];
if (!result_1) {
var code_1 = _e.length > 1 ? surrogate_pairs_1.getCodePoint(_e, 0) : _e.charCodeAt(0);
result_1 = (isHex ? '' + code_1.toString(16) : '' + code_1) + ';';
}
_c += result_1;
_d = _b.index + _e.length;
} while ((_b = encodeRegExp.exec(text)));
if (_d !== text.length) {
_c += text.substring(_d);
}
}
else {
_c =
text;
}
return _c;
}
decodeEntities
function decode(text, _a) {
var decodeSecondChar_1 = _a === void 0 ? defaultDecodeOptions : _a, decodeCode_1 = decodeSecondChar_1.level, level = decodeCode_1 === void 0 ? 'all' : decodeCode_1, _b = decodeSecondChar_1.scope, scope = _b === void 0 ? level === 'xml' ? 'strict' : 'body' : _b;
if (!text) {
return '';
}
var decodeRegExp = decodeRegExps[level][scope];
var references = allNamedReferences[level].entities;
var isAttribute = scope === 'attribute';
var isStrict = scope === 'strict';
decodeRegExp.lastIndex = 0;
var replaceMatch_1 = decodeRegExp.exec(text);
var replaceResult_1;
if (replaceMatch_1) {
replaceResult_1 = '';
var replaceLastIndex_1 = 0;
do {
if (replaceLastIndex_1 !== replaceMatch_1.index) {
replaceResult_1 += text.substring(replaceLastIndex_1, replaceMatch_1.index);
}
var replaceInput_1 = replaceMatch_1[0];
var decodeResult_1 = replaceInput_1;
var decodeEntityLastChar_2 = replaceInput_1[replaceInput_1.length - 1];
if (isAttribute
&& decodeEntityLastChar_2 === '=') {
decodeResult_1 = replaceInput_1;
}
else if (isStrict
&& decodeEntityLastChar_2 !== ';') {
decodeResult_1 = replaceInput_1;
}
else {
var decodeResultByReference_2 = references[replaceInput_1];
if (decodeResultByReference_2) {
decodeResult_1 = decodeResultByReference_2;
}
else if (replaceInput_1[0] === '&' && replaceInput_1[1] === '#') {
var decodeSecondChar_2 = replaceInput_1[2];
var decodeCode_2 = decodeSecondChar_2 == 'x' || decodeSecondChar_2 == 'X'
? parseInt(replaceInput_1.substr(3), 16)
: parseInt(replaceInput_1.substr(2));
decodeResult_1 =
decodeCode_2 >= 0x10ffff
? outOfBoundsChar
: decodeCode_2 > 65535
? surrogate_pairs_1.fromCodePoint(decodeCode_2)
: fromCharCode(numeric_unicode_map_1.numericUnicodeMap[decodeCode_2] || decodeCode_2);
}
}
replaceResult_1 += decodeResult_1;
replaceLastIndex_1 = replaceMatch_1.index + replaceInput_1.length;
} while ((replaceMatch_1 = decodeRegExp.exec(text)));
if (replaceLastIndex_1 !== text.length) {
replaceResult_1 += text.substring(replaceLastIndex_1);
}
}
else {
replaceResult_1 =
text;
}
return replaceResult_1;
}
document
function(contents) {
return htmlParser.parse(contents);
}
numeral
function(value) {
var value = numeral(value);
if (app.has(value) && app.has(value._value)) return value._value;
}
parse
function(element, type) {
if (!app.has(type)) type = "text";
if (app.has(element)) {
var html = element.innerHTML;
if (type === "text") return mod.strip(html);
if (type === "number") return mod.numeral(mod.strip(html));
return html;
}
return false;
}
strip
function(html, clean) {
if (!app.has(clean)) clean = true;
html = html.replace(/