Skip to content

Instantly share code, notes, and snippets.

View JonathonAshworth's full-sized avatar

Jonathon Ashworth JonathonAshworth

  • Everest Engineering
  • Melbourne, Australia
View GitHub Profile
// 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;
};
@JonathonAshworth
JonathonAshworth / asyncCleanup.jsx
Last active August 9, 2022 03:00
Cleaning up asynchronous render side effects
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?
@JonathonAshworth
JonathonAshworth / utils.js
Last active November 4, 2023 13:08
Collection of JS Utility Functions
// 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));
@JonathonAshworth
JonathonAshworth / object.js
Last active January 5, 2020 02:56
Object Utilities
// 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));
@JonathonAshworth
JonathonAshworth / wait.js
Created November 22, 2019 00:25
Wait function (can be awaited)
export default async n => new Promise((res, rej) => window.setTimeout(() => res(), n))
@JonathonAshworth
JonathonAshworth / debounce.js
Created November 22, 2019 00:21
Function debounce
export const debounce = (f, delay) => {
let timer = null
return (...args) => {
const execute = () => {
timer = null
f(...args)
}
if (timer !== null)
@JonathonAshworth
JonathonAshworth / unique.js
Created November 22, 2019 00:20
Array unique filter
export const unique = (arr, eq) => arr.reduce(
(acc, item) => (eq ? acc.find(a => eq(a, item)) !== undefined : acc.includes(item))
? acc
: [...acc, item]
, [])
@JonathonAshworth
JonathonAshworth / imSet.js
Last active September 19, 2019 06:21
Immutable Set
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)) {
class FooPage {
async loadData () {
const data = await getData()
this.setState({ data })
}
render () {
return (
<Page data={this.state.data} loadData={this.loadData}>
@JonathonAshworth
JonathonAshworth / expanded.js
Last active April 27, 2018 04:58
Expanded vs Flat data
const data = {
people: [
{
id: 0,
name: 'John',
bankAccount: { id: 0, balance: 2864 },
address: { id: 0, number: 83, street: 'Foo St' },
},
{
id: 1,