Last active
June 5, 2017 14:10
-
-
Save zerobias/aa6c6c3beea08df1dda8dbcd5fe4a386 to your computer and use it in GitHub Desktop.
Merge teplate function arguments into one list
This file contains hidden or 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
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