Skip to content

Instantly share code, notes, and snippets.

View nrkn's full-sized avatar

Nik nrkn

View GitHub Profile
@nrkn
nrkn / shared-promise-cache.ts
Created February 23, 2024 03:29
Shared promises, simpler version of cached-promises
export type ToSlugFunction<T extends any[]> = (...args: T) => string
export const sharedPromiseCache = <T extends any[], R>(
fn: (...args: T) => Promise<R>,
toSlug: ToSlugFunction<T> = (...args: T) => JSON.stringify(args)
): ((...args: T) => Promise<R>) => {
const cache = new Map<string, Promise<R>>()
return async (...args: T): Promise<R> => {
const key = toSlug(...args)
@nrkn
nrkn / cached-promises.ts
Last active December 6, 2023 01:39
Try to prevent cache stampedes when using promises
// try to prevent cache stampedes
//
// if you don't do this, then when we get a lot of simultaneous requests for the
// same thing and it's not cached yet, every single request will think it has
// to generate the resource, whereas using this makes it so that the very first
// one triggers the work, and the others either wait on the same promise, or get
// served the cached version if it's already resolved
//
// the effect of having every request try to generate something is to overload
// the server and cause weird side effects, like files not being read or written
@nrkn
nrkn / stringify.js
Last active July 19, 2023 08:24
Human readable json stringify where simple arrays use only one line
const isPrimitive = obj =>
obj === null ||
[ 'string', 'number', 'boolean' ].includes( typeof obj )
const isArrayOfPrimitive = obj =>
Array.isArray( obj ) && obj.every( isPrimitive )
const format = arr =>
`^^^[ ${
arr.map( val => JSON.stringify( val ) ).join( ', ' )
@nrkn
nrkn / kowloon-pixel-mapper.js
Created March 7, 2022 09:22
Kowloon Walled City Pixel Mapper for Scapeshop
const firstFloor = 0
const maxFloors = 12
const groundDensity = 0.7
const roofDensity = 0.8
export const pixelMapper = ({ r, g, b }) => {
if (r === 0 && g === 0 && b === 0) return []
const floor = r / 255
@nrkn
nrkn / pixel-mapper.js
Last active March 6, 2022 05:23
scapeshop pixel mapper example
/*
Your function should take a single data argument, which is an object:
// information about the image currently being sampled
{
// the color of the pixel
r: number, g: number, b: number, a: number,
// the sample location, relative to the source image
@nrkn
nrkn / index.ts
Created December 1, 2021 21:46
Runtime typing
import Ajv from 'ajv'
import { FromSchema } from 'json-schema-to-ts'
const productSchema = {
type: 'object',
properties: {
id: { type: 'number' },
name: { type: 'string' },
quantity: { type: 'number' },
type: {
@nrkn
nrkn / my-data.ts
Last active September 8, 2021 00:20
MyData
import {FromSchema} from 'json-schema-to-ts'
import Ajv from 'ajv'
const ajv = new Ajv()
const schema = {
type: 'object',
properties: {
foo: { type: 'integer' },
bar: { type: 'string' }
@nrkn
nrkn / index.ts
Last active May 25, 2020 08:52
TypeScript pattern matching with predicates
import { Shape } from './shapes.types'
import { area, perimeter } from './shapes.lib'
const shapes: Shape[] = [
{ radius: 4 }, { side: 5 }, { width: 6, height: 7 }
]
const totalArea = shapes.reduce(
( sum, shape ) => sum + area( shape ),
0
const intersectObjHash = ( a, b ) => {
const hash = {}
for ( let i = 0; i < b.length; i++ ) {
hash[ b[ i ] ] = true
}
return a.filter( el => hash[ el ] )
}
const createSequence = (length, cb) =>
Array.from({length}, ( _v, k ) => cb( k ))
// create array of characters a-z
const alphaLower = createSequence(
26, i => String.fromCharCode( i + 97 )
)
// create an array of empty arrays
const arr2d = createSequence( 10, () => [] )