decode-html
Advanced tools
Comparing version 1.0.2 to 2.0.0
33
index.js
@@ -0,14 +1,23 @@ | ||
// Store markers outside of the function scope, | ||
// not to recreate them on every call | ||
var entities = { | ||
'amp': '&', | ||
'apos': '\'', | ||
'lt': '<', | ||
'gt': '>', | ||
'quot': '"', | ||
'nbsp': '\xa0' | ||
}; | ||
var entityPattern = /&([a-z]+);/ig; | ||
module.exports = function decodeHTMLEntities(text) { | ||
var replacements = [ | ||
['amp', '&'], | ||
['apos', '\''], | ||
['lt', '<'], | ||
['gt', '>'] | ||
]; | ||
replacements.forEach(function(replace){ | ||
text = text.replace(new RegExp('&'+replace[0]+';', 'g'), replace[1]); | ||
// A single replace pass with a static RegExp is faster than a loop | ||
return text.replace(entityPattern, function(match, entity) { | ||
entity = entity.toLowerCase(); | ||
if (entities.hasOwnProperty(entity)) { | ||
return entities[entity]; | ||
} | ||
// return original string if there is no matching entity (no replace) | ||
return match; | ||
}); | ||
return text; | ||
}; | ||
}; |
{ | ||
"name": "decode-html", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "decode html entities", | ||
"main": "index.js", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"tape": "^4.5.1" | ||
}, | ||
"scripts": { | ||
@@ -9,0 +11,0 @@ "test": "node test.js" |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3349
35
1