Skip to content

Instantly share code, notes, and snippets.

@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 / geom.js
Last active November 11, 2020 23:18
All necessary Pie chart calculations based on dojox.charting.
var TWO_PI = 2 * Math.PI;
function tmpl (template, dict) {
return template.replace(/\$\{([^\}]*)\}/g, function (_, name) {
return dict[name];
});
}
function makeSegment (args, options) {
// args is {startAngle, angle, index, className}
@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 / ListHead.js
Last active October 19, 2020 02:59
Simple yet complete implementation of a double-linked list that reuses objects building lists with their properties.
'use strict';
class ListHead {
constructor(head = null, next = 'next', prev = 'prev') {
if (head instanceof ListHead) {
({nextName: this.nextName, prevName: this.prevName, head: this.head} = head);
return;
}
this.nextName = next;
this.prevName = prev;
@uhop
uhop / detectWebp.js
Last active July 4, 2020 15:28
Image-related utilities for use in browsers.
let flag;
const detectWebp = () => {
if (typeof flag == 'boolean') return flag;
const canvas = document.createElement('canvas');
flag = !!(canvas.getContext && canvas.getContext('2d') && /^data:image\/webp;/.test(canvas.toDataURL('image/webp')));
return flag;
};
export default detectWebp;
@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 / 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 / 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;