Skip to content

Instantly share code, notes, and snippets.

@andrejewski
andrejewski / koatil.js
Created August 12, 2018 22:07
Koa middleware utilities [WIP]
// koatil/index
function group (middlewares) {
return function _group (ctx, next) {
return runGroup(middlewares, 0, ctx, next)
}
}
function runGroup (routes, routeIndex, ctx, next) {
const route = routes[routeIndex]
<style>
* {
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";
box-sizing: border-box;
}
body {
margin: 0px;
position: fixed;
top: 0;
@andrejewski
andrejewski / logger.js
Created September 29, 2018 03:36
A good logger
exports.createLogger = function createLogger (write, values = []) {
return {
log: value => write([...values, value]),
withContext: value => createLogger(write, [...values, value])
}
}
@andrejewski
andrejewski / sock-scratch.js
Created October 7, 2018 17:59
Playing with a new kind of mock/stub/spy
function withTest (service, tester) {
function mark () {
if (process.env.NODE_ENV !== 'test') {
return service
}
}
return { ...service, mark, testMark }
}
@andrejewski
andrejewski / sub.js
Created March 21, 2020 19:32
A safer String.prototype.replace
// "substitute" is too hard to type
function sub (str, candidate, replacer) {
/*
NOTE: Using String.prototype.replace is bad because:
1) casting an arbitrary string to a RegExp is unsafe
2) replacement has obscure substitution rules ($ is special)
*/
let match = str.indexOf(candidate)
if (match === -1) {
@andrejewski
andrejewski / dattle.bnf
Created January 30, 2023 03:18
Dattle specification described in Backus–Naur form (BNF). Note: I was using the Rust `bnf` crate to iterate which doesn't seem to support UTF-8 ranges but Dattle is built atop UTF-8 and this grammar should be expanded to support UTF-8 quoted strings.
<value> ::= <nil> | <boolean> | <string> | <vector> | <map>
<nil> ::= "nil"
<boolean> ::= "true" | "false"
<character> ::= <bring-your-own-utf8-character-term>
<quote-escape> ::= '\"'
<string-value> ::= <character> | <quote-escape>
<string-content> ::= <string-value> | <string-value> <string-content>
<string> ::= '"' <string-content> '"'