Skip to content

Instantly share code, notes, and snippets.

View geraintluff's full-sized avatar

Geraint geraintluff

View GitHub Profile
// Quick'n'dirty EventEmitter class
function EventEmitter() {
}
EventEmitter.prototype = {
on: function (event, listener) {
this._events = this._events || {};
this._events[event] = this._events[event] || [];
this._events[event].push(listener);
this.emit('newListener', event, listener);
return this;
@geraintluff
geraintluff / findin.sh
Last active August 29, 2015 13:57
findin - add to ~/.bash_profile or ~/.bashrc
findin() {
find . -iname $1 -exec grep -Hin $2 {} \;
}
var tv4coerce = require('tv4-coerce');
tv4coerce.addFix(tv4coerce.errorCodes.INVALID_TYPE, function (data, expectedType) {
if (typeof data === 'string' && expectedType === 'array') {
return data.split(',');
}
});
var data = "1,2,3";
var schema = {"type": "array", "items": {"type": "number"}};
@geraintluff
geraintluff / tv4-asyncFetchSchemas.js
Last active August 29, 2015 14:01
A little tv4 extension that can pre-fetch all required schemas.
tv4.asyncFetchSchemas = function (urls, callback) {
var activeRequests = 0;
var attemptedRequest = {};
if (!Array.isArray(urls)) urls = [urls];
function requestFinished(url) {
activeRequests--;
var missing = tv4.getMissingUris();
for (var i = 0; i < missing; i++) {
@geraintluff
geraintluff / asyncReplace.js
Created August 28, 2014 09:18
Asynchronous replace() for JavaScript
String.prototype.asyncReplace = function (subStr, replacer, callback) {
var str = this;
var replacements = {};
var pending = 0;
var checkDone = function () {
if (!--pending) {
var result = str.replace(subStr, function (match) {
var args = Array.prototype.slice.call(arguments, 0);
var pos = args[args.length - 2];
var key = pos + '-' + match.length;
@geraintluff
geraintluff / crush.js
Created November 20, 2014 19:40
JavaScript Crusher
// Copyright 2014 Geraint Luff
// MIT Licensed
function crush(code, searchLimit) {
var originalCode = code;
searchLimit = searchLimit || 15;
var substitutions = [];
var bannedChars = /[\r\n"'\\]/;
for (var charNum = 32; charNum < 127; charNum++) { // ASCII printable ("safe") characters
var char = String.fromCharCode(charNum);
@geraintluff
geraintluff / README.md
Last active August 29, 2015 14:10
AMD define() and require() in 550 bytes

Asynchronous Module Definitions, in 550 bytes

This provides implementations of Asynchronous Module Definition's define() and require() functions. The minified code comes out around 550 bytes.

This enables module definitions, but it doesn't actually fetch any modules that are missing. If you want to automatically fetch modules, see "Extending" below.

The "CommonJS"-style hacks are not supported (e.g. special behaviour for dependencies "module", "require" or "exports").

Usage

@geraintluff
geraintluff / data-url.txt
Created December 31, 2014 11:59
Small single HTML file that securely loads libraries (SHA256-verified). User can then drag files onto the page and they appear PGP-encrypted in a DropBox account.
data:text/html,%3Cbody%3E%3Cscript%3Efunction%20sha256(a)%7Bfunction%20b(a,b)%7Breturn%20a%3E%3E%3Eb%7Ca%3C%3C32-b%7Dfor(var%20c,d,e=Math.pow,f=e(2,32),g=%22length%22,h=%22%22,i=%5B%5D,j=8*a%5Bg%5D,k=%5B%5D,l=%5B%5D,m=0,n=%7B%7D,o=2;64%3Em;o++)if(!n%5Bo%5D)%7Bfor(c=0;313%3Ec;c+=o)n%5Bc%5D=o;k%5Bm%5D=e(o,.5)*f%7C0,l%5Bm++%5D=e(o,1/3)*f%7C0%7Dfor(a+=%22%5Cx80%22;a%5Bg%5D%2564-56;)a+=%22%5Cx00%22;for(c=0;c%3Ca%5Bg%5D;c++)%7Bif(d=a.charCodeAt(c),d%3E%3E8)return;i%5Bc%3E%3E2%5D%7C=d%3C%3C(3-c)%254*8%7Dfor(i%5Bi%5Bg%5D%5D=j/f%7C0,i%5Bi%5Bg%5D%5D=j,d=0;d%3Ci%5Bg%5D;)%7Bvar%20p=i.slice(d,d+=16),q=k;for(k=k.slice(0,8),c=0;64%3Ec;c++)%7Bvar%20r=p%5Bc-15%5D,s=p%5Bc-2%5D,t=k%5B0%5D,u=k%5B4%5D,v=k%5B7%5D+(b(u,6)%5Eb(u,11)%5Eb(u,25))+(u&k%5B5%5D%5E~u&k%5B6%5D)+l%5Bc%5D+(p%5Bc%5D=16%3Ec?p%5Bc%5D:p%5Bc-16%5D+(b(r,7)%5Eb(r,18)%5Er%3E%3E%3E3)+p%5Bc-7%5D+(b(s,17)%5Eb(s,19)%5Es%3E%3E%3E10)%7C0),w=(b(t,2)%5Eb(t,13)%5Eb(t,22))+(t&k%5B1%5D%5Et&k%5B2%5D%5Ek%5B1%5D&k%5B2%5D);k=%5Bv+w%7C0%5D.concat(k),k%5B4%5D=k%5B4%5D+v%7C0%7Dfor(c=0
@geraintluff
geraintluff / index.html
Created April 28, 2015 17:56
Page to generate self-decrypting web pages
<style>
body {
margin: 0;
padding: 0;
}
a {
display: block;
}
</style>
<div id="drop" style="width: 100%; height: 69%; margin: 0; padding-top: 30%; text-align: center; font-family: Tahoma, sans-serif;">
@geraintluff
geraintluff / README.md
Last active August 29, 2015 14:22
Your Unix machine is awaited in Valhalla - witness!

The shutdown messages on the Mac/Linux command line are baked into the /sbin/shutdown executable.

This replaces the messages with suitable Mad Max: Fury Road quotes. You can make your own, but since you're patching a binary file the replacements MUST, I repeat MUST be exactly the same number of characters as the original, or your computer will not ride eternal, shiny and chrome.

The two versions below are for a Ubuntu Linux variant, and Mac OS X Yosemite (both versions Work For Me on at least one machine).