Skip to content

Instantly share code, notes, and snippets.

@phcorp
phcorp / dragToScroll.js
Last active February 3, 2024 18:46
Emulate dragging on a desktop device
const IGNORED_SELECTOR = 'input, label, select, textarea, button, fieldset, legend, datalist, output, option, optgroup';
const SCROLLABLE_SELECTOR = '.drag-to-scroll';
/**
* Transform mouse drag gesture to scroll.
*
* @param {{
* [ignoredSelector]: string, // The ignored elements selector
* [selector]: string, // The scrollable elements selector
* }}
@phcorp
phcorp / retry.php
Created June 10, 2020 15:59
Simple php retry
<?php
declare(strict_types=1);
namespace Utils;
/**
* @param callable $function Function to call
* @param int $times Maximum number of times the function is called
*
@phcorp
phcorp / cartesian-product.php
Last active February 3, 2024 13:14
Cartesian product
<?php
function multiply(...$sets): array
{
if (!$sets) {
return [[]];
}
$set = array_shift($sets);
$products = multiply(...$sets);
$result = [];
@phcorp
phcorp / shell-terminal-split.sh
Last active September 16, 2019 14:24
terminal split function using tmux
##
# Splits a terminal vertically
#
# panes start synchronized, to desynchronize them, type:
# ctrl + b then :setw synchronize-panes
#
# usage: tsplit[ n]
# where n is an optional number
##
function tsplit() {
@phcorp
phcorp / DoctrinevizCommand.php
Created March 21, 2019 12:20
Symfony command that generates dot using doctrine graphviz visitor
<?php
declare(strict_types=1);
namespace App\Command;
use Doctrine\DBAL\Schema\Visitor\Graphviz;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
@phcorp
phcorp / merge-xml.php
Last active March 21, 2018 16:00
PHP merge xml files
#!/usr/bin/env php
<?php
function println($text, $foregroundColor = NULL, $backgroundColor = NULL) {
$colors = [
'background' => [
'black' => '40',
'blue' => '44',
'cyan' => '46',
'green' => '42',
@phcorp
phcorp / replace-in-path.php
Created March 16, 2018 15:48
PHP replace in path
<?php
$root = './src';
$find = 'foo';
$replace = 'bar';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root));
$count = 0;
foreach ($iterator as $file) {
if ($file->isDir() || 'php' !== $file->getExtension() || '.' === $file->getFilename()[0]) {
continue;
@phcorp
phcorp / apple-touch-startup-image-polyfill.es6.js
Last active December 8, 2017 22:59
Polyfill for apple-touch-startup-image
/* global window, Image */
/**
* Class AppleTouchStartupImage.
*
* Insert a script in the html head at the 1st position containing:
* const startupImage = new AppleTouchStartupImage();
* startupImage.install();
*
* @prop {string} image - source
@phcorp
phcorp / backup-clean.sh
Last active February 3, 2024 13:18
Backup files cleaning script
#!/bin/bash
##############################################################################
# This script is meant to be run once a day.
#
# Backup files name format must be: database-%Y-%m-%dT%H:%M.bz2
#
# It deletes backup files that does not match one of these conditions:
# - backup every 15 minutes during one week
# - backup every day during 3 months
@phcorp
phcorp / form-data-to-object.js
Created August 10, 2017 15:45
Vanilla Javascript FormData to object
/*
* Converts FormData to object.
*
* @param {FormData} data
* To convert.
* @return {string}
* FormData converted to object.
*/
function formDataToObject(data) {
let value;