Skip to content

Instantly share code, notes, and snippets.

@willread
willread / gist:3ac901949c0b7b7d999c
Created January 5, 2015 19:38
Use watchers in an angular service
angular.module('app')
.service('myService', function($rootScope) {
// Inject an isolate scope into the rootscope to
// enable us to use $watch to from a service
this.scope = $rootScope.$new(true);
this.scope.myService = this;
this.scope.$watch('something', function() {});
@willread
willread / gist:670d717ccca32e4f2131
Last active August 29, 2015 14:07
Authenticating static files in nginx with a REST api
location / {
proxy_pass http://localhost:3000;
auth_request /api-qa/user;
}
error_page 401 /auth.html;
error_page 403 /auth.html;
location ~ /api|api|auth|css|fonts|favicon|images/ {
auth_request off;
@willread
willread / gist:404eac6fb3104976b000
Last active August 29, 2015 14:05
Generate a rotating loading gif from a single frame
convert -gravity center -distort SRT 45 loading-1.png loading-2.png
convert -gravity center -distort SRT 90 loading-1.png loading-3.png
convert -gravity center -distort SRT 135 loading-1.png loading-4.png
convert -gravity center -distort SRT 180 loading-1.png loading-5.png
convert -gravity center -distort SRT 225 loading-1.png loading-6.png
convert -gravity center -distort SRT 270 loading-1.png loading-7.png
convert -gravity center -distort SRT 315 loading-1.png loading-8.png
convert -channel A -dither Riemersma -delay 10 loading-*.png loading.gif
@willread
willread / gist:8851212
Last active August 29, 2015 13:56
Dynamic Base HREF's for hosting single page apps under variable paths (ie: google docs)
<script type="text/javascript">
document.write("<base href='" + document.location.href.replace("index.html", "").split("#")[0] + "' />");
</script>
@willread
willread / gist:7031438
Created October 17, 2013 20:11
Why does foo = "" while obj = {foo: "bar"}?
var getFoo = function(){
var val = "";
$.get(".", function(){
val = {foo: "bar"};
});
return val;
}
var foo = getFoo();
// foo = ""
@willread
willread / pre-commit.sh
Last active December 25, 2015 15:19
Pre-commit test running via git hooks and npm.
#!/usr/bin/env bash
NUM_ERRORS=`npm test | grep -c ERR`
if [ $NUM_ERRORS -ne 0 ]
then
echo -e "One or more tests failed, commit aborted"
exit 1
else
echo -e "All tests passed, proceeding with commit"
@willread
willread / gist:6690078
Created September 24, 2013 19:33
Send email with nodemailer
var nodemailer = require("nodemailer");
var transport = nodemailer.createTransport("SMTP", {
service: "Gmail",
auth: {
user: "[email protected]",
pass: "******"
}
});
@willread
willread / stripTrailingCommas
Created August 22, 2013 16:10
Regex to strip trailing commas from objects and arrays, because those are bad, mkay?
var s = "{ a: 0, b: [ 1, 2, 3, ], }";
s = s.replace(/\,(\s+)(\]|\})/g, "$1$2");
@willread
willread / gist:6253216
Created August 16, 2013 20:22
Show number of git commits by author
git shortlog -s -n
@willread
willread / gist:6150274
Created August 4, 2013 13:02
Magical pseudo-random number generator
private float seed = 1.0f;
float PseudoRandom(){
seed = (seed * 9301 + 49297) % 233280;
return seed / 233280;
}