Skip to content

Instantly share code, notes, and snippets.

View dannycroft's full-sized avatar

Danny Croft dannycroft

View GitHub Profile
function Words(str) {
this._str = str;
}
Words.prototype[Symbol.iterator] = function() {
var re = /\S+/g;
var str = this._str;
return {
next: function() {
Sinatra::Application.routes["GET"].each do |route|
before do
// All the things
end
end
@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);
}
]);
<!DOCTYPE html>
<title>Animations</title>
<body>
<script>
function createAndAnimate(part) {
var element = document.createElement('pre');
element.textContent = part;
document.body.appendChild(element);
element.animate(
// 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') {
@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;}())
}
var base64 = exports;
base64.encode = function (unencoded) {
return new Buffer(unencoded || '').toString('base64');
};
base64.decode = function (encoded) {
return new Buffer(encoded || '', 'base64').toString('utf8');
};
/**
* 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 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')
);