Skip to content

Instantly share code, notes, and snippets.

@zapthedingbat
zapthedingbat / poll-xid.js
Created Jun 25, 2020
Poll for the Conde Nast XID and pass it into permutive
View poll-xid.js
(function () {
var interval = setInterval(function () {
var [, xid] = (document.cookie.match(/CN_xi_d=([^;]+)/) || []);
xid = xid || window._4d && window._4d.user && window._4d.user.xid;
if (xid) {
permutive.identify([
{
id: xid,
tag: 'xid'
}
View morse-code.js
console.log(process.argv.slice(2).map(b=>[...b].reduce((a,b)=>2*a+("."==b?1:2),0)).map(a=>"-etianmsurwdkgohvf-l-pjbxcyzq--54-3---2--+----16=/-----7---8-90"[a]).join(''))
@zapthedingbat
zapthedingbat / watercolors.js
Created Nov 13, 2019
Generate watercolor style blobs: Inspired by tyler hobbs's generative watercolors- https://tylerxhobbs.com/
View watercolors.js
(function(doc) {
const TWO_PI = Math.PI * 2;
const HALF_PI = Math.PI / 2;
const RADIUS_SCALE = 0.05;
const RADIUS_SD = 15;
const POLYGON_SIDES = 5;
const POSITION_SD = 0.04;
const BASE_DEFORMATIONS = 3;
const LAYER_DEFORMATIONS = 3;
const LAYERS = 40;
@zapthedingbat
zapthedingbat / number-bytes.js
Created May 14, 2019
converting between numbers and bytes in javascript
View number-bytes.js
function bitLength(number) {
return Math.floor(Math.log2(number)) + 1;
}
function byteLength(number) {
return Math.ceil(bitLength(number) / 8);
}
function toBytes(number) {
if (!Number.isSafeInteger(number)) {
View tf-idf.js
function getTermKey(term) {
return term
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, "")
.replace(/[^\w]/g, "")
.toLowerCase()
};
function getTermsIn(document) {
return document.split(/[\s_():.!?,;]+/)
@zapthedingbat
zapthedingbat / README.md
Created Sep 26, 2018
Example reproduction of wallabyjs missing the setTimeout[util.promisify.custom] property used by the native node util.promisify module.
View README.md

Example reproduction of wallabyjs missing the setTimeout[util.promisify.custom] property

When running tests in wallaby the setTimeout[util.promisify.custom] property is missing, which causes util.promisify to fail.

Reproduction steps

Running this gist with the command line npm test will result in a passing test.

View count-words.js
Array.from(s.split(/\W+/)
.map(a => a.toLowerCase())
.reduce((p, v) => p.set(v, (p.has(v)?p.get(v):0)+1), new Map())
.entries()
)
.sort((a,b) => b[1]-a[1])
.forEach(a => console.log(`${a[0]}:${a[1]}`));
@zapthedingbat
zapthedingbat / Highlight.html
Created May 12, 2017
Highlight effect on <em> tags
View Highlight.html
<html>
<head>
<style>
em:before,
em:after{
content: " ";
display: block;
top:0;
bottom: 0;
View bookmarklet.js
console.log('bookmarklet');
@zapthedingbat
zapthedingbat / birthday.js
Created Feb 24, 2017
Calculating the probability that a member of a team shares their birthday with someone else in the same team.
View birthday.js
//https://en.wikipedia.org/wiki/Birthday_problem#Calculating_the_probability
function p(n){
let a = 1;
for(let i = 0; i < n; i++){
a *= ((365 - i) / 365);
}
return 1 - a;
}
const chance = p(parseInt(process.argv.pop())) * 100;
console.log(`There is a ${chance}% chance of two people in the same team sharing the same birthday`);
You can’t perform that action at this time.