Skip to content

Instantly share code, notes, and snippets.

@Knovour
Last active February 21, 2019 02:45
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 Knovour/cf1bb3f99f630e105f3bedea3c59078b to your computer and use it in GitHub Desktop.
Save Knovour/cf1bb3f99f630e105f3bedea3c59078b to your computer and use it in GitHub Desktop.
Some JS snippets collection
// Base on https://gist.github.com/kujon/2781489
const clamp = (val, min, max) => {
const realMin = max > min ? min : max
const realMax = max > min ? max : min
return Math.max(realMin, Math.min(realMax, val))
}
// Basic
// Array.range(12) -> [ 0, 1, 2, 3, ..., 11 ]
Array.range = length => [ ...Array(length).keys() ]
// With specific range
// Array.range(1, 12) -> [ 1, 2, 3, ..., 11 ]
Array.range = [ ...Array(to - from).keys() ].map(v => from + v)
// Another way
Array.range = (from, to) => Array.from({ length: to - from }, (v, idx) => idx + from)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment