Skip to content

Instantly share code, notes, and snippets.

View loilo's full-sized avatar

Florian Reuschel loilo

View GitHub Profile
@loilo
loilo / parse-filesize.php
Created February 13, 2021 10:01
PHP Filesize Parser
<?php
/**
* Parse a filesize to its number of bytes
*
* @param string|int|float $input
* @param int $base
* @return int
*
* @see https://github.com/patrickkettner/filesize-parser, the original JavaScript implementation of this function
@loilo
loilo / bitmask-to-array.mjs
Created November 15, 2020 14:09
Create Array From Bitmask
/**
* Convert a bitmask to an array of its individual bits
*
* @param {number} bitmask The bitmask to dissect
* @return {number[]}
*
* @example
* assert.equal(bitmaskToArray(0b101), [0b100, 0b001])
*/
export function bitmaskToArray(bitmask) {
@loilo
loilo / use-terminal-size.ts
Last active September 25, 2020 10:15
useTerminalSize() React Hook
import { useEffect, useState } from 'react'
/**
* React hook which provides the size of the terminal, based on https://usehooks.com/useWindowSize/
* Great for usage with Ink (https://npmjs.com/package/ink)
*/
export function useTerminalSize() {
const [terminalSize, setTerminalSize] = useState([
process.stdout.columns,
process.stdout.rows
@loilo
loilo / readme.md
Last active November 3, 2022 20:31
Async Computed Values for Vue 3

Async Computed Values for Vue 3

This gist provides a useAsyncComputed function which allows to create asynchronous dependencies on reactive values (using Vue 3's Composition API).

Requires at least Vue 3.0 and TypeScript 4.0.

Usage

Basic Usage

@loilo
loilo / readme.md
Last active April 9, 2024 20:01
Sass Dark/Light Theme Mixin

Sass Dark/Light Theme Mixin

This is a Sass mixin to handle a 3-way dark mode. It relies on a data-theme attribute on your <html> element with a value of light or dark. If data-theme is absent (i.e. it's neither light nor dark), the system's preferred mode is used.

body {
  // matches data-theme="light" or data-theme="auto" with system instructing light mode
  @include light {
    background: white;
 color: black;
@loilo
loilo / redirect-to-english.js
Last active October 12, 2023 21:23
Userscript: Redirect Website to its English Version
// This userscript redirects you to the English version of a website if it's denoted in the source code.
// Insert any URLs of websites below (after @match), for example https://developer.mozilla.org/* or https://www.php.net/*
// Use multiple @match clauses to enable the script on several domains.
// ==UserScript==
// @name Redirect to English
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Redirect websites to their English version
// @author Florian Reuschel <florian@loilo.de>
@loilo
loilo / manipulate_html.php
Last active July 19, 2023 02:50
Modify HTML Using PHP
<?php
function walk_dom(DOMNode $domNode, callable $callback): void
{
foreach ($domNode->childNodes as $node) {
$callback($node);
if ($node->hasChildNodes()) {
walk_dom($node, $callback);
}
@loilo
loilo / buffer.php
Created April 11, 2020 20:49
Output Buffering Directive for Laravel Blade Templates
<?php
// In your AppServiceProvider's boot() method, put this:
Blade::directive('buffer', function () {
return '<?php ob_start(); ?>';
});
Blade::directive('endbuffer', function (string $name) {
if ($name === '') {
return '<?php ob_end_clean(); ?>';
@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