Skip to content

Instantly share code, notes, and snippets.

View queviva's full-sized avatar

queviva

View GitHub Profile
@queviva
queviva / gate.js
Created January 26, 2022 00:02
gate value between values
const gate = (...v) => v.sort((a, b) => a - b)[1];
@queviva
queviva / XbetweenYandZ.js
Created January 31, 2022 05:19
method that quickly tells you if a value, x, is between any two other unknown values, y and z
const XbetweenYandZ = (x, y, z) => (x-y) * (x-z) < 0;
@queviva
queviva / randIntBetween.js
Last active January 31, 2022 22:16
returns a random integer between two other values
const randIntBetween = (x, y) => ~~(Math.random() * (x - y) ) + x
@queviva
queviva / xModByM.js
Last active January 31, 2022 22:37
returns a value of x, cyclically-modulated by M
const xModByM = (x, M) => ((x % M) + M) % M;
@queviva
queviva / Liz.js
Created February 1, 2022 09:24
add|remove the same listener for multiple events
const Liz = {
lizzers: [
[obj, 'event', e=>{...}],
[obj, 'event,event,...', e=>{...}],
[obj, 'event,event,...', e=>{...},{ passive: true }]
],
@queviva
queviva / zeroPad.js
Created February 3, 2022 18:16
front pads number with zero
('00000' + n).slice(-3)
@queviva
queviva / cssVars.js
Last active February 4, 2022 07:44
create list of all css variables
// run after all styles load
const cssVars = {
list : () => [...new Set([...[
[...document.styleSheets]
.map(s => [...s.cssRules]
.map(r => r.cssText)),
[...document.querySelectorAll('[style]')]
@queviva
queviva / toNumArray.js
Last active February 5, 2022 21:18
converts a string into an array of numbers
const toNumArray = s => s.match(/[\d\.-]+/g).map(p=>Number(p));
@queviva
queviva / sliceArrayOnCondition.js
Created February 10, 2022 23:21
slices an array based either on the condition of an element, or just uniformly by quantity
const sliceArrayOnCondition=(a,f)=>a.reduce((x,v,i)=>((isNaN(f)?f(v):i%f===0)&&i!==0?x.push([v]):x.at(-1).push(v),x),[[]]);
@queviva
queviva / cryptoFloats.js
Last active July 9, 2022 23:32
random floats from zero-to-one using crypto.getRandomValues()
const crandomValue = () => [...crypto.getRandomValues(new Uint32Array(1))].map(v => v / 4294967296.5)[0];
const crandomArray = (n=1) => [...crypto.getRandomValues(new Uint32Array(n))].map(v => v / 4294967296.5);