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
// jdn conversions for date arithmetic/comparison | |
const intDiv = (l,r) => ~~(l/r); // integer division | |
const jdnFromIso = iso => { | |
const [y, m, d] = iso.split('-').map(s => parseInt(s, 10)); | |
return intDiv(1461 * (y + 4800 + intDiv(m - 14, 12)), 4) | |
+ intDiv(367 * (m - 2 - 12 * intDiv(m - 14, 12)), 12) | |
- intDiv(3 * intDiv(y + 4900 + intDiv(m - 14, 12), 100), 4) | |
+ d - 32075; | |
}; |
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
useEffect(() => { | |
const pFoo = getFoo().then(foo => /* do somthing with our async foo, making sure to return it */); | |
return () => pFoo.then(foo => /* cleanup here will always run after we have gotten our foo and used it */); | |
}); | |
// This is one of the few cases where it makes sense to use raw promises instead of async/await | |
// The power of promises is that you can compose asynchronous logic asynchronously | |
// Afaik that's not possible with async/await? |
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
// f of the form v => v | |
export const objMap = (obj, f) => | |
Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, f(v)])); | |
// f of the form ([k, v]) => [k, v] | |
export const objMapEntries = (obj, f) => | |
Object.fromEntries(Object.entries(obj).map(f)); | |
export const arrDeepMap = (arr, f) => arr.map((x, i, a) => Array.isArray(x) ? arrDeepMap(x, f) : f(x, i, a)); |
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
// f of the form (k, v) => v | |
export const objMap = (obj, f) => | |
Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, f(k, v)])); | |
// f of the form ([k, v]) => [k, v] | |
export const objMapEntries = (obj, f) => | |
Object.fromEntries(Object.entries(obj).map(f)); |
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 default async n => new Promise((res, rej) => window.setTimeout(() => res(), n)) |
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 const debounce = (f, delay) => { | |
let timer = null | |
return (...args) => { | |
const execute = () => { | |
timer = null | |
f(...args) | |
} | |
if (timer !== null) |
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 const unique = (arr, eq) => arr.reduce( | |
(acc, item) => (eq ? acc.find(a => eq(a, item)) !== undefined : acc.includes(item)) | |
? acc | |
: [...acc, item] | |
, []) |
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
const imSet = (objOrArray, path, valueOrFunc) => { | |
const multipath = Array.isArray(path) && path.length > 1 | |
const pathItem = Array.isArray(path) ? path[0] : path // unwrap single item array | |
const isFunc = typeof valueOrFunc === "function" | |
const value = multipath | |
? imSet(objOrArray[pathItem], path.slice(1), valueOrFunc) | |
: isFunc ? valueOrFunc(objOrArray[pathItem]) : valueOrFunc | |
if (Array.isArray(objOrArray)) { |
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
class FooPage { | |
async loadData () { | |
const data = await getData() | |
this.setState({ data }) | |
} | |
render () { | |
return ( | |
<Page data={this.state.data} loadData={this.loadData}> |
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
const data = { | |
people: [ | |
{ | |
id: 0, | |
name: 'John', | |
bankAccount: { id: 0, balance: 2864 }, | |
address: { id: 0, number: 83, street: 'Foo St' }, | |
}, | |
{ | |
id: 1, |
NewerOlder