Skip to content

Instantly share code, notes, and snippets.

View bathos's full-sized avatar
⚱️
gimme elixir!

Darien Maillet Valentine bathos

⚱️
gimme elixir!
View GitHub Profile
@bathos
bathos / quik-utf8-to-cps-sync.js
Last active October 18, 2017 03:00
quik-utf8-to-cps-sync.js
// Deliberately permissive (but fast) conversion from utf8 buffer to codepoint
// array. Invalid multibyte codepoint sequences become replacement character and
// orphaned surrogates are expressly permitted.
//
// The speed is almost identical to the built-in TextDecoder; however, it yields
// an array of codepoints, rather than a string. When compared as ways to get
// codepoints (e.g. by following TextDecoder.end with map), this runs fourteen
// times faster (not because TextDecoder is slow, but because x.from(str, map)
// is — not sure what accounts for such a big difference, though it only reaches
// those extremes on large files).
@bathos
bathos / debouncing-promises.js
Last active November 8, 2017 18:59
debouncing-promises.js
// Typically "debouncing" means limiting execution frequency by a specific
// interval while still guaranteeing that, after a call, the behavior will still
// execute (just not synchronously; the relationship is many->one for "calls" to
// "executions"). However it is sometimes desireable to not limit by a specific
// time — you may just want to prevent something from recurring concurrently,
// without concern for overall frequency. In these cases, you may instead want
// to debounce "against" promises. This function accepts a method that returns a
// promise and returns a new function that will execute that method immediately
// only when the last execution’s promise remains unresolved, and otherwise will
// queue for re-execution (once) when the current execution is complete. In all
@bathos
bathos / strings.md
Last active March 22, 2018 02:32
strings.md

When Strings Attack

The Enduring Legacy of a Very Naughty Hack from 1995 / A Bit of Intl

This is my first attempt at a tech talk thing. The focus here is strings in ES. First we’ll talk about what a string is in ES, then some of the consequences that fall out of that (a few aren’t super obvious), and finally some of the cool tools available for working with strings safely & effectively. Because of the nature of the subject we’ll also get into some more general information about Unicode, encodings and broad concepts related to strings and language.

@bathos
bathos / function-types-kinda.md
Created April 8, 2018 06:15
function-types-kinda.md
FK Cstr TM: Lexical TM: Global / Strict
normal cstr - function() {}; new Function
normal no () => {} x() {}
classConstructor cstr - class {}
classConstructor no - -
generator cstr - -
generator no - function * () {}; * x() {}
async cstr - -
async no async () => {} async function() {}; async x() {}
@bathos
bathos / throwing-into-iterated-async-gen.js
Created December 1, 2019 21:36
throwing-into-iterated-async-gen.js
void async function() {
const asyncGenerator = async function *() { try { yield; } catch {} }();
try {
for await (const value of asyncGenerator) {
await asyncGenerator.throw(new Error);
}
console.log('caught');
} catch {
@bathos
bathos / weekdata.mjs
Last active December 7, 2019 14:08
/util/date/weekdata.mjs
import range from '/util/iterator/range.mjs';
// Week start data pulled from similarly golfed answer on SO:
// https://stackoverflow.com/a/57102881/1631952
//
// I’m not thrilled with this; we probably ought to be generating it since the
// data is pretty obscure. OTOH I’d be far less thrilled with loading hefty
// chunks of ICU data and this is pretty solid and compact!
//
// Note that the RegExp given for lang tags in that answer is a bit off. The one
@bathos
bathos / escape-eight-and-nine.md
Last active December 12, 2019 07:38
escape-eight-and-nine.md

Trying to understand what accounts for "\8" and "\9" being considered valid string literals with identity escapes in Chrome and FF.

Annex B permits sloppy mode to include legacy octal escapes in strings. It achieves this by replacing the ‘normal’ (strict) EscapeSequence production with a new version accompanied by three new productions.

In the sloppy version, three of the alternatives are the same. The change is replacing this rule:

"0" [lookahead ∉ DecimalDigit]

@bathos
bathos / home-obj.js
Created December 20, 2019 00:18
home-obj.js
class Foo extends Bar {
method() {
const homeObject = Foo.prototype;
const superReferenceBase = Object.getPrototypeOf(homeObject);
}
static method() {
const homeObject = Foo;
const superReferenceBase = Object.getPrototypeOf(homeObject);
}
@bathos
bathos / observing-last-index-values.js
Created December 30, 2019 21:55
observing-last-index-values.js
function matchAllButUpdateOriginalRegExp(regExp, string) {
return regExp[Symbol.matchAll].call({
constructor: { [Symbol.species]: function() { return regExp; } },
flags: regExp.flags,
lastIndex: regExp.lastIndex
}, string);
}
const pattern = /./g;
@bathos
bathos / acorn-nullish-coalescing-and-optional-chaining.mjs
Created March 18, 2020 13:00
acorn-nullish-coalescing-and-optional-chaining.mjs
export default function acornSkullFishKoalaCaressingAndNonstopSpinalDraining(Parser) {
const tt = Parser.acorn.tokTypes;
const ocToken = new Parser.acorn.TokenType('?.');
const ncToken = new Parser.acorn.TokenType('??', {
beforeExpr: true,
binop: 2.5
});
return class extends Parser {
getTokenFromCode(codePoint) {