Last active
May 24, 2023 13:38
-
-
Save supahfunk/bdb010aa80aa42d3a71fffeae929163d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 const mapRange = (num, inMin, inMax, outMin, outMax) => { | |
return (num - inMin) * (outMax - outMin) / (inMax - inMin) + outMin | |
} | |
export const newMapRange = (value, rangeA, rangeB, limit) => { | |
if (limit == null) { | |
limit = false; // eslint-disable-line | |
} | |
const [fromLow, fromHigh] = Array.from(rangeA) | |
const [toLow, toHigh] = Array.from(rangeB) | |
const result = toLow + ((value - fromLow) / (fromHigh - fromLow)) * (toHigh - toLow) | |
if (limit === true) { | |
if (toLow < toHigh) { | |
if (result < toLow) { | |
return toLow | |
} | |
if (result > toHigh) { | |
return toHigh | |
} | |
} else { | |
if (result > toLow) { | |
return toLow | |
} | |
if (result < toHigh) { | |
return toHigh | |
} | |
} | |
} | |
return result | |
} | |
export const lerp = (a, b, n) => (1 - n) * a + n * b | |
export const range = (value, min, max) => Math.min(Math.max(value, min), max) | |
export const shuffle = (array) => { | |
let currentIndex = array.length | |
let temporaryValue | |
let randomIndex | |
while (currentIndex !== 0) { | |
randomIndex = Math.floor(Math.random() * currentIndex) | |
currentIndex -= 1 | |
temporaryValue = array[currentIndex] | |
array[currentIndex] = array[randomIndex] | |
array[randomIndex] = temporaryValue | |
} | |
return array | |
} | |
export const chunk = (array, size) => { | |
const chunked = [] | |
for (let i = 0; i < array.length; i += 1) { | |
const last = chunked[chunked.length - 1] | |
if (!last || last.length === size) { | |
chunked.push([array[i]]) | |
} else { | |
last.push(array[i]) | |
} | |
} | |
return chunked | |
} | |
export const avarage = (nums) => { | |
if (nums.length === 0) return 0 | |
return (nums.reduce((a, b) => (a + b)) / nums.length).toFixed(2) | |
} | |
export const dividedByTen = (num) => { | |
const n = num + Math.random() - 0.5 | |
return Math.min(1, Math.max(0.01, (n * 0.1).toFixed(2))) | |
} | |
export const randomLow = () => { | |
return parseFloat((Math.random() * 0.2 + 0.01).toFixed(2)) | |
} | |
export const setMinMaxValue = (e) => { | |
const value = Math.max(0, Math.min(e.target.value, e.target.max)).toString() | |
const { length } = value | |
if (length === 1) { | |
return `0${value}` | |
} | |
return value | |
} | |
export const setZeroValue = (e) => { | |
const value = e.toString() | |
const { length } = value | |
if (length === 1) { | |
return `0${value}` | |
} | |
return value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment