Skip to content

Instantly share code, notes, and snippets.

@LucaColonnello
Created January 20, 2016 08:49
Show Gist options
  • Save LucaColonnello/04bd8a91b20f1e3a60d2 to your computer and use it in GitHub Desktop.
Save LucaColonnello/04bd8a91b20f1e3a60d2 to your computer and use it in GitHub Desktop.
String leading whitespaces
// 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`)
Copy link

ghost commented Jan 20, 2016

Fantastic. Good job!

@josieusa
Copy link

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