Skip to content

Instantly share code, notes, and snippets.

View loilo's full-sized avatar

Florian Reuschel loilo

View GitHub Profile
@loilo
loilo / args.php
Last active January 9, 2019 13:44
Minimist-like PHP $argv parser
<?php
$args = array_slice($argv, 1);
$unnamedArgs = [];
$namedArgs = [];
$currentArg = null;
foreach ($args as $arg) {
// Long name
@loilo
loilo / readme.md
Created January 9, 2019 11:41
Access PHP Variables in Nested Closures

Access PHP Variables in Nested Closures

This post was inspired by having to pass around ReactPHP's event loop all day.

😑 Are you tired of managing loads of use (...) constructs because you need some local variable inside a deeply nested closure?

🤯 Use the following tiny function abusing Closure::bind() to bend variable scope to your will!

function run_scoped(Closure $callback, $init = [])
@loilo
loilo / deepAsync.js
Last active May 29, 2019 12:17
Deeply resolves all promises in a data structure
// Arbitrarily nested object containing Promises goes in
// Plain data structure comes out
async function deepAsync (object) {
// Walk arrays
if (Array.isArray(object)) {
return Promise.all(object.map(async item => await deepAsync(item)))
// Walk objects
} else if (typeof object === 'object' && String(object) === '[object Object]') {
return Object
@loilo
loilo / argstr.md
Last active September 27, 2019 21:11
Build a command line arguments string from a PHP associative array

PHP arg string generator

Generate a command line arg string from an associative array.

This is purely built for functionality, not for beauty: no grouping of shorthand flags, all args will be escaped and wrapped in single quotes, no matter if needed or not.

Usage

Associative key-value pairs will be treated as command line options while array entries with no/integer keys will be used as regular arguments.

@loilo
loilo / rangeslider.md
Last active October 14, 2019 11:50
Rangeslider

Rangeslider

This is a small, production-ready Rangeslider library.

Try it out in this CodePen!

I created this because no existing library met all my criteria:

  • no heavy-weight third-party dependencies (i.e. no jQuery)
  • respect HTML5 `` restrictions (min, `max`, `step`)
@loilo
loilo / regex-tag.md
Last active October 23, 2019 12:32
Regular Expression Template Tag in TypeScript

Regular Expression Template Tag in TypeScript

I liked this gist enough that I made it a full-blown npm package. Check it out here.

After having used regular expression escape functions for years, it just crossed my mind that this might be way easier to achieve with a tagged template literal.

This is how to use this gist:

import rx from 'https://unpkg.com/@loilo/rx/dist/rx.mjs'
@loilo
loilo / fixate-date.js
Created November 8, 2019 10:17
Jest: Mock datetime
/**
* Fixates JavaScript's Date
* This is useful for snapshot testing data which refers to the current time
*
* Losely based on https://github.com/facebook/jest/issues/2234#issuecomment-324868057
*/
/**
* Keep a reference to the original date
*/
@loilo
loilo / moxy.js
Last active January 21, 2020 10:06
Moxy – a function that mocks an object's properties and methods with a proxy
/**
* Mock an object with a proxy, overriding methods and properties
*
* @param {object} object An object to mock
* @param {object} implementations The properties/methods to virtually "merge" into the mocked object
*/
function moxy(object, implementations) {
return new Proxy(object, {
get(target, key, receiver) {
if (key in implementations) {
@loilo
loilo / dom-position.js
Last active February 2, 2020 09:21
Compares the position of two elements in a DOM tree and returns -1, 0 or 1
// Note: This code is obsolete. There's a cross-browser DOM method `compareDocumentPosition()` which does the same, but more detailed.
// See https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition
function compareNodePosition( nodeA, nodeB, container ) {
if ( !( container instanceof HTMLElement ) ) {
container = document.body;
}
function getNodePositionArray( el, container ) {
var arr = [];
@loilo
loilo / events.mjs
Created February 20, 2020 12:23
Vue Page-Wide Events
export default function on(target, event, listener) {
let targetObject, boundListener
return {
mounted() {
// Allow functions for client-only targets
targetObject = typeof target === 'function' ? target() : target
if (typeof listener === 'string') {
// If a string is passed as a listener, use a method with that name