Skip to content

Instantly share code, notes, and snippets.

@donabrams
donabrams / example.js
Last active January 23, 2026 22:13
Monad-ish Maybe in JS
var good = {a: {b: {c: 1}}}
console.log(new Maybe(good).bind(t=>t.a).bind(t=>t.b).bind(t=>t.c).getOrElse(2))
var bad = {a: 3}
console.log(new Maybe(bad).bind(t=>t.a).bind(t=>t.b).bind(t=>t.c).getOrElse(2))
@donabrams
donabrams / mistate.md
Last active May 27, 2017 17:15
First attempt at spec-ing out a mistate data structure

mistate

A mistate is a Javascript data structure with a schema, underlying immutable data structure, lensing, and efficient subscriber abilities.

Schema

The schema format is the same as GraphQL.

Here's an example:

@donabrams
donabrams / mock-api-gateway.hs
Created June 12, 2017 14:30
Mock API Gateway signature
start :: ApiGateway -> Request -> Response
create :: forall i, o.
{
method,
path,
intReq: Async (HTTPRequest -> i),
lambda: Async (i -> Tuple o Status),
intRes: Status -> o -> Async HTTPResponse
} -> ApiGateway
-- problems: does o differ per status but need to be synced between 7/8?
1. Choose your boundary
2. Identify state
3. Test *just* the boundary
4. Any test that will be maintained (non-TDD) should have a positive and negative test
5. Unit tests can be thrown away at will
6. DRY/inheritance is usually bad in test code, especially if stateful
7. Try to colocate data and code as much as possible
8. Make randomness and time explicit
9. It’s best to write tests FIRST
@donabrams
donabrams / memoize.js
Last active November 9, 2017 16:24
Memoizer
let set = (cache, val, k, ...rest) => {
if (rest && rest.length) {
if (!cache.has(k)) {
cache.set(k, new Map());
}
set(cache.get(k), val, ...rest);
} else {
cache.set(k, val);
}
};
@donabrams
donabrams / choice.js
Last active November 20, 2017 20:40
choice.js - OOP Either
const choice = (decision) => {
let yes = () => {};
let no = () => {};
const a = {
yes: (f) => { yes = f; return a; },
no: (f) => { no = f; return a; },
decide: () => {
const next = decision() ? yes() : no();
return next.decide ? next.decide() : next;
}
@donabrams
donabrams / deepProxy.js
Last active April 20, 2018 18:28
Deep Read Only
const a = {
foo: "bar",
beep: {
boop: 1,
},
baz: [
true,
]
};
@donabrams
donabrams / checklist.txt
Last active April 25, 2018 23:16
General JWT approach that usually isn't terrible checklist
☐ Put JWT in a cookie
☐ HTTPOnly cookie
☐ Secure cookie
☐ Domain w/ a subdomain (never root)
☐ No authorization in the JWT body (look it up serverside ya lazy asses)
☐ Userid in cookie should NOT exist anywhere but auth service
☐ Short window ( < 15 min) for JWT authentication token
☐ Ability to deauthorize a refresh token chain serverside (but long life on a refresh token is OK)
☐ Ability to monitor requests by refresh token chain
@donabrams
donabrams / BetterNominal.ts
Last active August 3, 2018 18:29
Nominal typing in TypeScript
interface FooId extends String {
_fooIdBrand: string;
};
export interface Foo {
id: FooId;
color: 'yellow';
isAwesome: true;
}
interface BarId extends String {
Worker information
hostname: e493aab3-9ebb-4ff3-bf71-74d0216e5c37@4740.wjb-1
version: v3.6.0 https://github.com/travis-ci/worker/tree/170b2a0bb43234479fd1911ba9e4dbcc36dadfad
instance: 2d2c786d-0537-48f2-96f1-2eb3fa4a1976 travis-ci-macos10.12-xcode8.3-1507738863 (via amqp)
startup: 1m6.709206302s
Download from https://build.travis-ci.com/filter/redirect_io.rb failed. Trying https://build.travis-ci.com/filter/redirect_io.rb ...
ruby: No such file or directory -- /Users/travis/filter.rb (LoadError)