Skip to content

Instantly share code, notes, and snippets.

View timwhitlock's full-sized avatar

Tim Whitlock timwhitlock

View GitHub Profile
@timwhitlock
timwhitlock / TestModel.php
Created January 19, 2014 22:39
Testing for Guzzle model structure.
<?php
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Mock\MockPlugin;
use Guzzle\Service\Client;
use Guzzle\Service\Description\ServiceDescription;
use Guzzle\Tests\GuzzleTestCase;
class ModelTest extends GuzzleTestCase {
@timwhitlock
timwhitlock / abc.mo
Last active June 17, 2018 22:46
Testing Gettext MO hash table compilation in PHP
@timwhitlock
timwhitlock / t.js
Last active December 17, 2015 10:59
Simple translate function requiring no external libraries. Includes plural form logic baked into closure. Feedback welcome
/**
* Example Loco JavaScript export.
* Usage: var singular = t('File');
* Usage: var multiple = t('File','Files',3);
* See: http://localize.biz/free/converter/api#to-js
* Locale: pl_PL, Polish
*/
var t = function( pairs ){
// named plural forms according to Unicode
@timwhitlock
timwhitlock / emojify.js
Last active December 16, 2015 08:09
Code for Emojify bookmarklet at http://apps.timwhitlock.info/emoji/bookmark
/**
* Emojify all text nodes in a document
* @author Tim Whitlock
* @license MIT
*
* This is available as a compressed bookmarklet at:
* - http://apps.timwhitlock.info/emoji/tools/bookmarklet
*
* See the following links for algorithms and encodings:
* - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/charCodeAt
@timwhitlock
timwhitlock / ie-versions.scss
Last active December 12, 2015 09:58
example of how to use Sass runtime to generate IE-targeted stylesheets
// File: utils.scss
// Define mixins and default IE version in your utils include
$ie-version: 10 !default;
@mixin ie-max ( $max-version ){
@if $ie-version <= $max-version {
@content;
}
}
@timwhitlock
timwhitlock / eachTouchEvent.js
Created November 14, 2012 16:24
Abstracting touch and pointer events with an iterator function
function onTouchStart( event ){
eachTouchEvent( event, function( i, ev ){
ev.clientX; // <- do something with this
} );
return true;
}
function eachTouchEvent( event, callback ){
@timwhitlock
timwhitlock / touchmove.js
Created November 14, 2012 11:31
Snippet showing cancelling of touchmove event on vertical swipe
// [...] already calculated xmove and ymove since last movement ... ]
if( ymove && Math.abs(ymove) > Math.abs(xmove) ){
// this was intended as a vertical scroll - probably of the page
// we will allow this event through so the OS/browser can handle it.
return true;
}
if( xmove ){
// else take control of horizonal scroll position and cancel event
element.scrollLeft = Math.max( 0, xstart + xmove );
}
/**
* swipe inertia demo for browsers supporting the touchstart family of events
* @param HTMLElement
* @param Function touchend callback with speed and direction of swipe
*/
function initSwiper( el, callback ){
var xstart = 0, // <- initial scroll position on first touch
tstarts = [], // <- time each touch started
xoffset = [], // <- initial x position of each touch
yoffset = []; // <- initial y position of each touch
@timwhitlock
timwhitlock / _tw_intercept_comment.php
Created June 19, 2012 12:45
Boot any comment bot that fills in the author website field
<?php
/**
* Boot any comment bot that fills in the author website field.
*/
function _tw_intercept_comment( array $data ){
if( empty($data['comment_author_url']) ){
return $data;
}
get_header();
echo 'See ya';
@timwhitlock
timwhitlock / dict.js
Last active January 6, 2021 19:01
Searchable JavaScript Dictionary
/**
* Searchable JavaScript dictionary.
*
* Usage:
* var dict = require('path/to/this').create();
* dict.add( 'somedata', 'Text to index' );
* var items = dict.find('text');
*/
exports.create = function(){
return new Dict;