Skip to content

Instantly share code, notes, and snippets.

@Pritoj
Last active March 11, 2018 09:07
Show Gist options
  • Save Pritoj/9edf639d19f9c8f93c16a921ef235d51 to your computer and use it in GitHub Desktop.
Save Pritoj/9edf639d19f9c8f93c16a921ef235d51 to your computer and use it in GitHub Desktop.
Array extensions inspired by this tweet https://twitter.com/AntJanus/status/971634313136451584
// Array hug, one array hugs the other
// If the hugger is odd numbered, more in the front
Array.prototype.hug = function(arr){
let hugged = this.slice(0,Math.ceil(this.length/2));
hugged.push(...arr);
hugged.push(...this.slice(Math.ceil(this.length/2), this.length));
return hugged;
}
// Array spoon, one array spoons the other
// If the spooner is odd numbered, more in the back
Array.prototype.spoon = function(arr){
let hugged = this.slice(0,Math.floor(this.length/2));
hugged.push(...arr);
hugged.push(...this.slice(Math.floor(this.length/2), this.length));
return hugged;
}
// Array kiss, alias for concat
Array.prototype.kiss = Array.prototype.concat
// Array procreate, picket from this tweet
// https://twitter.com/insideouslyapt/status/971764778401181696
Array.prototype.procreate = function(other){
return this.slice(0, Math.min(this.length, other.length))
.reduce((family, x, i) => {
family.push(this[i])
family.push(other[i])
return family
}, [])
}
// Array wink, like a pop but doesn't remove the value
Array.prototype.wink = function(){
return this[this.length-1];
}
//Array peek, Like shift but doesn't remove the value
Array.prototype.peek = function(){
return this[0];
}
// Array flash, alias to length
// As requested here https://twitter.com/TURDSTOMPER/status/971761659315240960
Array.prototype.flash = function(){
return this.length
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment