Skip to content

Instantly share code, notes, and snippets.

View getify's full-sized avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@getify
getify / gist:1b26accb1a09aa53ad25
Last active April 20, 2024 11:35
first draft sketch of a "Worker" polyfill
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Worker Polyfill</title>
<script src="polyfill.worker.js"></script>
</head>
<body>
<h1>Worker Polyfill</h1>
<script>
@getify
getify / 1.md
Last active March 23, 2024 16:16
describing a bundling question in detail

I'm the author of an npm package that comes as a single ESM-format module, let's call it "A". This package is only a client-side (browser) library, it will not work in Node -- it interacts with client-only web platform APIs.

The "A" package doesn't use any typical build tools (typescript, webpack/vite, etc). But it does include a simple "publish-build" script that's used at npm publish time to prepare a dist/ directory for the package.

The package relies on three npm package dependencies (call them "B", "C", and "D"), which I do not own/control. These 3 packages only distribute themselves as plain .js window-global style scripts. They cannot be imported, because (unfortunately) they make assumptions about global-scope this (being window), non-strict mode, etc. That also means they can't just be inlined into the "A" distribution module file.

Moreover, these dependencies are big enough (and potentially useful enough) that a user of "A" might also want to access and use "B", "C", or "D" functi

@getify
getify / 1-CalendarItem.js
Last active March 21, 2024 09:11
an illustration (non-trivial example) of many newer JS class features
// abstract class, not intended to be instantiated directly
class CalendarItem {
static #UNSET = Symbol("unset")
static #isUnset(v) {
return v === this.#UNSET;
}
static {
for (let [idx,msg] of [
"ID is already set.",
"ID is unset.",
@getify
getify / a0.js
Last active January 9, 2024 09:55
examples using go-style CSP API emulation on top of asynquence-flavored CSP
var ASQ = require("asynquence-contrib");
ASQ()
.runner(
ASQ.csp.go(function*(ch){
yield ASQ.csp.put(ch,1);
yield ASQ.csp.take( ASQ.csp.timeout(1000) );
console.log( "a", yield ASQ.csp.take( ch ) );
}),
@getify
getify / gist:5226305
Last active January 7, 2024 11:59
playing around with an `Object.make()` helper
// `Object.make(..)` is a helper/wrapper for `Object.create(..)`. Both create a new
// object, and optionally link that new object's `[[Prototype]]` chain to another object.
//
// But `Object.make(..)` makes sure the new object always has a `__proto__` property
// (even a null one) and delegation to a `isPrototypeOf(..)` method, both of which are
// missing from the bare object (aka "Dictionary") created by `Object.create(null)`.
//
// `isPrototypeOf()` is put on a extra object that your created object can delegate to,
// if any only if you create an empty object (by not passing a `linkTo`) that otherwise
// wouldn't have access to `isPrototypeOf()`.
@getify
getify / gist:5285514
Last active January 7, 2024 11:58
since `let (foo = 42) { ... }` is not coming to ES6 after all...

I hereby propose this form of let usage as the next best option, since the clearly better let (foo = 42) { ... } let-block-statement syntax is dead and not coming to ES6:

/*let*/ { let foo = 42;

   // your code that uses `foo`

}
@getify
getify / ex1-prototype-style.js
Last active January 7, 2024 11:58
OLOO (objects linked to other objects) pattern explored (with comparison to the prototype style of the same code)
function Foo(who) {
this.me = who;
}
Foo.prototype.identify = function() {
return "I am " + this.me;
};
function Bar(who) {
Foo.call(this,"Bar:" + who);
@getify
getify / 1.js
Created January 2, 2024 17:53
minimal POC for Reader monad in JS
var r = Reader();
console.log(
r
.map(env => ({ ...env, y: 2 }))
.chain(env => Reader(() => ({ ...env, z: 3 })))
.evaluate({ x: 1 })
);
// { x: 1, y: 2, z: 3 }
@getify
getify / 1-setup.js
Last active December 15, 2023 13:25
find size of largest region in matrix... solutions are breadth-first iterative (2) and depth-first recursive (3)
// Adapted from: https://www.geeksforgeeks.org/find-length-largest-region-boolean-matrix/
"use strict";
var M1 = [
[ 0, 0, 1, 1, 0 ],
[ 1, 0, 1, 1, 0 ],
[ 0, 1, 0, 0, 0 ],
[ 0, 0, 0, 1, 1 ]
];
@getify
getify / 1.js
Last active December 8, 2023 17:51
JS vs Foi symbol count comparision: FP (partial application, pipelines, etc)
// note: this uses proposed JS syntax for pipeline operator: https://github.com/tc39/proposal-pipeline-operator
// CHARACTER STATS (code comments and their padded whitespace ignored)...
// letters, numbers (a-z A-Z 0-9): 317
// non-letters-non-whitespace: 138
// simple symbols (+ - * / . " : ( ) [ ] { } , =): 94
// compound symbols (=> ?? ?. |> %%): 15 (30 chars)
// optional semicolons: 14
// whitespace: 90