Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug
Daniel-Hug / dom-helpers.js
Last active May 22, 2019 14:32
DOM helpers: Query elements (qs, qsa), Add and remove event listeners (on, off, once), Empty element of its child nodes: (removeChilds), Efficiently render lists to DOM: (renderMultiple), Prepend instead of append DOM nodes (prependAInB)
/* DOM helpers
**************************************/
// Get elements by CSS selector:
function qs(selector, scope) {
return (scope || document).querySelector(selector);
}
function qsa(selector, scope) {
return (scope || document).querySelectorAll(selector);
}
function runningSum(addends) {
var sums = [addends[0]];
for (var i=1; i<addends.length; i++) {
sums.push(addends[i]);
}
return sums;
}
@Daniel-Hug
Daniel-Hug / array-to-object.js
Last active October 7, 2018 10:07
JS arrayToObj(): convert array to object with a key-value map function
// Call arrayToObj() passing your array and function.
function arrayToObj(array, keyValueMap) {
var obj = {};
var len = array.length;
// Your function will be called for each item in the array.
for (var i = 0; i < len; i++) {
var curVal = array[i];
// Just like with [].forEach(), your function will be passed the
// curent item of the array, its index, and the array it's in.
var keyValuePair = keyValueMap(curVal, i, array);
@Daniel-Hug
Daniel-Hug / zip-arrays.js
Last active June 7, 2018 04:05
Zip arrays into one | Example with two arrays: value 0 from a, value 0 from b, value 1 from a, etc.
// Zip arrays into one
// Example with two arrays: value 0 from a, value 0 from b, value 1 from a, etc.
function zipArrays() {
var zipped = [];
var arrays = [].slice.call(arguments);
for (var valueI = 0; arrays.length > 0; valueI++) {
for (var arrayI = 0; arrayI < arrays.length; arrayI++) {
if (arrays[arrayI].length - 1 < valueI) {
arrays.splice(arrayI, 1);
continue;
@Daniel-Hug
Daniel-Hug / to-csv.js
Last active June 7, 2018 04:04
JS function: convert objects in an array or object to rows in a CSV file
/*
usage:
var csvString = toCSV({
key1: { col1: 5, col2: 'hi' },
key2: { col1: 7, col2: 'hello' }
}, {
// default config:
includeKeys: true,
delimiter: ',',
@Daniel-Hug
Daniel-Hug / counting-sort.js
Last active November 21, 2017 00:55
JS functions: linear time counting sort for integers
function countingSort(arr, min, max) {
var i, z = 0, count = [];
for (i = min; i <= max; i++) {
count[i] = 0;
}
for (i=0; i < arr.length; i++) {
count[arr[i]]++;
}
for (i = min; i <= max; i++) {
while (count[i]-- > 0) {
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= documentTitle %></title>
<link rel="stylesheet" href="https://daniel-hug.github.io/files/stackedit/base.css" />
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body><div class="container"><%= documentHTML %></div></body>
@Daniel-Hug
Daniel-Hug / css-in-js.js
Last active October 30, 2017 22:53
JS function: embed multi-line CSS in JS
// pass a function-encased multi-line comment
// returns the multi-line string in the comment
function hereDoc(f) {
// return the function's representation as a string
return f.toString()
// except, if the string starts with one or more characters other than a forward slash
// which are followed by "/*" and then optionally followed by "!", remove this whole prefix.
.replace(/^[^/]+\/\*!?/, '')
// and, if "*/" appears followed by one or more characters other
// than a forward slash to end the string, remove this whole suffix.
@Daniel-Hug
Daniel-Hug / multi-line-string.js
Last active October 30, 2017 22:52
Use Function.prototype.toString for multi-line strings in JS.
// pass a function-encased multi-line comment
// returns the multi-line string in the comment
function hereDoc(f) {
// return the function's representation as a string
return f.toString()
// except, if the string starts with one or more characters other than a forward slash
// which are followed by "/*" and then optionally followed by "!", remove this whole prefix.
.replace(/^[^/]+\/\*!?/, '')
// and, if "*/" appears followed by one or more characters other
// than a forward slash to end the string, remove this whole suffix.