Skip to content

Instantly share code, notes, and snippets.

@xphere
xphere / debounce.js
Created February 17, 2014 15:59
debounce.js
function debounce(callback, timeout) {
var timer;
return function() {
timer && clearTimeout(timer);
timer = setTimeout(function(args) {
timer = null;
callback.apply(this, args);
}.bind(this, arguments), timeout||100);
}
}
function setSelectionPlugin() {
var _setSelectionRange, _getCursorPosition, support = (function() {
var input = document.createElement('input'), result;
result = {
setSelectionRange: ('setSelectionRange' in input) || ('selectionStart' in input),
createTextRange: ('createTextRange' in input) || ('selection' in document)
};
input = null;
return result;
}());
@xphere
xphere / Stream.js
Last active August 29, 2015 13:56
Streams javascript naïve implementation
var Stream = (function () {
var undefined, empty, proto;
function extend(dest, source) {
for (var arg = 1; arg < arguments.length; ++arg) {
for (var key in arguments[arg]) {
if (arguments[arg].hasOwnProperty(key)) {
dest[key] = arguments[arg][key];
}
}
@xphere
xphere / functions.sh
Last active August 29, 2015 13:56
Bash function for Symfony console
sf() {
local DIR
pushd . > /dev/null 2>&1
while ! git exec test -x app/console; do
if [ $PWD = "/" ]; then
popd > /dev/null 2>&1
echo "Can't find a suitable Symfony application" 1>&2
return 1
fi
@xphere
xphere / chooseRandomTeamMate.php
Created July 16, 2014 12:03
Choose randomly a team mate from GitHub
<?php
class Github
{
private $token;
public function __construct($token)
{
$this->token = $token;
}
@xphere
xphere / FirewallListenerCompilerPass.php
Created February 2, 2015 14:56
Add listeners that launch on particular firewalls
<?php
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
/**
* Class FirewallListenerCompilerPass
*
* @author Berny Cantos <be@rny.cc>
@xphere
xphere / MergeConcatTwigExtension.php
Created February 26, 2015 10:01
Twig filter for `merge_concat` between arrays, making easier to add classes and csv lists as attributes.
<?php
/**
* Copyright © 2015 Berny Cantos <be@rny.cc>
*
* This work is free. You can redistribute it and/or modify it under the
* terms of the Do What The Fuck You Want To Public License Version 2 as
* published by Sam Hocevar.
*
* See http://www.wtfpl.net/about file for more details.
@xphere
xphere / split-and-merge.md
Created April 13, 2015 15:34
Instructions for splitting a directory in a repo and merge into another one
  1. Split directory in a branch in source repository

    git subtree split -P <name-of-folder> -b <name-of-new-branch>
    
  2. Add remote of source in destination repository and fetch

    git remote add -f <source> <source-repository>
    
<?php
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
// Interface implementation for testing purposes
class MyParameterBag implements ParameterBagInterface
{
private $container = array();
public function add(array $parameters)
{
@xphere
xphere / gist:907539
Created April 7, 2011 10:48
Validation of Value Objects
<?php
// With static validation, no object is created if not valid.
if (Locale::isValid($localeName)) {
$locale = new Locale($localeName);
doFancyStuffWith($locale);
}
// With explicit validation
$locale = new Locale($localeName);