Skip to content

Instantly share code, notes, and snippets.

@jimkang
jimkang / require-all-in-dir.js
Created September 18, 2018 00:48
Requiring all files in a directory
/* global __dirname */
var fs = require('fs');
var files = fs.readdirSync(__dirname).filter(isAConfigFile);
var configs = files.map(file => require(__dirname + '/' + file));
function isAConfigFile(filename) {
return filename.endsWith('-config.js');
}
@jimkang
jimkang / da-mystery-of-unit-type.scala
Created July 6, 2018 13:59
Fairly similar lines of code seem to compile differently here.
def render(s: String, mode: NameMode): String =
if (s.matches("\\d+")) {
return render(SafeLong(s.toInt), mode);
// The line below seems to compile.
} else if (s.charAt(0) == '-') {
"negative " + render(s.substring(1).toInt, mode)
} else if (s.length < 2) {
val c = s.charAt(0)
if (charIsAlpha(c)) {
onesForChar(c)
@jimkang
jimkang / stddev.js
Created June 12, 2018 14:01
Simple stddev example
var curry = require('lodash.curry');
var counts = [9, 4, 4, 5, 49];
var avgCount = mean(counts);
var deviations = counts.map(curry(bMinusA)(avgCount));
var squaredDevs = deviations.map(square);
var variance = mean(squaredDevs);
var populationStdDev = Math.sqrt(variance);
console.log('avgCount', avgCount);
console.log('populationStdDev', populationStdDev);
@jimkang
jimkang / tool.js
Created May 1, 2018 12:42
Typical top of a tool script
/* global __dirname, process */
var fs = require('fs');
var ndjson = require('ndjson');
var through2 = require('through2');
var callNextTick = require('call-next-tick');
if (process.argv.length < 3) {
console.log(
'Usage: node tools/toolname.js <export something.ndjson> > new_file.ndjson'
@jimkang
jimkang / tracking-colors.js
Created March 8, 2018 20:25
In case I ever need it: Color tracker for secret click-detection mirror canvases
var hash = require('string-hash');
function TrackingColors() {
var idsForColors = {};
return {
getTrackingColorForId,
getIdForTrackingColor
};
function getTrackingColorForId(id) {
@jimkang
jimkang / number-sayer.make
Created February 10, 2018 02:03
A thing that'll say large numbers like an octillion
# Usage: ZEROES=9 make number-audio
# e.g. A million has six zeroes (3 * (1 + 1)).
# A billion has nine zeroes (3 * (2 + 1)).
# A trillion has 12 zeroes (3 * (3 + 1)).
number-audio:
rm -f output.wav
rm -f test-list.txt
echo "file static/one.wav\n" > test-list.txt
for i in {1..$(ZEROES)}; do printf "file 'static/zero.wav'\n" >> test-list.txt; done
ffmpeg -f concat -safe 0 \
@jimkang
jimkang / size-svg-to-window.js
Created January 8, 2018 02:04
Set svg size to window size
var width = +window.innerWidth;
var height = +window.innerHeight;
svg.attr('width', width);
svg.attr('height', height);
@jimkang
jimkang / get-titles-from-page.js
Created December 22, 2017 00:56
Get titles from pages like this: http://www.pwnrank.com/top-500
var names = []; var items = document.querySelectorAll('.game .title'); for (var i = 0; i < items.length; ++i) { names.push(items[i].textContent); } console.log(JSON.stringify(names, null, 2));
@jimkang
jimkang / check-for-islands.js
Last active October 11, 2017 04:24
Quick program to check a tree for islands (groups of nodes that do not connect to every other node)
/* global process */
// Exhaustively searches for islands in a minimum spanning tree.
var fs = require('fs');
if (process.argv.length < 3) {
console.log('Usage: node tools/check-for-islands.js minimum-spanning-tree.json');
}
var mst = JSON.parse(fs.readFileSync(process.argv[2]));
@jimkang
jimkang / scrape-wikipedia-artist-names.js
Created September 18, 2017 13:46
Scraping artist names from Wikipedia pages like this: https://en.wikipedia.org/wiki/Category:Musicians_by_band Paste this into the console for each page.
var artistNodes = document.getElementsByClassName('CategoryTreeLabel');
var nodeCount = artistNodes.length;
var names = [];
for (var i = 0; i < nodeCount; ++i) {
let name = artistNodes[i].textContent;
name = name.replace('(band) ', '');
name = name.replace(' members', '');
names.push(name);
}
console.log(JSON.stringify(names, null, 2));