Skip to content

Instantly share code, notes, and snippets.

@josephrocca
josephrocca / example.js
Created September 27, 2018 15:06
Decode WebP Images with NodeJS (inlined .wasm binary)
(async function() {
const WebPDecoder = await require("./fetchWebPDecoder")();
let webPDataUrl = "data:image/webp;base64,UklGRvYRAABXRUJQVlA4IOoRAAAQgQCdASpAAfAAPu1qrlEppaQqqXIMIVAdiWUHDODHxFjwOctUkVXfTu1UpVUx/8lcT9b44Nz547X6Nn7QXa4dE58x2yL9IRGF4LD3X1pNIIC8qvPaDVDfcOj3EjGc+T9JhNJF+L/SM7sk8BKuDSa9HYiOOPGixk0tWnhAPbVsTW2lc62I2xRGGbcjPJKjDUzadFCmB0hIFFWlTLKi6sBE/xkHbn/zbCr3pURqob/vE5mte/EjzeKcS7diP0QXZTbqEAHxHsKHICWbCjPQ2ZIUZ5Csu45dJ/hH/Gj1zF+R11M6P6dOpmpLZC+Fpv7dzbE08zm01ZOyOyXMBFoIJoB9bUmXZNfuYe4DuIGJrHH9Jw5K5Xp4m8dprR3hyErtiuNMK59cK4Ku0JyhQbrWURbQWFTbNvFk8hjBOOdBQPO/mJHMyHw/tSsFM6E+2yhGFDXnR9uYL+TCkGjo9JUE/2e7zJ9mLHS4ut+NkkDBQO7qJfJhYf1/PK5wUyAKc8oyhSCNBs28xKg/624mNhmQQhARg9qbZD8l8GiKnton/U3MCwVr81c0+6xKrGWYKYJGvRyVRj5TqwwbQPdrek7/hyhW/YNEYYY/BPljajvona6v3h+Q9faJtzqv0HBqITCFbKrXaFF0IZhlZZ4jLfL9h7kZ+I9tAUwxyeFWEpzl0+yDzOaJkwIAEshOQvZ3LsV5EIj/sysyOYDHhKKcVHPgGLHGq6g4OHovginNIHs08DpGFRfG7DG7MEF835uLXSHASXMdFpjHe3yp8NWYyVAqKfzrxU1tNQDkbezkHfcXRibTbHIlnxeP8sLv/hL7MRWyH/OoGNm+lok41jGTBlv0eZVrBXpx5r+xyBRdPkex0
@josephrocca
josephrocca / chromeFourtySupport.js
Last active January 8, 2019 01:01
Chrome 40 Support
/*
Workaround for a workaround for a workaround.
Includes:
- NodeList.forEach polyfill (not supported by polyfill.io)
- https://polyfill.io/v2/polyfill.js?features=default,es5,es6,Array.prototype.@@iterator,Array.prototype.entries,Array.prototype.find,Array.prototype.findIndex,Array.prototype.copyWithin,Array.prototype.findIndex,Array.prototype.includes,Array.prototype.keys,Array.prototype.values,DOMTokenList.prototype.@@iterator,DocumentFragment,Element.prototype.dataset,EventSource,Function.name,HTMLCanvasElement.prototype.toBlob,HTMLDocument,MutationObserver,NodeList.prototype.@@iterator,IntersectionObserver,IntersectionObserverEntry,NodeList.prototype.@@iterator,Object.entries,Object.values,Promise.prototype.finally,RegExp.prototype.flags,String.fromCodePoint,String.prototype.codePointAt,String.prototype.padEnd,String.prototype.padStart,String.prototype.repeat,Symbol.hasInstance,Symbol.isConcatSpreadable,Symbol.match,Symbol.replace,Symbol.search,Symbol.species,Symbol.split,Symbol.toPrimitive,Symbol.toS
@josephrocca
josephrocca / prefix-2-grams.json
Last active January 26, 2019 01:13
N-grams based on first N letters of top 466k+ english words (@dwyl/english-words/words.txt) - i.e. "prefix N-grams"
[["un",20576],["co",12944],["re",11833],["pr",10795],["no",9483],["in",8667],["ca",7734],["su",7670],["de",7549],["se",7169],["ma",6876],["pa",6834],["di",6735],["st",6462],["an",6050],["ch",6043],["tr",5657],["mi",5328],["pe",5072],["ba",5050],["po",5017],["ha",4714],["me",4703],["be",4683],["he",4553],["mo",4343],["ov",4326],["sp",4202],["sa",4182],["la",3935],["th",3894],["te",3718],["sc",3663],["br",3596],["al",3514],["sh",3482],["ho",3464],["ta",3333],["cr",3305],["ph",3279],["fo",3163],["ar",3160],["bo",3157],["en",3128],["li",3107],["qu",3095],["si",3000],["so",2993],["hy",2985],["gr",2978],["ra",2888],["to",2863],["ga",2847],["ex",2677],["pi",2660],["do",2648],["ro",2626],["ne",2608],["le",2597],["wa",2595],["we",2582],["pl",2566],["lo",2554],["bi",2506],["bu",2486],["cl",2433],["fi",2421],["fl",2420],["fa",2402],["ac",2389],["mu",2274],["wi",2213],["fr",2173],["vi",2139],["ou",2128],["bl",2109],["am",2039],["da",2018],["ve",1935],["wo",1906],["go",1902],["ge",1860],["im",1855],["ti",1853],["as",1817]
We can't make this file beautiful and searchable because it's too large.
word,count
the,23135851162
of,13151942776
and,12997637966
to,12136980858
a,9081174698
in,8469404971
for,5933321709
is,4705743816
on,3750423199
@josephrocca
josephrocca / robin_sloan_scifi_word_freq.json
Created February 1, 2019 18:30
Sci-Fi word frequency list - English - From Robin Sloan's corpus: https://archive.org/details/scifi-corpus
This file has been truncated, but you can view the full file.
[["the",1525822],["and",674744],["of",652624],["to",640889],["a",632384],["he",397874],["in",365169],["i",355116],["it",345875],["was",334239],["that",289920],["you",271823],["his",234562],["had",197751],["for",185642],["on",177058],["with",168310],["but",167076],["as",156642],["at",147392],["is",147082],["be",139376],["said",129771],["they",128724],["not",127876],["have",119022],["were",110064],["him",110059],["from",106605],["we",105946],["this",104558],["all",103965],["she",99490],["one",97835],["there",96107],["out",95461],["her",92843],["what",90389],["no",87988],["if",87456],["by",86852],["me",85342],["up",83391],["an",81799],["would",80978],["my",76870],["or",72968],["them",72908],["been",71502],["could",70379],["its",69778],["so",69552],["are",69392],["into",65377],["then",63288],["like",62445],["do",62289],["about",61958],["your",59700],["now",58651],["when",57757],["time",56519],["back",53631],["their",53562],["more",52462],["know",48939],["can",47843],["only",46853],["just",46465],["will",46365],["
@josephrocca
josephrocca / videoToCanvasToVideo.js
Last active February 25, 2019 10:57
Put a video on a canvas, and then encode the canvas frames into a new video file
// Depends on these script files being available:
// /library/whammy.js
// /library/libwebp-0.1.3.min.js
// Some details here: https://stackoverflow.com/questions/52371970/changing-quality-of-mediarecorder-and-canvas-capturestream/52378272#52378272
// videoUrl must either be a blob object url, or a file hosted on the same domain as this script, or have proper CORS headers to prevent canvas being "tainted".
// Obviously this function is useless unless you add some code to change the output
// canvas/video (e.g. add effects, resize, etc.)
@josephrocca
josephrocca / setup.sh
Last active May 21, 2019 13:40
server setup script
#!/usr/bin/env bash
# Node v12
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs
# Kakoune
sudo apt install libncursesw5-dev pkg-config
git clone https://github.com/mawww/kakoune.git && cd kakoune/src
make
@josephrocca
josephrocca / BigMap.mjs
Last active June 10, 2019 16:49
BigMap.js
// This is very "hacked together" - not properly bug/security tested or anything.
// Slower than using @ronomon/hash-table directly, but still faster
// than V8 for strings right now: https://bugs.chromium.org/p/v8/issues/detail?id=9348
// and doesn't cause crash when memory usage pushes into dozens
// of GB: https://bugs.chromium.org/p/v8/issues/detail?id=9350
// You need to pass trackKeys:true into the constructor options argument object
// if you want to use map.keys() or the other iterators.
@josephrocca
josephrocca / only-adjectives.md
Last active July 20, 2019 10:23
Words that are only adjectives (no other part-of-speech)

I've taken this list and filtered out words that have other parts-of-speech according to wordpos. It went from 28,479 lines down to 23,799 lines. wordpos definitely isn't perfect, so it's likely that there are many words still in this list that have other part-of-speech values. Here's the code I used:

const fetch = require('node-fetch');
const WordPOS = require('wordpos');
const wordpos = new WordPOS();

(async function() {
  let text = await fetch("https://raw.githubusercontent.com/taikuukaits/SimpleWordlists/master/Wordlist-Adjectives-All.txt").then(r => r.text());
  let adjectives = text.replace(/\r/g, "").split("\n");
@josephrocca
josephrocca / getAllMatches.js
Last active May 26, 2020 15:41
JavaScript matchAll "polyfill"
// Not actually a polyfill. Just a function that gets all matches and puts them in an array. So I don't have to keep Googling for how exec works.
function getAllMatches(str, regex) {
const matches = [];
let m;
while(1) {
m = regex.exec(str);
if(m) matches.push(m);
else break;
}