Created
May 27, 2019 15:03
-
-
Save bartcis/59c070f241c285c2bddc71cb110f2685 to your computer and use it in GitHub Desktop.
HTML entities converter - solution 1
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
| // 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 = '<'; | |
| break; | |
| case '&': | |
| a = '&'; | |
| break; | |
| case '>': | |
| a = '>'; | |
| break; | |
| case '"': | |
| a = '"'; | |
| break; | |
| case "'": | |
| a = '''; | |
| 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