Skip to content

Instantly share code, notes, and snippets.

@zerobias
Last active June 5, 2017 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerobias/aa6c6c3beea08df1dda8dbcd5fe4a386 to your computer and use it in GitHub Desktop.
Save zerobias/aa6c6c3beea08df1dda8dbcd5fe4a386 to your computer and use it in GitHub Desktop.
Merge teplate function arguments into one list
function mergeTemplateArgs(strings, ...values) {
const stringsLn = strings.length
const hasValues = stringsLn > 1
if (!hasValues) return strings
const pairsCount = stringsLn - 1
const fullLength = pairsCount * 2 + 1
const result = Array(fullLength)
result[fullLength -1] = strings[stringsLn-1]
for (let i = 0, j = 0; i < pairsCount; i++, j+=2) {
result[j] = strings[i]
result[j+1] = values[i]
}
return result
}
// USAGE
mergeTemplateArgs`x = ${2}+${3}`
// => ["x = ", 2, "+", 3, ""]
mergeTemplateArgs``
// => [""]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment