Skip to content

Instantly share code, notes, and snippets.

@techanon
Created May 10, 2018 15:41
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 techanon/622fb6f942c8324991241f1cbe7a0cba to your computer and use it in GitHub Desktop.
Save techanon/622fb6f942c8324991241f1cbe7a0cba to your computer and use it in GitHub Desktop.
snippets for quickly zipping either multiple arrays or multiple strings.
// 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