Skip to content

Instantly share code, notes, and snippets.

@marcojetson
marcojetson / progressBar.php
Last active May 16, 2018 17:46
Simple and customisable command line progress bar in PHP
<?php
/**
* Creates a progress bar
*
* @param string $template Template to use, available placeholders are %finished, %unfinished, %percent and %eta
* @param int $width Width of the progress bar
* @return callable A function that renders the bar and takes the percent as only argument
*/
function progressBar(
@marcojetson
marcojetson / jquery.removeClass.js
Last active August 29, 2015 14:28
Add regex support to jQuery's removeClass
(function ($) {
'use strict';
$.fn.originalRemoveClass = $.fn.removeClass;
$.fn.removeClass = function (arg) {
if (arg instanceof RegExp) {
return this.originalRemoveClass(function (i, css) {
var remove = [];
$.each(css.split(/\s+/), function (i, cls) {
<?php
class PoolManagerException extends Exception {}
class PoolManagerInvalidWeightException extends PoolManagerException {}
class PoolManagerNodeNotFoundException extends PoolManagerException {}
class PoolManagerEmptyException extends PoolManagerException {}
/**
* Generic pool manager with horizontal scaling in mind
*
@marcojetson
marcojetson / emojis.html
Last active November 24, 2015 17:55
CSS emojis w/o images
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
.emoji {
display: inline-block;
font-style: normal;
position: relative;
top: 1px;
@marcojetson
marcojetson / packpub.sh
Created December 8, 2015 17:22
PacktPub free book of the day
curl https://www.packtpub.com/packt/offers/free-learning | grep -A7 dotd-title | tail -1 | xargs -0 osascript sendmail.applescript [email protected] "Today's free eBook"
<?php
trait FlagTrait
{
/** @var int */
private $flags = 0;
/**
* @param int $flag
* @return bool
@marcojetson
marcojetson / example.php
Created July 5, 2016 15:57
Extract and reduce measurement units from strings
<?php
require __DIR__ . '/strtounits.php';
assert(strtometers('25000.23 meters') === 25000.23);
assert(strtometers('3km, 25.5 meters') === 3025.5);
@marcojetson
marcojetson / shortcuts.js
Created October 8, 2016 09:51
Key shortcut configuration macOS alike
$('input').keydown(function (e) {
e.preventDefault();
var keys = [];
e.altKey && keys.push('⌥');
e.ctrlKey && keys.push('⌃');
e.metaKey && keys.push('⌘');
e.shiftKey && keys.push('⇧');
@marcojetson
marcojetson / concept.php
Last active October 19, 2016 13:17
Array support for Phalcon forms
<?php
class Form extends \Phalcon\Forms\Form
{
/**
* @inheritdoc
*/
public function bind(array $data, $entity, $whitelist = null)
{
foreach ($this->getElements() as $element) {
/**
* Executes a function with a given interval
* @arg {function} fn - Function to execute,
* @arg {integer} interval - Interval in milliseconds
*/
function poll(fn, interval) {
fn(() => setTimeout(() => poll(fn, interval), interval));
}
poll(retry => $.get('/status').success(res => res.status === 'pending' && retry()));