Skip to content

Instantly share code, notes, and snippets.

@fauxsaurus
fauxsaurus / asyncMap.js
Last active November 30, 2019 21:08
Run array map functions sequentially & asynchronously.
const asyncMap=(arr,cb)=>arr.reduce(async (arr,...args)=>[...(await arr),await cb(...args)],[])
const wait=async ms=>
new Promise(res=>
setTimeout(()=>
{
const newVal=ms+"ms";
console.log(newVal);
res(newVal);
}, ms)
)
@fauxsaurus
fauxsaurus / curry.js
Last active October 13, 2018 16:18
one line curry function
const curry=(fn,...xs)=>xs.length>=fn.length?fn(...xs):(...ys)=>curry(fn,...xs,...ys)
@fauxsaurus
fauxsaurus / callback2promise.js
Last active November 28, 2018 21:31
Callback to promise
function callback2promise(func,...args)
{
return new Promise(function(res,rej)
{
func(...args,(err,data)=>err?rej(err):res(data))
})
}
@fauxsaurus
fauxsaurus / center.css
Created December 3, 2018 21:49
Vertically & Horizontally centered text.
.parent
{
align-items:center;
display:flex;
justify-content:center;
}
@fauxsaurus
fauxsaurus / wait.js
Created June 25, 2019 16:11
A simple wait/sleep command in JavaScript.
await new Promise(done=>setTimeout(done,seconds*1000))
//note: replace "seconds" with a number to use this
//hint: this will only work inside an async function
@fauxsaurus
fauxsaurus / colorblind-gradients.html
Last active May 26, 2021 02:58
A visual representation of different types of colorblindness & their affects on color gradients (filters adapted from this awesome repo at https://github.com/oftheheadland/Colorblindly).
<!Doctype html>
<style>
div
{
background:linear-gradient(to right,
#f00 0%,
#ff0 16.66%,
#0f0 33.33%,
#0ff 50%,
#00f 66.66%,
.hue-gradient
{
background:linear-gradient(to right,
#f00 0%,
#ff0 16.66%,
#0f0 33.33%,
#0ff 50%,
#00f 66.66%,
#f0f 83.33%,
#f00 100%
@fauxsaurus
fauxsaurus / space-case.js
Created January 24, 2020 01:27
Convert PascalCase, camelCase, snake_case, & kebab-case into "space case".
export const spaceCase = txtWithNoSpaces =>
txtWithNoSpaces
.split(/_|-/g) // snake_case & kebab-case
.join(' ')
.replace(/([A-Z])/g, ' $1') // PascalCase & camelCase
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ')
.replace(/\s+/, ' ') // dashed entries create two spaces, bump that down to one
.replace(/([A-Z])\s(?=[A-Z]\b)/g, '$1') // e.g. "U U I D" => "UUID"
/\
_\/_/
/ /
/\
/ /
import {useState} from 'react';
export const useVector = <T>(initialValue: T, arity = 3) => {
const [vector, setVector] = useState<T[]>([...Array(arity)].fill(initialValue));
const updateVector = (newValue: T) => setVector([newValue].concat(vector.slice(1)));
return [vector, updateVector] as const;
};