Skip to content

Instantly share code, notes, and snippets.

@patcullen
patcullen / JSONValidate.js
Created May 28, 2013 20:33
A function to validate JSON objects against a schema.
// used in schema valiadtion for json objects
String.prototype.fromCamelToLabel = function() {
return this[0].toUpperCase() + this.substring(1).replace(/[A-Z]/g, function(h){
return " "+h;
});
};
var jv = {};
// Validate an object against a predefined schema.
<html><head>
<script src="mootools.js"></script>
<script src="loading-mt-office-x-min.js"></script>
<script>
window.addEvent('domready', function(){
$('btnRequest').addEvent('click', function() {
var anim = loading.start($('btnRequest'));
fakeXHR('goesNowhere.php', function(result) {
$('txtResult').set('text', result);
anim.stop();
<html><head>
<script src="sea.js"></script>
<script src="jquery.js"></script>
<script>
$(function(){
function fakeXHR(url, callback) {
setTimeout(function(){callback('This is some pseudo XHR content. Random number: '+Math.random());}, Math.random()*1000+500);
}
seajs.use('loading', function(loading) {
$('#btnRequest').click(function() {
@patcullen
patcullen / example.js
Created February 3, 2014 16:07
pst-obj is a cheap and quick utility for setting up a persistence layer in your app for node.
// Start ye'r engines
var
http = require('http'),
persist = require('pst-obj'),
state = { hits: 0 }
;
// api/layer to interface with state object
function getPageCount() {
state.hits++;
@patcullen
patcullen / settings.php
Created March 4, 2014 07:45
ImageMachine settings.php sample
<?php
$settings = array(
// one key is used for reading images (so that this can be public facing, but still prevent people from uploading)
'key_read' => '~',
// a separate secret key for storage. we don't want just any old monkey uploading images.
'key_store' => '123',
@patcullen
patcullen / js1k-boilerplate.html
Last active August 29, 2015 13:57
A boilerplate for starting a JS1k entry. Provides a compression function that allows hand-picking the tokens for replacement while also offering suggested tokens.
<!doctype html><html><head><title>JS1k Entry</title><meta charset="utf-8" /><style>html, body { margin: 0; padding: 0; border: 0; }#c { display: block; } /* kill scrollbars from hell */</style></head><body><canvas id="c"></canvas><script>
var a = document.getElementsByTagName('canvas')[0];
var b = document.body;
var d = function(e){ return function(){ e.parentNode.removeChild(e); }; }(a);
// unprefix some popular vendor prefixed things (but stick to their original name)
var AudioContext = window.AudioContext || window.webkitAudioContext;
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function(f){ setTimeout(f, 1000/30); };
// fix bug in safari: http://qfox.nl/weblog/218
document.body.clientWidth;
// auto resize (original) canvas. call `onresize(w,h) to limit the size of the canvas
@patcullen
patcullen / client_models.js
Last active August 29, 2015 13:57
A demo set of three models for Collect mobile framework
// for brevity, this sample has some properties missing :/
"client": {
"field": {
"firstname": { "type": "string", "req": true, "min": 2, "max": 50, "regex": "text", "description": "First Name" },
"lastname": { "type": "string", "req": true, "min": 2, "max": 50, "regex": "text", "description": "Last Name" },
"email": { "type": "string", "req": true, "regex": "email" },
"portfolio": { "type": "integer", "control": "input", "default": "", "req": false },
"clientproduct": { "type": "rel-many", "description": "Products this client uses", "control": "checklist" },
"clientstate": { "type": "integer", "req": false, "control": "select", "description": "State" }
},
console.log('Location:', '' + window.location);
console.log('Number of links:', document.querySelectorAll('a').length);
console.log('Number of _blank links:', document.querySelectorAll('a[target="_blank"]').length);
console.log('% of _blank links:', ~~( document.querySelectorAll('a[target="_blank"]').length / document.querySelectorAll('a').length * 100 ) );
// changed .log() to .dir() for twitter
/* Tested on 2014-04-14
Location: https://www.google.co.za/
Number of links: 45
@patcullen
patcullen / AnchorTest.js
Created April 14, 2014 18:38
A snippet of JS to test how many links follow to a new target.
console.log('Location:', '' + window.location);
console.log('Number of links:', document.querySelectorAll('a').length);
console.log('Number of _blank links:', document.querySelectorAll('a[target="_blank"]').length);
console.log('% of _blank links:', ~~( document.querySelectorAll('a[target="_blank"]').length / document.querySelectorAll('a').length * 100 ) );
@patcullen
patcullen / injectCSS_demo1.js
Created May 7, 2014 12:56
Inject CSS with Javascript
require(['injectCSS'], function(injectCSS) {
injectCSS(function(){/*
.case1 {
border: 3px solid #0f0;
}
*/});
injectCSS('.case2 { border: 3px solid #00f; } ');
});