Last active
December 14, 2022 02:32
-
-
Save Cicolas/449a05497ef661ae09a4afb10b843a3c to your computer and use it in GitHub Desktop.
utils functions js
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
export function getInRandomList(list) { | |
var s; | |
do { | |
s = list[Math.floor(Math.random() * 1000) % list.length]; | |
} while (s[1]) | |
return s; | |
} | |
export function Clamp(num, min, max) { | |
return Math.min(Math.max(num, min), max) | |
}; | |
export function InverseLerp(min, max, num) { | |
return Clamp((num - min) / (max - min), 0, 1); | |
} | |
export function pullToTop(arr, i) { | |
const item = arr.splice(i, 1)[0]; | |
arr.unshift(item); | |
} | |
export function pushToBottom(arr) { | |
const item = arr.shift(); | |
arr.push(item); | |
} | |
export function formatNumber(number, minDec, maxDec) { | |
return number.toLocaleString(undefined, { | |
minimumFractionDigits: minDec, | |
maximumFractionDigits: maxDec | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment