Skip to content

Instantly share code, notes, and snippets.

@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,
@uhop
uhop / rich.html
Created May 14, 2018 19:11
Rich text editor sketch
<!doctype html>
<html>
<head>
<title>Rich text editor demo</title>
<script src="./rich.js" defer></script>
<style>
#editor[contentEditable=true] {
padding: 0.5em;
margin: 1em;
}
@uhop
uhop / Component.js
Created June 12, 2017 22:14
React to Web Components bridge
class Component extends HTMLElement {
constructor () {
super();
this.addEventListener('click', this.changeBackground.bind(this));
this.addEventListener('transitionend', this.revertBackground.bind(this));
}
static get observedAttributes () {
return ['text'];
}
@uhop
uhop / dnd.js
Last active March 14, 2017 20:07
The minimalistic DnD code.
(function () {
'use strict';
window.dnd = window.dnd || {};
function Move (container, options, node, e) {
this.container = container;
this.options = options;
this.node = node;
@uhop
uhop / .bash_aliases
Created July 3, 2016 20:19
My bash setup
# commands from http://www.askapache.com/linux/bash_profile-functions-advanced-shell.html
#alias chmod='command chmod -c'
alias cpr='command cp -rpv'
alias df='command df -kh'
alias df1='command df -ia'
alias diff='diff -up'
alias dsiz='du -sk * | sort -n --'
alias du='command du -kh'
alias du1='echo *|tr " " "n" |xargs -iFF command du -hs FF|sort'