View to-csv.js
/*
usage:
var csvString = toCSV({
key1: { col1: 5, col2: 'hi' },
key2: { col1: 7, col2: 'hello' }
}, {
// default config:
includeKeys: true,
delimiter: ',',
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;
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%);
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));
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;
}
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];
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';
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;
}
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];
View stringify-table.js
function stringifyTable(arr) {
var longest = 0;
return arr.map(function(innerArr) {
innerArr = trimArray(innerArr).map(val => '' + val);
longest = Math.max(longest, Math.max(innerArr.map(val => val.length)));
return innerArr;
}).map(function(innerArr) {
return innerArr.map(function(val) {
return padLeft('' + val, longest, ' ');
}).join();