Skip to content

Instantly share code, notes, and snippets.

@Pritoj
Created March 9, 2018 05:42
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 Pritoj/1c6cd79b9eeb647866920c27ca187f33 to your computer and use it in GitHub Desktop.
Save Pritoj/1c6cd79b9eeb647866920c27ca187f33 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment