Skip to content

Instantly share code, notes, and snippets.

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 / 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 / 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 / nested-colors.css
Created October 30, 2017 22:34
CSS style: differentiate nested <mark> elements with a gamut of colors
/* differentiate nested <mark> elements with a gamut of colors */
mark {
color: #000;
transition: background-color 0.2s ease-in;
}
mark,
mark:hover,
mark:hover mark {
background: hsl(0, 100%, 90%);
@Daniel-Hug
Daniel-Hug / have-same-properties.js
Created October 17, 2017 06:36
JS function: check if two objects have the same properties
function getProperties(obj) {
var newObj = {};
for (var key in obj) {
newObj[key] = true;
}
return newObj;
}
function haveSameProperties(a, b) {
return JSON.stringify(getProperties(a)) === JSON.stringify(getProperties(b));
@Daniel-Hug
Daniel-Hug / get-gaps.js
Created October 13, 2017 07:17
JS function: get gaps between numbers
function getGaps(nums) {
var gaps = [];
for (var i = 1; i < nums.length; i++) {
gaps.push(nums[i] - nums[i-1]);
}
return gaps;
}
@Daniel-Hug
Daniel-Hug / get-run-lengths.js
Created October 13, 2017 01:21
JS function: count number of numer of items in each run (strings of identical items)
function getRunLengths(array) {
var runLengths = [];
var curValue;
for (var i = 0; i < array.length; i++) {
if (array[i] === curValue) {
runLengths[runLengths.length - 1]++;
}
else {
runLengths.push(1);
curValue = array[i];
@Daniel-Hug
Daniel-Hug / log.js
Created August 3, 2017 23:59
log messages to page maintaining whitespace formatting
var log = (function() {
function qs(selector, scope) {
return (scope || document).querySelector(selector);
}
var log = qs('#log');
return function(msg) {
var p = document.createElement('p');
p.style.whiteSpace = 'pre';
@Daniel-Hug
Daniel-Hug / display-props.js
Created August 3, 2017 23:56
JS function: Create a string representation of a (nested) object for logging purposes
function displayProps(obj, indent) {
indent = indent || '\t';
var str = ({}).toString.call(obj).slice(8,-1) + ':\n';
for (var key in obj) {
var val = typeof obj[key] === 'object' ?
displayProps(obj[key], indent + '\t') : obj[key];
str += indent + key + ': ' + val + '\n';
}
return str;
}
@Daniel-Hug
Daniel-Hug / create-matrix.js
Created June 26, 2017 07:11
JS function: create 2d array or multidimensional matrix w/o constructor
// create 2d array or multidimensional matrix
function createMatrix() {
var matrix = [];
matrix.add = function(val, i1, i2/*, ...*/) {
var cur = matrix, curI;
// loop from 2nd argument to 2nd to last
for (var i = 1; i < arguments.length - 1; i++) {
curI = arguments[i];