Skip to content

Instantly share code, notes, and snippets.

@ukslim
ukslim / test-double-function.md
Created February 26, 2025 14:30
Testing a functional parameter two ways

Two ways of creating and asserting with a test double for a functional parameter

We are testing a function that takes a function as an argument. We don't need Jest to do its magic poke-in-replacements-for-imports magic. But we have the option of using jest.fn() to create a test double.

Example is in plain JS so that types don't clutter things.

Code under test (CUT):

HTTP Caching Lightning Summary

Why use cached data?

Speed and cost.

  • I'm a browser and the user returns to a photo page. Why spend time and network usage fetching it again?
  • I'm a proxy and a client has requested a static page I served another client recently. Why fetch it from the origin again?

Why not use cached data?

#!/usr/bin/env node
/*
My problem is that I have two networks - one in my house and one in my garden office, and I move
between them. In the office, my Mac can still see the house network, so it doesn't automatically switch
to the office network. It's strong enough for many things, but not for video conferencing. So unless I
remember to manually switch networks, I end up noticing when my colleagues tell me I'm breaking up.
This script exits early if we're on a network that isn't part of that scenario.
@ukslim
ukslim / refactor-from-ramda.md
Last active May 24, 2024 13:09
Refactoring from Ramda

Refactor away from Ramda

I love Ramda, but my organisation is falling away from it - we have hires who don't think in FP, and Typescript and Ramda fight somewhat.

You can coax Ramda code to work in Typescript by strategically adding as in the right places. But it's often quite hard work -- harder work than refactoring away from Ramda before converting the plain JS to TS.

Here's some patterns for refactoring away from Ramda. In most cases, applying any of these changes leaves you with less clear code than you started with. But you can now apply more refactorings - extract function, extract method, pulling up

@ukslim
ukslim / fpscaling.js
Last active March 4, 2024 09:57
Avoiding Floating Point errors by scaling up.
// TL:DR
// In JS (and most other languages using floating point)
// 0.3 - 0.2 != 0.1
// 0.6 + 0.3 != 0.9
// These might cause problems for you.
// But!
// (0.3 * 10) - (0.2 * 10) == 0.1 * 10 == 1
// (0.6 * 10) + (0.3 * 10) == 0.9 * 10 == 9
// Use this to your advantage.
// For example, if your get inputs in numbers representing ££.pp, multiply by 100 and do your maths in pennies.
/*
Find a 5 digit combination that meets these constraints:
x[0] * x[1] == 24
x[3] == x[1] / 3
x[3] + x[4] == x[0] + x[2]
sum(x) == 26
not all the digits are unique
*/
/*
@ukslim
ukslim / ramda-typescript.md
Last active October 12, 2020 11:37
Ramda and Typescript

Ramda and TypeScript

... when @types/ramda is installed.

Type inference for currying:

const curried = add(1);
// infers: const curried: (b: number) => number

...