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 / 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' }
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, () => [] )
@nrkn
nrkn / asm.js
Created October 7, 2019 06:17
Minimal instruction set
const add = (memory, dest, src) => {
memory[dest] += memory[src]
}
const addV = (memory, dest, value) => {
memory[dest] += value
}
const sub = (memory, dest, src) => {
memory[dest] -= memory[src]
@nrkn
nrkn / object-filter.js
Created October 3, 2019 01:00
object filter
export const objectFilter = ( obj, predicate ) => {
if ( obj === null || obj === undefined )
throw Error( 'Cannot convert null or undefined to an object' )
if ( typeof predicate !== 'function' )
throw Error( 'Expected predicate to be a function' )
obj = Object( obj )
return Object.keys( obj ).reduce(