Last active
February 21, 2019 02:45
-
-
Save Knovour/cf1bb3f99f630e105f3bedea3c59078b to your computer and use it in GitHub Desktop.
Some JS snippets collection
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
// 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)) | |
} |
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
// 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