Skip to content

Instantly share code, notes, and snippets.

@DukeyToo
Last active October 31, 2016 19:27
Show Gist options
  • Save DukeyToo/ba13dbca527f257a6c59 to your computer and use it in GitHub Desktop.
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
//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