This file contains hidden or 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
// 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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
/* | |
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 |
This file contains hidden or 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
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: { |
This file contains hidden or 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
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' } |
This file contains hidden or 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 intersectObjHash = ( a, b ) => { | |
const hash = {} | |
for ( let i = 0; i < b.length; i++ ) { | |
hash[ b[ i ] ] = true | |
} | |
return a.filter( el => hash[ el ] ) | |
} |
This file contains hidden or 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 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, () => [] ) |
This file contains hidden or 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 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] |
This file contains hidden or 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 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( |
NewerOlder