Skip to content

Instantly share code, notes, and snippets.

View juice49's full-sized avatar

Ash juice49

View GitHub Profile
@juice49
juice49 / Flash.php
Created September 16, 2015 10:39
ZF2 Flash Data Plugin
<?php
namespace App\Controller\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Session\Container;
use Zend\Session\ManagerInterface as Manager;
@juice49
juice49 / asBlog.php
Last active February 1, 2016 15:01
WordPress "as blog" function
<?php
function asBlog($blogId, $function) {
return function(...$arguments) use($blogId, $function) {
switch_to_blog($blogId);
$output = $function(...$arguments);
restore_current_blog();
return $output;
};
}
@juice49
juice49 / npm.md
Last active February 17, 2016 13:26
Useful npm scripts

HashMark is a small utility which takes a file (or sdtin) as input, and writes the contents of the input to a file named with a hash digest of the file.

#npm-run-all A CLI tool to run multiple npm-scripts in parallel or sequential.

@juice49
juice49 / index.js
Created February 18, 2016 21:29
Websocket router
function routeWebsocket(server, controllers = []) {
server.on('connection', client => {
client.on('message', ({ event, data }) => {
const controller = controllers[event];
if(controller) {
controller(data);
}
});
});
}
@juice49
juice49 / append-around.js
Last active March 4, 2016 10:37
AppendAround with onRemove and onAppend callbacks
/*
* !!! IMPORTANT !!!
* This has been modified to accept "onRemove" and "onAppend" callbacks.
* See comments 1, 2, and 3.
*/
/*! appendAround markup pattern. [c]2012, @scottjehl, Filament Group, Inc. MIT/GPL
how-to:
1. Insert potential element containers throughout the DOM
2. give each container a data-set attribute with a value that matches all other containers' values
3. Place your appendAround content in one of the potential containers
@juice49
juice49 / example.js
Last active October 6, 2016 15:19
Delay a JS promise
const delay = delayPromise(1000)
delay(() => fetch('/'))
.then(console.log)
@juice49
juice49 / index.js
Created October 6, 2016 15:20
Timeout a JS promise
'use strict';
const timeoutPromise = ms => promise => new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new PromiseTimeout());
}, ms);
promise.then(
res => {
clearTimeout(timeout);
resolve(res);
@juice49
juice49 / b.php
Created September 8, 2017 09:41
PHP benchmark
<?php
function b () {
$start = microtime(true);
return function () use ($start) {
$end = microtime(true);
return $end - $start;
};
}
@juice49
juice49 / l.php
Created November 6, 2017 15:39
PHP log
<?php
function l ($name) {
return function ($line) use ($name) {
file_put_contents($name, $line . PHP_EOL, FILE_APPEND);
};
}
@juice49
juice49 / index.js
Created January 22, 2018 10:37
Console log prefix
const log = (...message) =>
console.log.apply(console, [ '[PREFIX]', ...message ])
log('foo', 'bar', 'bat', 1, 2, 3)
// [PREFIX] foo bar bat 1 2 3