Skip to content

Instantly share code, notes, and snippets.

View tobiashm's full-sized avatar

Tobias H. Michaelsen tobiashm

View GitHub Profile
@tobiashm
tobiashm / README.md
Last active November 17, 2023 13:39
TypeScript transpiler for Node.js

Minimal TypeScript loader for Node.js

In newer versions of Node.js you can register your own loader and resolvers. This is an example of automatically transpiling .ts files.

Usage:

> node --import ./ts-loader.mjs ./my-app.ts
@tobiashm
tobiashm / cvr.ts
Last active October 25, 2023 21:41
TypeScript check for CVR-nr
type LengthOfString<T extends string, S extends string[] = []> = T extends `${string}${infer R}` ? LengthOfString<R, [...S, string]> : S['length'];
type HasLengthOfEight<T extends string> = LengthOfString<T> extends 8 ? T : never;
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type IsNumeric<T extends string, S = T> = T extends Digit ? S : T extends `${Digit}${infer R}` ? IsNumeric<R, S> : never;
type StartsWithNonZeroDigit<T extends string> = T extends `${Exclude<Digit, '0'>}${string}` ? T : never;
type CVRNummer<T extends string> = IsNumeric<T> & HasLengthOfEight<T> & StartsWithNonZeroDigit<T>;
@tobiashm
tobiashm / decode-jwt.js
Created June 14, 2023 08:34
Simple JWT deocder
/**
* @typedef {object} Header - JOSE Header https://datatracker.ietf.org/doc/html/rfc7519#section-5
* @property {string} typ - Type https://datatracker.ietf.org/doc/html/rfc7519#section-5.1
* @property {string} alg - Algorithm https://datatracker.ietf.org/doc/html/rfc7515#section-4.1.1
*/
/**
* @param {string} char - a string containing a single character
*/
const toURIComponent = (char) => '%' + ('00' + char.charCodeAt(0).toString(16)).slice(-2);
@tobiashm
tobiashm / .zshrc
Created July 21, 2022 20:13
Simple Node.js version manager (based on Homebrew)
# Simple Node.js version manager
#
# Usage example:
# $ use-node 16
#
# The strategy is to identify the Homebrew version, e.g. 'node@16', and the prepend the path to its binaries to $PATH.
# Caveat: This only supports selecting a major version, as that's the level of granularity that Homebrew provides.
use-node() {
if [[ "$1" == "$(node --version | sed 's/v\([0-9]*\).*/\1/')" ]]; then
return 0;
@tobiashm
tobiashm / cpr-to-year.groovy
Created January 6, 2021 11:49
Determine birth year from CPR-nummer (Danish personal identifier)
def cpr = '0101374000'
def match = (cpr =~ /^\d{4}(\d{\d}2})(\d)\d{3}$/)[0]
def year = match[1] as Integer
def x = match[2] as Integer
switch (x) {
case 0..3: year += 1900; break;
case [4, 9]: year += year <= 36 ? 2000 : 1900; break;
@tobiashm
tobiashm / README.md
Created March 23, 2020 13:51
Custom Laravel Translator that handles locale with region

In config/app.php in the 'providers' section, replace Illuminate\Translation\TranslationServiceProvider::class with App\Translation\TranslationServiceProvider::class

@tobiashm
tobiashm / userscript.js
Created September 23, 2019 12:07
Safari userscript
window.addEventListener('load', function() {
if (!document.location.href.startsWith('https://app.clubhouse.io/')) { return; }
function isDropdownNode(node) {
return node.nodeType === Node.ELEMENT_NODE && node.classList.contains('dropdown');
}
document.addEventListener('DOMNodeInserted', function(event) {
var insertedNode = event.target;
@tobiashm
tobiashm / AuthServiceProvider.php
Created June 13, 2019 12:21
Laravel composite guard
<?php
namespace App\Providers;
use App\Auth\CompositeGuard;
use Illuminate\Auth\TokenGuard;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use MiladRahimi\LaraJwt\Guards\Jwt as JwtGuard;
@tobiashm
tobiashm / Kernel.php
Last active January 8, 2019 13:01
Load commands from all PSR-4 Autoload namespaces
<?php
namespace App\Console;
use Illuminate\Console\Application as Artisan;
use Illuminate\Console\Command;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use ReflectionClass;
use Symfony\Component\Finder\Finder;
@tobiashm
tobiashm / use-node.sh
Last active November 2, 2018 13:39
Switch Node.js version with Homebrew
# Assumes you have installed individual major versions with Homebrew, like node@8,
# this command allows you to switch by calling e.g. `use-node 8`, or to get the
# current latest version (`brew install node`) if you don't pass an argument.
# Notice: Since it modifies the env, this will only work for the current session.
# I.e. other terminal windows will have the default Node version, unless they’re also changed.
use-node() {
wanted=$([ "$1" == "" ] && echo "node" || echo "node@$1")
prefix=$(brew --prefix $wanted 2> /dev/null)
if [ "$prefix" != "" ]; then
PATH="$(echo $PATH | tr ':' '\n' | grep -v ${prefix%@*} | tr '\n' ':')"