Skip to content

Instantly share code, notes, and snippets.

View loilo's full-sized avatar

Florian Reuschel loilo

View GitHub Profile
@loilo
loilo / reset.js
Last active November 3, 2017 14:15
Reset a single form element
// 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 12:42
Synchronize shareable key-value state to the window.location.hash as human-readable as possible
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 June 9, 2018 01:57
Parses column-formatted UNIX output

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 August 3, 2018 20:29
Activate Object Spread in Templates of a Vue CLI v3 Project
// 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 / DNode.php
Last active August 31, 2018 06:54
DNode Comfort Interface (Node.js Server, PHP Client)
<?php
require_once __DIR__ . "/vendor/erasys/dnode-php-sync-client/DnodeSyncClient.php";
class DNode
{
protected $connection;
public static function connect($host, $port)
{
@loilo
loilo / xtract.md
Created October 24, 2018 14:17
Structural Extraction of PHP Arrays

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 December 5, 2018 08:29
(An Approach to) Vue.js Template Variables

(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 -->
@loilo
loilo / async-await-vorteil.md
Last active January 9, 2019 13:35
Wozu Entwickler async/await brauchen

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)
@loilo
loilo / serialize-multipart-form.js
Last active January 9, 2019 13:40
Serializes a form's elements into a ready-to-send POST body string
const serializeMultipart = async form => {
const data = {}
const formdata = Array.from(form.querySelectorAll('input, select, textarea, button')).map(input => {
if (input.disabled) return false
switch (input.nodeName) {
case 'INPUT':
switch (input.type.toLowerCase()) {
case 'checkbox':
@loilo
loilo / break.js
Last active January 9, 2019 13:41
Breaks elements out of their container
// This was originally designed for creating virtual print sheet layouts in HTML/CSS,
// breaking contents too long for one page to the next one.
// You can play around with it here: https://jsfiddle.net/Loilo/L3w8zjuf/3/
function internalBreak (breakItem, outmostContainer, breakDirection, childIsBroken = false) {
const container = breakItem.parentElement
const containerItems = Array.from(container.children)
// Move all items after `breakItem` to a new container
const nextContainer = container.cloneNode()