Skip to content

Instantly share code, notes, and snippets.

View al-the-x's full-sized avatar

David Rogers AKA "AL the X" al-the-x

View GitHub Profile
@al-the-x
al-the-x / example.txt
Last active August 29, 2015 13:55
AWK script to find number of occurrences of "foo=" in a very large text file (or stream); supplied is a small example of the format one might expect.
{
foo=1
bar=2
baz=3
}
{
foo=0
}
{
}
@al-the-x
al-the-x / router.php
Last active August 29, 2015 14:01
Simple router file for serving legacy PHP projects like Wordpress with `php -S host:port router.php`
<?php
$path = rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
// If that doesn't correspond to an existing path, assume a route...
$path = realpath(__DIR__ . $path) ?: __DIR__ . '/index.php';
extract(pathinfo_safe($path)); // $dirname, $basename, $extension, and $filename
if ( !$extension ) extract(pathinfo_safe($path .= '/index.php'));
@al-the-x
al-the-x / Model.php
Created July 27, 2014 20:00
Another thought about PHP ORMs that occurred to me today, should I ever fall victim to that particular land war in Asia... What if a Model class exposed a static method for configuration of child classes, which accepted a Closure that was invoked upon instantiation, like `Model#initialize` in Ruby?
<?php
class Model
{
static $hooks = [ ];
/**
* Add initialization hook $init to (FIFO) queue, which will be bound to the
* instance of the object at initialization.
*
@al-the-x
al-the-x / FunctionalTestCase.php
Last active August 29, 2015 14:04
I get tired of writing this simple `FunctionalTestCase` over and over again. I should submit to @sbergmann for PHPUnit...
<?php
/**
* A "functional" test is typically one that runs the full application through it's
* paces: constructing a request, triggering a route, generating a response, and
* testing the fully rendered output. This abstract class provides some assertions
* appropriate for testing requests and responses and abstract methods for actually
* fetching requests and responses and routing the application. The developer should
* extend this class for his or her specific framework or application.
*/
@al-the-x
al-the-x / controller.js
Created September 3, 2014 22:19
Using Lodash inside of Angular
angular.module('myModule', [ ], function($rootScope){ // equivalent to .config(function($rootScope){ . . . })
$rootScope._ = _; // Make `_` available in Angular Expressions
}).controller('MyController', function(Something){
var saveSomething = this.saveSomething = Something.save();
$scope.$watch('something', _.debounce(saveSomething, 500));
$scope.something = 'some value';
})
; // END myModule
@al-the-x
al-the-x / check-writing.js
Created October 2, 2014 15:33
Practiced Kata: Check Writing from @TheIronYard--Orlando
var assert = require('assert');
function test(actual, expected, success){
success = success || 'pass!';
assert.equal(actual, expected);
console.log(success);
}
@al-the-x
al-the-x / conway-simple.js
Created October 2, 2014 15:40
Conway's Game of Life from Week 2 at @TheIronYard--Orlando
/**
* Conway's Game of Life: http://en.wikipedia.org/wiki/Conway's_Game_of_Life
*
*/
@al-the-x
al-the-x / check-writing.js
Created October 7, 2014 14:34
Check Writing + forEach re @TheIronYard--Orlando Coding Dojos
var assert = require('assert');
function test(actual, expected, success){
success = success || 'pass!';
assert(actual === expected) || console.log(success);
}
/**
* Check Writing
@al-the-x
al-the-x / deepEqual.js
Created October 8, 2014 14:08
Write your own `assert.deepEqual()`...
var assert = require('assert');
/**
* @param Array actual
* @param Array expected
* @param String message optional
* @return Boolean
*/
assert.deepEqual = function(actual, expected, message){
/* Then a miracle occurs... */
@al-the-x
al-the-x / dojo.js
Created October 11, 2014 12:15
Code from running a Dojo at @theironyard in Charleston, SC as a guest lecture for @calweb
/** --- TEST CODE --- **/
var expect = chai.expect;
/**
* Next multiple of 3 or 5
* n = 0: 3
* n = 3: 5
* n = 5: 6
* n = 6: 9
* n = 9: 10