Last active
May 31, 2018 06:39
-
-
Save ciffelia/760a5ddf23411afe2ffeab9b6d26148d to your computer and use it in GitHub Desktop.
stripMargin.js
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
const stripMargin = (string, marginPrefix = '|') => { | |
const lines = string.replace(/^\n|\n+$/g, '').split('\n'); | |
let result = ''; | |
lines.forEach((val, i) => { | |
const indexOfMarginPrefix = val.indexOf(marginPrefix); | |
if(indexOfMarginPrefix !== -1) { | |
result += val.slice(indexOfMarginPrefix + 1) + '\n'; | |
} | |
}); | |
return result; | |
}; |
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
console.log(stripMargin(` | |
|Tell me and I forget. | |
|Teach me and I remember. | |
|Involve me and I learn. | |
|(Benjamin Franklin) | |
`)); | |
console.log(stripMargin(` | |
>Tell me and I forget. | |
>Teach me and I remember. | |
>Involve me and I learn. | |
>(Benjamin Franklin) | |
`, '>')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment