Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript Utilities

JavaScript Utilities

Note: Using StandardJS rules.

Range

function * range(start, end, step = 1) {
  while (start <= end) {
    yield start
    start += step
  }
}

Array.from(range(1, 10)) //=> [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

Rotate Array (Left and Right)

function rotate(array, n) {
  while (n > array.length) { n = n - array.length }
  while (n < 0) { n = array.length - (n * -1) }

  array.push.apply(array, array.splice(0, n))

  return array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.