Created
May 14, 2017 15:42
-
-
Save max-lt/bb9b36f75f043639f43c643002ccd5ac to your computer and use it in GitHub Desktop.
Circular shift on a javascript array
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
// 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