Skip to content

Instantly share code, notes, and snippets.

@lujanfernaud
Last active August 18, 2018 08:02
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 lujanfernaud/e054eeb98dfcfa7c0d127405dfe2a750 to your computer and use it in GitHub Desktop.
Save lujanfernaud/e054eeb98dfcfa7c0d127405dfe2a750 to your computer and use it in GitHub Desktop.
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