Created
March 13, 2018 05:12
-
-
Save haingdc/44fa1f5424bde0177528b077798a01c7 to your computer and use it in GitHub Desktop.
A Gentle Introduction to Functional JavaScript: Part 3
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js'></script> | |
<script> | |
var poem = 'Twas brillig, and the slithy toves\n' + | |
'Did gyre and gimble in the wabe;\n' + | |
'All mimsy were the borogoves,\n' + | |
'And the mome raths outgrabe.'; | |
var replace = _.curry(function (find, replacement, str) { // 🎭 | |
var regex = new RegExp(find, 'g'); | |
return str.replace(regex, replacement); | |
}); | |
var wrapWith = _.curry(function (tag, str) { // 🎭 | |
return '<' + tag + '>' + str + '</' + tag + '>'; | |
}); | |
var modifyPoem = _.flow( // compose ≈ flow trong Lodash | |
replace('brillig', wrapWith('em', 'four o’clock in the afternoon')), | |
replace('\n', '<br/>\n'), | |
wrapWith('p'), | |
wrapWith('blockquote'), | |
); | |
console.log(modifyPoem(poem)); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment