Skip to content

Instantly share code, notes, and snippets.

@uhop
uhop / roll.js
Created February 14, 2020 17:23
How many tries it takes to roll a particular sequence
'use strict';
const roll = pattern => {
const samples = [];
let counter = 0;
for(;;){
if (samples.length >= pattern.length) {
samples.shift();
}
const sample = Math.floor(Math.random() * 6) + 1;
@uhop
uhop / getAWS.js
Last active June 17, 2021 22:24
Script to set up AWS environment according to its profile.
'use strict';
const AWS = require('aws-sdk');
let profile = process.env.LAMBDA_TASK_ROOT && process.env.AWS_EXECUTION_ENV ? '' : 'default';
const profileIndex = process.argv.indexOf('--profile');
if (profileIndex > 0 && profileIndex + 1 < process.argv.length) {
profile = process.argv[profileIndex + 1];
}
@uhop
uhop / distributions.js
Last active October 19, 2020 03:09
Useful scripts to deal with AWS CloudFront distributions by the associated domain names and invalidations.
'use strict';
const iterate = async function*(client) {
const params = {MaxItems: '100'};
for (;;) {
const data = await client.listDistributions(params).promise(),
list = data.DistributionList,
items = list.Items;
if (items) {
yield items;
@uhop
uhop / Cache.js
Last active October 14, 2019 15:40
Simple cache implementation.
'use strict';
const List = require('./List');
class Cache {
constructor(capacity = 10) {
this.capacity = capacity;
this.size = 0;
this.list = new List();
this.dict = {};
@uhop
uhop / SplayTree.js
Last active October 11, 2019 21:47
Simple implementation of a splay tree.
'use strict';
const zig = tree => {
const newTree = tree.l,
parent = (newTree.p = tree.p);
if (parent) {
if (parent.l === tree) parent.l = newTree;
else parent.r = newTree;
}
tree.p = newTree;
@uhop
uhop / List.js
Last active October 11, 2019 21:46
Simple yet complete implementation of a double-linked list.
'use strict';
class ListNode {
constructor() {
this.prev = this.next = this;
}
}
const pop = head => {
const rest = head.next;
@uhop
uhop / Heap.js
Last active October 9, 2019 23:55
Simple heap implementation
'use strict';
// using heap implementation from https://github.com/heya/ctr under the BSD-3 license
class Heap {
constructor(less = (a, b) => a < b, arrayLike = []) {
this.less = less;
this.array = Heap.make(Array.from(arrayLike), this.less);
}
@uhop
uhop / XmlBuilder.js
Last active April 23, 2020 16:52
Simple generic XML builder and utilities.
'use strict';
// Loosely based on JSONx: https://tools.ietf.org/html/draft-rsalz-jsonx-00
const escapeValueDict = {'&': '&amp;', '<': '&lt;'};
const escapeValue = s => ('' + s).replace(/[&<]/g, m => escapeValueDict[m]);
const escapeAttrDict = {'&': '&amp;', '<': '&lt;', '"': '&quot;'};
const escapeAttr = s => ('' + s).replace(/[&<"]/g, m => escapeAttrDict[m]);
@uhop
uhop / build-index.js
Last active April 29, 2021 12:19
Modern/legacy builds with webpack
'use strict';
if (process.argv.length < 3) {
console.log('Usage: node build-index.js inFile outFile');
console.log(' All file names are relative to the project directory.')
console.log('Example: node build-index.js src/index.html docs/index.html');
process.exit(1);
}
const fs = require('fs');
@uhop
uhop / stream-merge.js
Last active July 26, 2018 23:49
Merge two corresponding streams by keys
'use strict';
const {Readable} = require('stream');
const merge = (s1, s2) => {
s1.pause();
s2.pause();
let item1 = null,
item2 = null,