Skip to content

Instantly share code, notes, and snippets.

Daniel Hug Daniel-Hug

Block or report user

Report or block Daniel-Hug

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View runninSum.js
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 Jun 7, 2018
JS function: convert objects in an array or object to rows in a CSV file
View to-csv.js
/*
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 Jun 7, 2018
Zip arrays into one | Example with two arrays: value 0 from a, value 0 from b, value 1 from a, etc.
View zip-arrays.js
// 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 Oct 30, 2017
CSS style: differentiate nested <mark> elements with a gamut of colors
View nested-colors.css
/* 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 Oct 17, 2017
JS function: check if two objects have the same properties
View have-same-properties.js
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 Oct 13, 2017
JS function: get gaps between numbers
View get-gaps.js
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 Oct 13, 2017
JS function: count number of numer of items in each run (strings of identical items)
View get-run-lengths.js
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 Aug 3, 2017
log messages to page maintaining whitespace formatting
View log.js
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 Aug 3, 2017
JS function: Create a string representation of a (nested) object for logging purposes
View display-props.js
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 Jun 26, 2017
JS function: create 2d array or multidimensional matrix w/o constructor
View create-matrix.js
// 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];
You can’t perform that action at this time.