Skip to content

Instantly share code, notes, and snippets.

@max-lt
Created May 14, 2017 15:42
Show Gist options
  • Save max-lt/bb9b36f75f043639f43c643002ccd5ac to your computer and use it in GitHub Desktop.
Save max-lt/bb9b36f75f043639f43c643002ccd5ac to your computer and use it in GitHub Desktop.
Circular shift on a javascript array
// performs a circular shift on an array (i < 0 ? right : left)
Object.defineProperty(Array.prototype, 'rotate', {
enumerable: false,
value: function (i) {
if (i == 0) return this;
const s = (this.splice).apply(this, i < 0 ? [i] : [0, i]);
(i < 0 ? this.unshift : this.push).apply(this, s);
return this;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment