Last active
October 31, 2016 19:27
-
-
Save DukeyToo/ba13dbca527f257a6c59 to your computer and use it in GitHub Desktop.
Optimized filter in es6 for decoding html values for angularjs when displaying in input or options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//simple filter to decode html-encoded values, for display in options or inputs | |
//optimized so that it doesn't redo the expensive decoding every time | |
myApp.filter('decoded', function() { | |
"use strict"; | |
let e = null; //only init as-needed, and keep around | |
let cache = {}; | |
function htmlDecode(input) { | |
if (cache[input]) { | |
//console.log('using cached', input, cache[input]) | |
return cache[input]; | |
} | |
if (e === null) | |
e = document.createElement('div'); | |
e.innerHTML = input; | |
let result = e.childNodes[0].nodeValue; | |
cache[input] = result; | |
return result; | |
} | |
return function(input) { | |
return htmlDecode(input); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment