Skip to content

Instantly share code, notes, and snippets.

View joshuacerbito's full-sized avatar
🔥
🎸🎛🎛🎚🔊

Joshua Cerbito joshuacerbito

🔥
🎸🎛🎛🎚🔊
View GitHub Profile
@joshuacerbito
joshuacerbito / sass_font-size_handling.scss
Created August 4, 2015 07:10
Handles font-size caculations
$base-font-size: 16px;
// Calculate rem value
@function calculateRem($size) {
$remSize: $size / $base-font-size;
@return $remSize * 1rem;
}
// Calculate Viewport-Width dependent value
@function calculateVW($size) {
function assert (condition, message) {
if (!condition) {
message = message || "Assertion failed";
if (typeof Error !== "undefined") {
throw new Error(message);
}
throw message; // Fallback
}
}
@joshuacerbito
joshuacerbito / objectSize.js
Last active May 19, 2016 02:14
Get the size of an object
Object.size = function ( obj ) {
var size = 0, key;
for (key in obj) {
if ( obj.hasOwnProperty(key) ) { size++; }
}
return size;
};
@joshuacerbito
joshuacerbito / selector.js
Created May 23, 2016 03:38
Native JS Object Selector
function $$(selector, context) {
context = context || document;
var elements = context.querySelectorAll(selector);
return Array.prototype.slice.call(elements);
}
@joshuacerbito
joshuacerbito / unless.js
Last active May 31, 2016 03:17
Performs a function unless the given expression is false
function unless(exp, func) {
if ( typeof func === 'function' && !exp ) {
func.apply(this, arguments);
}
return !exp;
}
@joshuacerbito
joshuacerbito / aspectRatio.js
Last active July 15, 2016 04:13
Get new width or height while preserving the aspect ratio
// Description: Get new width or height while preserving the aspect ratio
// TODO: Add AspectRatio related methods
var AspectRatio = {
getScaled: function ( currentW, currentH, newSize, property ) {
return ( property === 'width' )? ((currentH / currentW) * newSize) : (currentW / currentH) * newSize)
}
};
@joshuacerbito
joshuacerbito / limitToNumber.js
Created August 6, 2016 02:38
Limit text input to numeric values
textInput.addEventListener('keydown', function (e) {
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
(e.keyCode == 65 && e.ctrlKey === true) || // Allow: Ctrl+A
(e.keyCode == 67 && e.ctrlKey === true) || // Allow: Ctrl+C
(e.keyCode == 88 && e.ctrlKey === true) || // Allow: Ctrl+X
(e.keyCode >= 35 && e.keyCode <= 39)) // Allow: home, end, left, right
{ return; }
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
@joshuacerbito
joshuacerbito / shell_unhide.sh
Created September 10, 2016 11:41
Unhide all files from a directory; A fix for the common Windows virus. (Change the Volume and Directory name)
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
# set me
FILES=/Volumes/DATA/Drive/*/
for f in $FILES
do
chflags nohidden "$f"
done
# restore $IFS
IFS=$SAVEIFS
@joshuacerbito
joshuacerbito / get_set_data_attribute.js
Created September 28, 2016 05:33
IE-compatible data attribute getter/setter
Object.prototype.data = function (prop, val) {
var _return,
notIE = ( this.dataset !== undefined );
return ( notIE )? // not IE
( typeof val === "undefined" ) ? // get data value
this.dataset[prop] : this.dataset[prop] = val
: // is IE
( typeof val === "undefined" ) ? // get data value
this.getAttribute('data-' + prop) : this.setAttribute('data-' + prop, val);
@joshuacerbito
joshuacerbito / html5-dataset.js
Created September 28, 2016 06:30 — forked from brettz9/html5-dataset.js
Dataset Shim
/**
* Add dataset support to elements
* No globals, no overriding prototype with non-standard methods,
* handles CamelCase properly, attempts to use standard
* Object.defineProperty() (and Function bind()) methods,
* falls back to native implementation when existing
* Inspired by http://code.eligrey.com/html5/dataset/
* (via https://github.com/adalgiso/html5-dataset/blob/master/html5-dataset.js )
* Depends on Function.bind and Object.defineProperty/Object.getOwnPropertyDescriptor (polyfills below)
* All code below is Licensed under the X11/MIT License