Skip to content

Instantly share code, notes, and snippets.

@dead-claudia
dead-claudia / babel-node.mjs
Created July 5, 2018 07:56
Babel reload script concept
/**
* This tiny wrapper file checks for known node flags and appends them
* when found, before invoking the "real" _babel-node(1) executable.
*/
import getV8Flags from "v8flags";
import path from "path";
getV8Flags(function(err, v8Flags) {
if (err != null) throw err;
@dead-claudia
dead-claudia / id-generate.js
Created January 30, 2019 00:35
Simple ID generator
"use strict"
// Creates a `generate` function using an alphabet, for optimally small IDs
// if you can only use certain characters. This could be useful for file name
// generators, minifiers, among many others. You *do* need to expand letter
// ranges, though.
module.exports = alphabet => {
const charTable = [...new Set(Array.from(alphabet, x => `${x}`))]
let counter = 0
if (charTable.length < 2) {
@dead-claudia
dead-claudia / proto.js
Created January 27, 2015 03:57
Object.prototype.__proto__ polyfill for ES6 (if the runtime *doesn't* implement it -- it's technically optional for non-web browsers)
/**
* By Isiah Meadows.
* This code is licensed under CC0 1.0 Universal. You can get a copy of the license at https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
*/
if (!function (F) {
return {}.__proto__ === Object.prototype; // Also if it's broken
}(function () {}) && Object.getPrototypeOf && Object.setPrototypeOf) {
Object.defineProperty(Object.prototype, '__proto__', {
'get': function () {
@dead-claudia
dead-claudia / classnames-object.js
Created December 16, 2018 21:13
`classnames`, but emitting an object instead of a string.
var hasOwn = {}.hasOwnProperty
function writeClasses() {
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i]
if (!arg) continue
if (typeof arg === 'string' || typeof arg === 'number') {
this[arg] = true
} else if (typeof arg === 'string') {
@dead-claudia
dead-claudia / 0-react-hooks.jsx
Created October 29, 2018 21:29
React if they went with a new language rather than an embedded DSL
// This is a rough port of their TodoMVC app, using their Hooks API
import React, { useRef, useState, useEffect, useMemo } from "react"
import { render } from "react-dom"
function uuid() {
let uuid = ""
for (let i = 0; i < 32; i++) {
if (i === 8 || i === 12 || i === 16 || i === 20) uuid += "-"
@dead-claudia
dead-claudia / local-date.js
Last active September 27, 2018 17:33
Simple date library
// A simpler implementation of date handling, with the following additional
// assumptions:
//
// 1. We only need the day, month, and year.
// 2. Time zones are irrelevant.
// 3. Day/month/year checks and range checks are *far* more common than date
// difference calculation.
// 4. Dates are rarely written to, mostly read.
//
// Helpfully, this lets us represent dates with a single 32-bit integer, which
var Module = require("module")
function requireUncached(file, baseDir) {
// Hack: hijack Node's internal resolution algorithm to require the file
// as if from a fake module in the correct base directory. It also will
// avoid several bugs with the `resolve` module (Node's is necessarily
// more stable).
var dirname = path.resolve(baseDir)
var m = new Module(path.join(dirname, "dummy.js"))
@dead-claudia
dead-claudia / mithril-remove-node-from-dom.js
Created August 28, 2018 10:31
Recast Mithril node removal code
function removeNodeFromDOM(vnode) {
var parent = vnode.dom.parentNode
if (parent != null) {
var count = vnode.domSize
if (count != null && count > 1) {
while (--count) parent.removeChild(vnode.dom.nextSibling)
}
parent.removeChild(vnode.dom)
}
}
@dead-claudia
dead-claudia / weak-collections.js
Last active July 30, 2018 02:26
Weak maps and weak sets in terms of private symbols
const _wmSymbol = Symbol.private("wmSymbol")
const _wsSymbol = Symbol.private("wsSymbol")
const call = Function.call.bind(Function.call)
const hasOwn = Function.call.bind({}.hasOwnProperty)
const getOwn = Object.getOwnPropertyDescriptor
const TE = TypeError
const checkMapThisKey = makeChecker("WeakMap", "weak map", _wmSymbol)
const checkSetThisKey = makeChecker("WeakSet", "weak set", _wsSymbol)
function makeChecker(name, human, symbol) {
// A complete set of type defs for Fantasy Land
interface Setoid {
"fantasy-land/equals"(other: this): boolean;
}
interface Ord {
"fantasy-land/lte"(other: this): boolean;
}
interface Semigroupoid<A, B> {