Skip to content

Instantly share code, notes, and snippets.

Avatar

Florian Reuschel loilo

View GitHub Profile
@loilo
loilo / reset.js
Last active Nov 3, 2017
Reset a single form element
View reset.js
// Resets a given form element
function resetInput (input) {
// 1. Remember the <input> position by grabbing sibling and parent
const next = input.nextSibling
const parent = input.parentNode
// 2. Create a temporary <form> element, put the <input> there and reset that form
const tmpForm = document.createElement('form')
tmpForm.appendChild(input)
@loilo
loilo / HashStateManager.js
Last active May 11, 2018
Synchronize shareable key-value state to the window.location.hash as human-readable as possible
View HashStateManager.js
class HashStateManager {
constructor ({ global = window, prefix = '!' } = {}) {
this.window = global
this.prefix = prefix
this.callbacks = new Set
this.urlListenerEnabled = true
this.boundUrlListener = this.urlListener.bind(this)
this.internalState = this.parseState()
this.publicState = this.createState()
@loilo
loilo / ColumnParser.md
Last active Jun 9, 2018
Parses column-formatted UNIX output
View ColumnParser.md

PHP Column Parser

This PHP class takes a block of column-styled UNIX output (e. g. from composer show) and dissects it into lines and columns.

Take this excerpt from running the mentioned command in a Symfony project:

doctrine/cache               v1.7.1 Caching library offering an object-oriented API for many cache backends
doctrine/inflector           v1.2.0 Common String Manipulations with regard to casing and singular/plural rules.
illuminate/contracts         v5.5.2 The Illuminate Contracts package.
illuminate/support           v5.5.2 The Illuminate Support package.
@loilo
loilo / vue.config.js
Created Aug 3, 2018
Activate Object Spread in Templates of a Vue CLI v3 Project
View vue.config.js
// Store this as vue.config.js in your project root
// to enable object spread in templates.
// Note that you'll need to provide an Object.assign()
// polyfill for legacy browsers.
module.exports = {
chainWebpack: config => {
config.module
.rule('vue')
.use('vue-loader')
@loilo
loilo / high-sierra-iso.md
Last active Aug 30, 2018
Create an installation ISO file for macOS High Sierra
View high-sierra-iso.md
  1. Prerequisites:
    • You need a running macOS system.
    • I assume you're familiar with using the terminal.
    • All the examples below refer to installing macOS High Sierra, you may need to adjust them to your needs if you want to install another macOS version.
  2. Download macOS High Sierra from the AppStore.
  3. The macOS installer will start after the download finiashes. We don't need it, so we can close it.
  4. We need an empty image where we'll put the installer. Let's create one using the macOS DiskUtil CLI:
    hdiutil create -o /tmp/HighSierra -size 8G -layout SPUD -fs HFS+J -type SPARSE
@loilo
loilo / DNode.php
Last active Aug 31, 2018
DNode Comfort Interface (Node.js Server, PHP Client)
View DNode.php
<?php
require_once __DIR__ . "/vendor/erasys/dnode-php-sync-client/DnodeSyncClient.php";
class DNode
{
protected $connection;
public static function connect($host, $port)
{
@loilo
loilo / Debouncer.md
Last active Oct 3, 2018
ReactPHP debounce
View Debouncer.md

ReactPHP Debouncer

A simple & classic debounce function. You provide a callable and specify a $wait timer and get a callable (closure) back.

The closure takes the same arguments as the provided callable, but will only be executed $wait seconds after being invoked. Invoking the closure again before $wait seconds have passed will refresh the execution delay to the full $wait seconds again.

This can be useful for example for detecting if a socket hasn't received data in, let's say, 10 seconds:

$loop = React\EventLoop\Factory::create();
@loilo
loilo / xtract.md
Created Oct 24, 2018
Structural Extraction of PHP Arrays
View xtract.md

Structural Extraction of PHP Arrays

Extract structures from PHP arrays, kind of like an advanced pluck.

Signature

The provided xtract function takes a $source (usually an array) and a $target, which is the structure to transform the $source into:

mixed xtract( mixed $source, mixed $target )

Simple Examples

@loilo
loilo / readme.md
Last active Dec 5, 2018
(An Approach to) Vue.js Template Variables
View readme.md

(An Approach to) Vue.js Template Variables

This has first been published as an article on dev.to.

The Problem

From time to time, I have the need to temporarily store the results of a method call in Vue templates. This is particularly common inside loops, where we cannot easily use computed properties.

Basically what we'd want to avoid is this:

<!-- List.vue -->
<ul>
@loilo
loilo / async-await-vorteil.md
Last active Jan 9, 2019
Wozu Entwickler async/await brauchen
View async-await-vorteil.md

Besseres Scoping mit async/await

Problem

Wie greife ich auf frühere Ergebnisse in der Promise-Chain zurück?

ajaxCall()
    .then(response => {
      //  ^ man beachte diese Variable
      return expensiveEvaluation(response.data)
You can’t perform that action at this time.