Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"es6 & beyond": ch2, "tagged template literals", cover explicit multiline string literals #539

Open
getify opened this issue Sep 21, 2015 · 0 comments

Comments

@getify
Copy link
Owner

getify commented Sep 21, 2015

Illustrate a tagged template literal function that makes multiline string literals require explicit linebreaks instead of assuming them.

function multiline(strings,...values) {
   return Function(`return "${strings.raw.join("").replace(/\n/g,"") }"`)();
}
// TODO: fix this so it adds in the interpolated values
//    (but does not strip linebreaks in those values!)
// TODO: also, don't use the `Function(..)` option; just find
//    actual "\n" occurrences in the raw strings and make them
//    into linebreaks

// WIP:
// function multiline(strings,...values) {
//  var strs = strings.raw.map(function mapper(v){
//      return v.replace(/\n/g,"")
//          .replace(/(^|[^\\]|(?:\\\\)+)((?:\\n)+)/g,function(match,p1,p2){
//              return `${p1}${p2.replace(/\\n/g,"\n")}`;
//          });
//  });
//  return strs.join("");
// }```

```js
console.log( `hello

 world` );
// hello
// 
//  world
console.log( multiline`hello

 world` );
// hello world
console.log( multiline`hello\n
\n
 world` );
// hello
//
//  world
@getify getify changed the title "es6 & beyond": ch2, "template literals", cover explicit multiline string literals "es6 & beyond": ch2, "tagged template literals", cover explicit multiline string literals Sep 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Second Edition
ES6 & Beyond
Development

No branches or pull requests

1 participant