Created
May 10, 2018 15:41
-
-
Save techanon/622fb6f942c8324991241f1cbe7a0cba to your computer and use it in GitHub Desktop.
snippets for quickly zipping either multiple arrays or multiple strings.
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
// Licence: Public domain | |
function zipArray(...params) { | |
for (let i in params) | |
if (!Array.isArray(params[i])) | |
throw new Error(`All parameters to be zipped MUST be an array. Parameter ${i} is not.`); | |
let out = [], count = Math.max(...params.map(i=>i.length)); | |
for (let i = 0; i < count; i++) { | |
for (let param of params) { | |
if (i < param.length) out.push(param[i]); | |
} | |
} | |
return out; | |
} | |
function zipString(...params) { | |
for (let i in params) | |
if (typeof params[i] != 'string') | |
throw new Error(`All parameters to be zipped MUST be an string. Parameter ${i} is not.`); | |
let out = "", count = Math.max(...params.map(i=>i.length)); | |
for (let i = 0; i < count; i++) { | |
for (let param of params) { | |
if (i < param.length) out += param.slice(i,1); | |
} | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment