Skip to content

Instantly share code, notes, and snippets.

@errietta
Created March 29, 2019 08:47
Show Gist options
  • Save errietta/1197949aa1fbf09764bb7ddb8358cf10 to your computer and use it in GitHub Desktop.
Save errietta/1197949aa1fbf09764bb7ddb8358cf10 to your computer and use it in GitHub Desktop.
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