Skip to content

Instantly share code, notes, and snippets.

View jczimm's full-sized avatar

Jacob Zimmerman jczimm

View GitHub Profile
@jczimm
jczimm / quick-pipe.js
Created February 10, 2021 01:00
Hack to implement piping without experimental pipeline operator (|>)
Object.prototype.pipe = function pipe(fn) { return fn(this.constructor(this)); }
const auto = rules =>
(strs, ...vars) => strs.map((str, i) => {
if (i >= vars.length) return str;
const v = vars[i];
const r = rules[typeof v];
return str + (r ? r(v) : v);
}).join('');
// examples:
const t = auto({ boolean: v => v || '', object: JSON.stringify });
@jczimm
jczimm / mini-uni-golf.js
Last active June 4, 2022 05:21
unicode string encoder/decoder for ascii
const toHexCharCode = char => char.charCodeAt(0).toString(16).padStart(2, '0');
function encodeGolf(plain) {
return unescape( // enter unicode escape codes land
plain.match(/[\s\S]{1,2}/g) // split string into pairs of letters
.map(([a, b]) => b ? '%uD8' + toHexCharCode(a) + '%uDC' + toHexCharCode(b) : a) // encode each pair as a high surrogate and low surrogate in unicode (where an odd character, just include it unencoded to preserve odd length)
.join('') // concatenate the escape codes as a string
); // exit escape codes land; return actual unicode characters
}
// 5 bytes fewer than decode
@jczimm
jczimm / index.js
Last active June 23, 2016 04:33
requirebin sketch
const choo = require('choo')
const app = choo()
app.model({
// namespace: "app",
state: { title: 'Set the title' },
reducers: {
update: (action, state) => ({ title: action.value })
}
})
@jczimm
jczimm / noteToFrequency.js
Last active January 18, 2016 00:49
Calculates the frequency of a note provided on 12TET scale. (traditional Western scale)
const a4Freq = 440;
const noteNames = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"];
function noteToFrequency(noteName) {
const [, noteLetter, octaveNum] = noteName.match(/^(\w#?)(\d)$/) || [];
const numStepsFromA = noteNames.indexOf(noteLetter.toUpperCase());
if (numStepsFromA === -1 || !noteLetter) return new Error("Invalid note");
const numStepsFromMiddleA = numStepsFromA + 12 * (octaveNum - 4);
module['exports'] = function jczimmCLI (hook) {
var request = require('request');
var http = require('http')
, vm = require('vm')
, concat = require('concat-stream');
request.get("https://npmcdn.com/hget@3.0.0/index.js", function(err, res, body) {
var sandbox = { require: require };
vm.createContext(sandbox);
vm.runInThisContext(body, './remote_modules/hget.js', sandbox);
var hget = require('./remote_modules/hget.js');
@jczimm
jczimm / example.es7.js
Last active February 6, 2016 19:40
Tiny right-to-left control flow utility (experiment) utilizing context binding. Allows both sync and async steps. Syntax for context-binding simplified by the experimental strawman operator (https://github.com/zenparsing/es-function-bind). No error/misuse handling. *Uses ES7 features*
// Syntax 1:
s.val(13)::third::second::first::s.run();
// Syntax 2: (-> s.val completely unnecessary)
third::second::first::s.run(13);
//
// Example programmatic creation of a chain
let chain = third;
@jczimm
jczimm / charmap.js
Last active November 24, 2015 01:40
Simple character mapping.
var charMap = function charMap(str, from, to, tryLowerCase = false) {
return str
.split("")
.map((char, i) => to.charAt(from.indexOf(tryLowerCase ? char.toLowerCase() : char)) || char)
.join("");
};
var normal = "abcdefghijklmnopqrstuvwxyz",
smallLetters = "\u1d00\u0299\u1d04\u1d05\u1d07\u0493\u0262\u029c\u026a\u1d0a\u1d0b\u029f\u1d0d\u0274\u1d0f\u1d18\u01eb\u0280s\u1d1b\u1d1c\u1d20\u1d21x\u028f\u1d22";
@jczimm
jczimm / Chain.js
Last active October 10, 2015 19:43
Fancy array construction (fully extendable)
function Chain (datum) {
if (datum != undefined) {
this.length = this.length !== undefined ? this.length + 1 : 1;
this[this.length - 1] = datum;
return Chain.bind(this);
} else {
return this;
}

Keybase proof

I hereby claim:

  • I am jczimm on github.
  • I am jczimm (https://keybase.io/jczimm) on keybase.
  • I have a public key whose fingerprint is F12C FDA4 2608 B5F4 7582 0D1B DFB5 382A 5D4D E45C

To claim this, I am signing this object: