Last active
January 21, 2025 13:34
-
-
Save jakubfiala/185f7275ecf971c02bab5a70112c4c62 to your computer and use it in GitHub Desktop.
js functions that i need often and i am NOT making another npm package, fuck that
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
// like python's range, yields numbers from start to end (not inclusive) | |
export const range = function* (start, end, step = 1) { | |
let index = start; | |
while (index < end) { | |
yield index; | |
index++; | |
} | |
} | |
// like python's enumerate, yields [index, item] | |
export function* enumerate(iterable) { | |
let count = 0; | |
for (let item of iterable) { | |
yield [count++, item]; | |
} | |
} | |
// returns a randomly selected item from a given array | |
export const choose = list => list[Math.floor(Math.random() * list.length)]; | |
// converts degress to radians | |
export const rad = x => (x * Math.PI) / 180; | |
// converts radians to degrees | |
export const deg = x => (x * 180) / Math.PI; | |
// aint installing no lodash | |
// https://stackoverflow.com/a/27078401 | |
export const throttle = (callback, wait) => { | |
let go = true; | |
return () => { | |
if (go) { | |
go = false; | |
setTimeout(() => { | |
go = true; | |
callback.call(); | |
}, wait); | |
} | |
}; | |
}; | |
// returns the sum of numbers in the given array | |
export const sum = (vector) => { | |
let s = 0; | |
for (var i = 0; i < vector.length; i++) { | |
s += vector[i]; | |
} | |
return s; | |
}; | |
// returns the mean of numbers in the given array | |
export const mean = (vector) => { | |
return sum(vector) / vector.length; | |
}; | |
// returns the variance of numbers in the given array | |
export const variance = (vectorMean, vector) => { | |
let s = 0; | |
for (let i = 0; i < vector.length; i++) { | |
s += (vector[i] - vectorMean) ** 2; | |
} | |
return s; | |
} | |
/** | |
* Standard deviation with N-1 normalisation factor | |
* which matches Matlab's `std` with the default `w` parameter | |
* | |
* @param {Array} vectorMean mean of the provided vector (provided separately for modularity) | |
* @param {Array} vector the vector itself | |
*/ | |
export const stddev = (vectorMean, vector) => { | |
return Math.sqrt(variance(vectorMean, vector) / (vector.length - 1)); | |
} | |
export const scale = (value, smin, smax, tmin, tmax) => (value - smin) * (tmax - tmin) / (smax - smin) + tmin; | |
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | |
export const debounce = (func, wait, immediate) => { | |
let timeout; | |
return function () { | |
const later = () => { | |
timeout = null; | |
if (!immediate) func.apply(this, arguments); | |
}; | |
const callNow = immediate && !timeout; | |
clearTimeout(timeout); | |
timeout = setTimeout(later, wait); | |
if (callNow) func.apply(this, arguments); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment