Skip to content

Instantly share code, notes, and snippets.

View dannycroft's full-sized avatar

Danny Croft dannycroft

View GitHub Profile
@dannycroft
dannycroft / exclude-node-modules
Last active February 27, 2019 18:25
Exclude node_modules out of Sublime Text 2 searches
// Add the following to your preferences file
"folder_exclude_patterns":[".git","node_modules"]
@dannycroft
dannycroft / chunk.lodash.js
Created November 25, 2013 19:28
Lodash / Underscore method for breaking data sets into smaller sets (chunks)
/**
* Lodash / Underscore method for breaking data sets into smaller sets (chunks)
*
* @arguments
*
* collection: Array / Object
* chuckSize: Number
*
* @example
*
@dannycroft
dannycroft / object.keys.js
Created December 5, 2013 08:45
Object.keys pollyfill
Object.keys = function (obj) {
var keys = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
keys.push(key);
}
}
return keys;
};
var app = express.createServer(
(function(req, res, next) {
if(req.url.indexOf("/stylesheets/") === 0) {
res.setHeader("Cache-Control", "public, max-age=345600"); // 4 days
res.setHeader("Expires", new Date(Date.now() + 345600000).toUTCString());
}
return next();
}),
express.static(__dirname + '/static')
);
/**
* Get content from a URL (e.g. JavaScript) using RSVP promises
*/
var getUrl = function (url) {
var xhr = new XMLHttpRequest();
// @see https://github.com/tildeio/rsvp.js
var promise = new RSVP.Promise();
xhr.open('GET', url);
var base64 = exports;
base64.encode = function (unencoded) {
return new Buffer(unencoded || '').toString('base64');
};
base64.decode = function (encoded) {
return new Buffer(encoded || '', 'base64').toString('utf8');
};
@dannycroft
dannycroft / wrap.js
Created February 12, 2014 14:45
View how Chrome wraps all JavaScript entered into the Developer Tools
// Open Dev Tools console and enter
!(function a(){ debugger; }());
// Then you should see the wrapping
with ((console && console._commandLineAPI) || {}) {
!(function a(){ debugger;}())
}
// dollar selector
function $(C, P) {
P = P || document;
return /:first$/.test(C) ? (P = P.querySelector(C.slice(0, -6))) ? [P] : [] : [].slice.call(P.querySelectorAll(C))
}
// simple utility to add an event listener
$.on = function (CSS, parentNode, type, handler, capture) {
// in case parentNode is missing
if (typeof type !== 'string') {
<!DOCTYPE html>
<title>Animations</title>
<body>
<script>
function createAndAnimate(part) {
var element = document.createElement('pre');
element.textContent = part;
document.body.appendChild(element);
element.animate(
@dannycroft
dannycroft / angular-ga.js
Created October 16, 2014 16:02
Angular GA
(function(angular) {
angular.module('analytics', ['ng']).service('analytics', [
'$rootScope', '$window', '$location', function($rootScope, $window, $location) {
var track = function() {
$window._gaq.push(['_trackPageview', $location.path()]);
};
$rootScope.$on('$viewContentLoaded', track);
}
]);