Skip to content

Instantly share code, notes, and snippets.

View joshuakemmerling's full-sized avatar

Joshua Kemmerling joshuakemmerling

View GitHub Profile
@joshuakemmerling
joshuakemmerling / global.css
Created January 5, 2019 08:05
Normalize.css supplement for a little nicer base setup.
html {
-webkit-font-smoothing: antialiased;
}
body {
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Helvetica, Arial, sans-serif;
line-height: 1.5;
}
h1, h2, h3, h4, h5, h6 {
@joshuakemmerling
joshuakemmerling / dependency-injection.js
Last active May 25, 2018 07:04
Dependency injection
var DI = function () {
let _deps = {}
this.dep = (name, func) => { _deps[name] = func }
this.getDep = (name) => _deps[name]
}
DI.prototype.inject = function (fn) {
let fnString = fn.toString().replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg, ''),
paramNames = fnString.split('(')[1].split(')')[0].split(',').map(v => v.trim()),
@joshuakemmerling
joshuakemmerling / groupby.js
Last active May 25, 2018 07:03
groupby function
/**
* Creates an object of groupings.
*
* @param {Function|String} f - The function that returns value to group by or object key.
* @returns {Object} Returns the composed aggregate object.
* @example
*
* array.groupBy('city');
* array.groupBy(function (obj) { return obj.city; });
* array.groupBy((obj) => { return obj.city; });
@joshuakemmerling
joshuakemmerling / hamming.js
Last active May 21, 2018 04:09
Get hamming numbers
const getNthHamming = n => getHammingNumbers(n).reverse()[0]
const getHammingNumbers = n => {
let numbers = []
let next = 1
while (numbers.length < n) {
if (isHammingNumber(next))
numbers.push(next)
@joshuakemmerling
joshuakemmerling / sort.js
Last active May 25, 2018 12:51
Multi Level Sorting with JavaScript
const multilevelsort = (records, order) => records.sort(chainSortBy(order))
const sort_by = ({ key, direction }) => {
return function (a, b) {
a = a[key]
b = b[key]
return a == b ? 0 : [-1, 1][+(direction === 'ascending')] * ((a > b) - (b > a))
}
}
@joshuakemmerling
joshuakemmerling / transpose.js
Last active May 25, 2018 07:08
JavaScript transpose(). Like Ruby transpose.
Array.prototype.transpose = function () {
return this.length === 0 ? this : this[0].map((col, i) => this.map((row) => row[i]))
}
@joshuakemmerling
joshuakemmerling / article.scss
Created August 27, 2016 15:11
Default typography for an article. Does not apply to the entire site.
article {
h1, h2, h3, h4, h5, h6, img, ul, ol { margin-bottom: 20px; }
h1 { font-size: 2em; line-height: 1.5; }
h2 { font-size: 1.5em; line-height: 1.5; }
h3 { font-size: 1.17em; line-height: 1.5; }
h4 { font-size: 1em; line-height: 1.5; }
h5 { font-size: 0.83em; line-height: 1.5; }
h6 { font-size: 0.67em; line-height: 1.5; }
strong { font-weight: bold; }
img { display: block; max-width: 100%; margin-bottom: 20px; }
@joshuakemmerling
joshuakemmerling / dynamic-urls.md
Last active May 25, 2018 12:49
Filter list of dynamic URLs.

Steps

1. Get incoming URL

var url = '/product/shoes/234'

2. Create regex from incoming URL

var regex = '/(product|:[a-zA-Z0-9]+)/(shoes|:[a-zA-Z0-9]+)/(234|:[a-zA-Z0-9+)'

@joshuakemmerling
joshuakemmerling / global.js
Last active October 1, 2019 01:12
Promise JS
new Promise(function (reject) {
}).then(function (reject) {
reject('this is an error');
}).then(function () {
console.log('never run');
}).catch(function (err) {
console.log('err', err);
}).finally(function () {
@joshuakemmerling
joshuakemmerling / global.js
Last active May 25, 2018 12:46
Vanilla pub/sub system.
events.publish('/page/load', {
url: '/some/url/path' // any argument
});
var subscription = events.subscribe('/page/load', function(obj) {
// Do something now that the event has occurred
});
// ...sometime later where I no longer want subscription...
subscription.remove();