Skip to content

Instantly share code, notes, and snippets.

View garystorey's full-sized avatar
:octocat:

Gary Storey garystorey

:octocat:
View GitHub Profile
function camelCase( s ) {
return s.replace(/\-(\w)/g, function( i, m ){
return m.toUpperCase();
});
}
@garystorey
garystorey / extend.js
Created October 7, 2014 16:44
Extends one JS object from another
function extendDefaults(source, properties) {
var property;
for (property in properties) {
if (properties.hasOwnProperty(property)) {
source[property] = properties[property];
}
}
return source;
}
// ----
// Sass (v3.3.14)
// Compass (v1.0.1)
// ----
//helpers
$helpers: (
fll: (
float: left
),
@garystorey
garystorey / hammer.jquery.js
Created September 30, 2014 21:03
Convert hammer to jquery plugin
// jQuery plugin
$.fn.hammer = function (event, cb) {
var $el = this;
var hmr = new Hammer($el.get(0));
hmr.on(event, $.proxy(cb, $el));
};
// Usage
$('.some-element').hammer('tap', function (e) {
console.log(e);
@garystorey
garystorey / simple.js
Created September 17, 2014 16:41
instead of if..else to add and remove class, use toggle with logic
/* This */
if (this.scrollTop > 147) {
wrap.addClass("fix-search");
} else {
wrap.removeClass("fix-search");
}
/* Becomes This */
wrap.toggleClass("fix-search", this.scrollTop > 147);
/*
* Debug utilities
* @see https://gist.github.com/mkretschek/611475412faa996ef73d
*/
var $dbg;
(function () {
$dbg = {};
/*
* Accurately returns the height and width of the viewport
* much more reliably than simply $(window).height
*
* Source: http://andylangton.co.uk/blog/development/get-viewport-size-width-and-height-javascript
*/
function viewport() {
var e = window, a = 'inner';
@garystorey
garystorey / getcriticalstyles.js
Created August 4, 2014 18:05
Get critical styles via Gulp
var penthouse = require('penthouse');
var Promise = require("bluebird");
var penthouseAsync = Promise.promisify(penthouse);
gulp.task('critical', function(){
penthouseAsync({
url : 'http://daverupert.com/',
css : './stylesheets/style.css',
height: 480
}).then( function (criticalCSS){
@garystorey
garystorey / raf.js
Created August 4, 2014 18:04
From Googles PageSpeed Insight - loading css files
var cb = function() {
var l = document.createElement('link'); l.rel = 'stylesheet';
l.href = 'small.css';
var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
};
var raf = requestAnimationFrame || mozRequestAnimationFrame ||
webkitRequestAnimationFrame || msRequestAnimationFrame;
if (raf) raf(cb);
else window.addEventListener('load', cb);
@garystorey
garystorey / umd.js
Created July 30, 2014 15:36
UMD for javascript
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['b'], factory);
} else if (typeof exports === 'object') {
module.exports = factory;
} else {
root.amdWeb = factory(root.b);
}
}(this, function (b) {
//your code here