Created
January 20, 2016 08:49
-
-
Save LucaColonnello/04bd8a91b20f1e3a60d2 to your computer and use it in GitHub Desktop.
String leading whitespaces
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
// Strip margin (Leading whitespaces) | |
function pipeFactory( separator = ' ' ){ | |
return (s, ...args) => { | |
return s | |
.map(( v, i ) => v + ( args[i] || '') ) | |
.join('') | |
.replace(/(\r\n|\r|\n)([\s]+)?\|/g, separator) | |
; | |
}; | |
} | |
const pipe = pipeFactory(); | |
const pipeml = pipeFactory('\n'); | |
const a = 5; | |
const b = 10; | |
const c = 15; | |
// test normal pipe | |
console.log(pipe`test normal: | |
|test${a} | |
|test${b} | |
|test${c} | |
test`) | |
// test pipe multiline | |
console.log(pipeml`test multiline: | |
|test${a} | |
|test${b} test${c} | |
test`) |
I'm going to fork it and use it in production
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fantastic. Good job!