Skip to content

Commit

Permalink
blear.utils.string
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed May 22, 2016
1 parent 7cecb8d commit eafeb9e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blear.utils.string",
"version": "1.0.2",
"version": "1.0.3",
"description": "string utils",
"scripts": {
"live": "browser-sync start --config bs-config.js",
Expand Down
42 changes: 30 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,28 @@ var access = require('blear.utils.access');
var reHump = /[A-Z]/g;
var reSep = /[-_ ]([a-z])/g;
var reRegExp = /[.*+?^=!:${}()|[\]\/\\-]/g;
var reLessThan = /</g;
var escapeHTMLMap = {
'&amp;': /&/g,
'&lt;': /</g,
'&gt;': />/g,
'&quot;': /"/g,
'&apos;': /'/g,
'&#x2f;': /\//g
};
/**
* 解码 html 实体符 map
* @type {Object}
*/
var unescapeHTMLMap = {
'&': /&amp;/g,
'<': /&lt;/g,
'>': /&gt;/g,
'"': /&quot;/g,
'\'': /&apos;/g,
'/': /&#x2f;/g
};
var reEscape = /&#(x)?([\w\d]{0,5});/ig;
var reAssignVarible = /\$\{([^{}]*?)}/g;
var pEl = document.createElement('p');


/**
Expand Down Expand Up @@ -137,14 +156,7 @@ exports.padEnd = function (str, maxLength, char) {
};


var escapeHTMLMap = {
'&amp;': /&/g,
'&lt;': /</g,
'&gt;': />/g,
'&quot;': /"/g,
'&apos;': /'/g,
'&#x2f;': /\//g
};


/**
* 编码字符串为 html 实体符
Expand All @@ -167,9 +179,15 @@ exports.escapeHTML = function (str) {
* @returns {string|string}
*/
exports.unescapeHTML = function (str) {
pEl.innerHTML = (str + '').replace(reLessThan, '&lt;');
str = str.replace(reEscape, function (full, hex, code) {
return String.fromCharCode(parseInt(code, hex ? 16 : 10));
});

return pEl.innerText || pEl.textContent || '';
object.each(unescapeHTMLMap, function (to, reg) {
str = str.replace(reg, to);
});

return str;
};


Expand Down

0 comments on commit eafeb9e

Please sign in to comment.