Skip to content

Instantly share code, notes, and snippets.

View hexus's full-sized avatar
🤖

Chris Andrew hexus

🤖
View GitHub Profile
@hexus
hexus / test_interactive.php
Created December 21, 2018 11:28
An unfriendly PHP test script for interactive input and STDOUT/STDERR
<?php
fwrite(STDOUT, "What's your name?\n");
$name = ucfirst(trim(fread(STDIN, 2048))) ?: 'No-name';
fwrite(STDOUT, "Thanks, $name\n");
fwrite(STDERR, "Unfortunately, $name, your name is so abysmal, it's caused an error\n", 100);
fwrite(STDERR, "But it's okay, we still care about you\n");
fwrite(STDOUT, "How would you like to say good bye?\n");
fread(STDIN, 2048);
fwrite(STDERR, "Good bye, $name. Nobody loves you.\n", 100);
@hexus
hexus / phaser3-renderpass-issue.js
Last active December 29, 2017 03:53
Phaser 3 - Renderpass Y-axis flip and downsample/upsample issues example
var game = new Phaser.Game({
type: Phaser.WEBGL,
width: 1280,
height: 720,
backgroundColor: '#2d2d2d',
parent: 'phaser',
scene: {
preload: preload,
create: create,
update: update

Keybase proof

I hereby claim:

  • I am hexus on github.
  • I am hexusio (https://keybase.io/hexusio) on keybase.
  • I have a public key ASB-gUFx_IbUUk73bTRmcKkfaYTRqOkd8DaLJiUcQPVGzgo

To claim this, I am signing this object:

@hexus
hexus / asplode.php
Last active December 21, 2018 11:31
Explodes a value to an array to an expected length.
<?php
// It would be nice if PHP had something like this built in
function asplode($delimiter, $string, $limit, $pad = null) {
return array_pad(explode($delimiter, $string, $limit), $limit, $pad);
}
// Then things like this wouldn't cause an error without array_pad()
list($one, $two) = explode(' ', 'nospaces', 2);
// We're forced to do this, and it's just ugly and tedious
@hexus
hexus / throttle.js
Last active February 5, 2016 00:37
Helper function for throttling event handlers
var throttle = function(callback, timeout, context) {
timeout = timeout || 0;
context = context || this;
if (typeof callback === 'function') {
var last = 0;
return function() {
var time = new Date().getTime();
@hexus
hexus / array-casting.php
Last active October 15, 2015 09:38
How I write methods that add to a collection of values after realising that one can cast pretty much any primitive type to a single-element array...
<?php
class Setters {
protected $data = array();
public function oldFriendlyAdd($key, $value = null) {
if (is_array($value)) {
$values = $value;
@hexus
hexus / NormalisePath.php
Created September 15, 2014 08:45
A helper function for path normalisation that replaces all consecutive slashes with a single forward slash and trims leading and trailing slashes.
/**
* Helper function for consistent path values.
*
* @param string $path
* @return string
*/
function NormalisePath($path)
{
return preg_replace('#[\\\|/]+#', '/', trim($path, '\/'));
}
@hexus
hexus / webogramSpeechRecognition.js
Created July 2, 2014 18:46
Webogram Speech Recognition in Chrome
// Webogram speech recognition! - courtesy of Hexus
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.onresult = function(event){
var result = event.results[event.resultIndex][0];
var transcript = result.transcript;
var transtripped = transcript.replace(/[\s\r\n]+/,''); // Spaces with every transcript but the first
var inputElement = document.getElementsByClassName('im_send_form')[0].getElementsByClassName('form-control')[1];
console.log(result);
switch(transtripped){
@hexus
hexus / prettyDateDiff.php
Created May 3, 2014 17:04
Concise way of printing pretty dates relative to the current time (e.g. "30 seconds ago", "15 days ago", "1 month from now")
function dateDiff($date1,$date2=null){
if(!$date2){$date2 = 'now';}
$date1 = new DateTime($date1);
$date2 = new DateTime($date2);
return $date1->diff($date2);
}
function prettyDate($date){
$diff = static::dateDiff($date);
$intervals = array('s'=>'second','i'=>'minute','h'=>'hour','d'=>'day','m'=>'month','y'=>'year');
@hexus
hexus / ajaxify.js
Last active January 3, 2016 19:19
Playing with the HTML5 History API to load some CMS pages with XHR in place of a full page refresh. The only change needed server-side was the exclusion of repeated content, given the presence of the get variable 'ajax'.
$(function(){
var hasPopped = false;
// XHR request wrapper for page content
$.loadPage = function(href,data,callback,method){
$.ajax(href,{
dataType: 'html',
data: data,
type: method,
success: function(response){