Skip to content

Instantly share code, notes, and snippets.

View ryandabler's full-sized avatar

Ryan Dabler ryandabler

View GitHub Profile
@ryandabler
ryandabler / json-parser.js
Last active January 23, 2024 17:57
Complete example of a JSON parser for this article: https://itnext.io/demystifying-json-parse-383bb4907d79
const tokens = [
[/^\s+/, null],
[/^\[/, '['],
[/^]/, ']'],
[/^\{/, '{'],
[/^}/, '}'],
[/^:/, ':'],
[/^,/, ','],
[/^"/, '"'],
[/^\d+/, 'number'],
//
// ----- Sequences -------------------------
//
const range = (l, u = Infinity) => function* () {
while (true) {
if (l < u) {
yield l++;
continue;
}
@ryandabler
ryandabler / Smallfuck.ts
Last active October 2, 2021 21:39
Smallfuck interpreter built using TypeScript's type system for this article: https://itnext.io/typescript-and-turing-completeness-ba8ded8f3de3
//
// ------- General utilities ---
//
type ArrayFromNumber<N extends number, A extends any[] = []> = A['length'] extends N ? A : ArrayFromNumber<N, [...A, any]>;
type Increment<T extends number> = [...ArrayFromNumber<T>, any]['length'];
type Decrement<T extends number> = ArrayFromNumber<T> extends [...(infer U), any] ? U['length'] : never;
type Rewind<Arr extends any[], I extends number, Depth extends number = 0> = Arr[I] extends '['
? Depth extends 0 ? I : Rewind<Arr, Decrement<I>, Decrement<Depth>>
@ryandabler
ryandabler / Arithmetic.ts
Last active December 19, 2023 17:09
Complete example of an arithmetic system built inside TypeScript's type system for this article: https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
// Utility types
type Length<T extends any[]> = T extends { length: infer L } ? L : never;
type BuildTuple<L extends number, T extends any[] = []> =
T extends { length: L }
? T
: BuildTuple<L, [...T, any]>;
type MultiAdd<N extends number, A extends number, I extends number> =
I extends 0
@ryandabler
ryandabler / Proxy.js
Last active November 30, 2020 00:24
Complete example of proxying an array for this article: https://itnext.io/meta-programming-in-javascript-with-proxies-64fa4898070e
const db = [
{
name: 'John Doe',
age: 28,
lastModified: Date.now(),
lastAccessed: Date.now()
},
{
name: 'Jane Smith',
age: 30,
@ryandabler
ryandabler / Object property descriptors.js
Last active June 17, 2021 00:05
Complete example of an object defined using property descriptors for this article: https://itnext.io/enhancing-javascript-objects-with-descriptors-and-symbols-2cdc95e9b422
// Create "backend" object to hold data for getters and setters on main object
const _ = Object.create( null );
Object.defineProperties(
_,
{
firstname: {
value: 'John',
writable: true,
enumerable: false,
@ryandabler
ryandabler / IndexedDB with cursor complete example.js
Last active December 8, 2020 06:58
A complete working example of all the uses of cursor for this article: https://itnext.io/searching-in-your-indexeddb-database-d7cbf202a17
const invoices = [
{ invoiceId: "c1-5b", vendor: "GE", paid: true },
{ invoiceId: "a", vendor: "GE", paid: false },
{ invoiceId: "-7c", vendor: "Whirlpool", paid: true },
{ invoiceId: "b", vendor: "Bosch", paid: true },
{ invoiceId: "9b54-dbdd", vendor: "Bosch", paid: false },
{ invoiceId: "2cac788-", vendor: "Whirlpool", paid: true },
{ invoiceId: "99cd9c14", vendor: "Bosch", paid: false },
{ invoiceId: "a1854b-14b", vendor: "Frigidaire", paid: false },
{ invoiceId: "43b76a75", vendor: "Bosch", paid: true },
const invoices = [
{invoiceId: "c1-5b", vendor: "GE", paid: true},
{invoiceId: "a", vendor: "GE", paid: false},
{invoiceId: "-7c", vendor: "Whirlpool", paid: true},
{invoiceId: "b", vendor: "Bosch", paid: true},
{invoiceId: "9b54-dbdd", vendor: "Bosch", paid: false},
{invoiceId: "2cac788-", vendor: "Whirlpool", paid: true},
{invoiceId: "99cd9c14", vendor: "Bosch", paid: false},
{invoiceId: "a1854b-14b", vendor: "Frigidaire", paid: false},
{invoiceId: "43b76a75", vendor: "Bosch", paid: true},
@ryandabler
ryandabler / JQ class using generator method.js
Created June 9, 2018 00:03
This is a jQuery-lite implementation using generator methods for iterability
class JQ {
constructor(list) {
let i = 0;
while (list[i] !== undefined) {
this[i] = list[i];
i++;
}
this.length = i;
}
@ryandabler
ryandabler / JQ class using Symbol.iterator.js
Created June 8, 2018 20:45
This is a jQuery-lite implementation using Symbol.iterator for built-in iterability
class JQ {
constructor(list) {
let i = 0;
while (list[i] !== undefined) {
this[i] = list[i];
i++;
}
this.length = i;