Created
March 29, 2019 08:47
-
-
Save errietta/1197949aa1fbf09764bb7ddb8358cf10 to your computer and use it in GitHub Desktop.
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 qw = (strings, ...expr) => { | |
let words = []; | |
for (let i = 0; i < strings.length; i++) { | |
// all the words in the string part | |
let to_add = strings[i].split(/\s+/).filter(s => !!s); | |
//the previous was an expression AND we don't have a space aftr: | |
if (to_add.length && words.length && i > 0 && expr[i-1] && strings[i][0] !== ' ') { | |
words[words.length - 1] += to_add.pop(); | |
} | |
words.push(...to_add); | |
if (expr[i]) { | |
// the previous was a string ending in a space (or this is the first expression), push | |
if (!words.length || strings[i].substr(-1) === ' ') { | |
words.push(expr[i]); | |
} else { | |
// we want to append an expression to the string | |
words[words.length - 1] += expr[i]; | |
} | |
} | |
} | |
return words; | |
} | |
/* | |
let $totes_perl; | |
$totes_perl = qw`foo bar baz`; | |
console.log($totes_perl); | |
*/ | |
module.exports = qw; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment