Skip to content

Instantly share code, notes, and snippets.

View xdmorgan's full-sized avatar
Unverified

Dan Morgan xdmorgan

Unverified
View GitHub Profile
@xdmorgan
xdmorgan / balanced-columns.ts
Created January 29, 2019 02:13
Attempt to balance an array into even columns;
const balance = (data: any[], columns: number = 4) => {
const offset = data.length % columns ? 1 : 0
const height = (data.length - (data.length % columns)) / columns + offset
const unbalanced = [...data]
const balanced = []
while (unbalanced.length) balanced.push(unbalanced.splice(0, height))
return balanced
}
@xdmorgan
xdmorgan / optional-in-object.js
Created October 2, 2018 01:08
Optional in nested Object
function optional(obj: any, ...keys: string[]) {
let opt = obj;
while (keys.length && opt[keys[0]] !== undefined) {
const key = keys.shift();
opt = opt[key];
}
return keys.length ? false : opt;
}
const scrollHeight = optional(someDOMNode, "previousElementSibling", "scrollHeight");
@xdmorgan
xdmorgan / expressions.py
Last active July 24, 2018 00:07
FoC Exercises: Python 2 Expressions
# Coming soon
@xdmorgan
xdmorgan / raw.css
Created February 27, 2018 20:13
Testing styles over the internet, fam.
h1, .h1 {
color: royalblue;
}
a, a:link {
color: orangered;
}
@xdmorgan
xdmorgan / NIBEXAMPLE.swift
Last active August 16, 2016 13:20
Swift 2 IBDesignable Nib Tutorial / Boilerplate
/*
Start by creating two files (replace NIBEXAMPLE with w/e you want but make sure the names match):
1, NIBEXAMPLE.xib
* File > New > File... > iOS > View
2, NIBEXAMPLE.swift
* File > New > File... > iOS > Cocoa Touch Class (Extends UIView)
After creating the two files above, replace the default contents of your .swift file
with the code below. Be sure to update `class NIBEXAMPLE` and `nibName = "NIBEXAMPLE"`
@xdmorgan
xdmorgan / random.string.js
Last active September 9, 2015 22:29
Generates a random string, in this case an ID.
/* Optional customization ==========================
generateID();
generateID({
base: 'video_',
length: 12,
charset: '1234asdf',
overkill: true
});
@xdmorgan
xdmorgan / _grid.scss
Last active August 29, 2015 14:27
A simple and flexible Sass grid.
$create-grid: true;
// Breakpoints
$break-mobile: 320px;
$break-mobile-max: 460px;
$break-tablet: 760px;
$break-tablet-max: 900px;
$break-desktop: 1104px;
$break-desktop-max: 1320px;
@xdmorgan
xdmorgan / modulate.js
Last active August 29, 2015 14:26
Got a framer prototype with this handy function in it, tried writing my own, judging by my use-case it mimics Framer's modulate perfectly, however, I should to run it through some unit tests see if it always does. [feedback welcome]
// map a value from one range onto another, in this case scrolling
var st = _private.getScrollTop(),
m1 = _private.modulate( st, [0, 500], [0,1] ),
m2 = _private.modulate( st, [0, 400], [1,0] ),
m3 = _private.modulate( st, [0, 300], [0,3] );
_private.modulate = function(val, range1, range2){
var min1 = range1[0], max1 = range1[1],
min2 = range2[0], max2 = range2[1],
@xdmorgan
xdmorgan / lol.konami.js
Created May 30, 2014 20:53
Based on chris coyiers konami code, adds konami class to html to override styles and triggers jquery event "KONAMI_CODE" for js fun.
var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
$(document).keydown(function(e) {
kkeys.push( e.keyCode );
if ( kkeys.toString().indexOf( konami ) >= 0 ) {
$(document).unbind('keydown',arguments.callee);
@xdmorgan
xdmorgan / validate-email-function.js
Created February 5, 2014 17:57
regex, handles everything i've thrown at it (that was a legit email) including + signs
function validateEmail(email) {
var re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}