Skip to content

Instantly share code, notes, and snippets.

@bartcis
Created May 27, 2019 15:03
Show Gist options
  • Select an option

  • Save bartcis/59c070f241c285c2bddc71cb110f2685 to your computer and use it in GitHub Desktop.

Select an option

Save bartcis/59c070f241c285c2bddc71cb110f2685 to your computer and use it in GitHub Desktop.
HTML entities converter - solution 1
// Convert HTML characters
function convertHTMLBasic(string) {
// 1. Convert a string into array and check each element
let array = string.split('').map((a) => {
// 2. If an element match to defined HTML entity
// replace it with text eqivalent
switch (a) {
case '<':
a = '&lt;';
break;
case '&':
a = '&amp;';
break;
case '>':
a = '&gt;';
break;
case '"':
a = '&quot;';
break;
case "'":
a = '&apos;';
break;
}
return a;
});
// 3. Return array converted into a string
return array.join('');
}
console.time('Start Algo 1');
console.log(convertHTMLBasic(`>I love 'Rock & Roll'<`));
console.timeEnd('Start Algo 1'); // Start Algo 1: 0.5478515625ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment