Skip to content

Instantly share code, notes, and snippets.

View bennettdams's full-sized avatar

Bennett Dams bennettdams

  • Germany
View GitHub Profile
@bennettdams
bennettdams / zod-empty-string-undefined-optional.ts
Last active April 3, 2024 16:20
Zod empty string transformed to `undefined` including handling optional
import { z } from 'zod'
const emptyStringToUndefined = z.literal('').transform(() => undefined)
/**
* Provide a schema and get a schema that is optional and empty strings are transformed to `undefined`.
* When someone removes his input of this field, the then empty string is transformed to `undefined`.
*
* Example:
*
@bennettdams
bennettdams / typesafe-array-null-filter.ts
Last active April 29, 2021 17:01
TypeScript: typesafe array filtering for null
"https://www.typescriptlang.org/play?target=7#code/JYOwLgpgTgZghgYwgAgMJysg3gWAFDLLAAmAXMiAK4C2ARtANz6HUD2xEANuQM5hSgA5sgA+FSp074AvvnwJWIPsgQYAjOXSYxVScgC82ImWRqANMjYduyAEQBBW8ll4FSsCowAmTRlHi9Q10pV0VlVSgAZl9tAM4DIxJySIsrLnJbVCcXeTCPCJ5yAAotf2CASgBtAF0EyojzTygvCwjI6rk8Tgh8jB4AdWAwAAtWSjAAOQkbLRq6jrx8AHol5AB3IeHkADoYYE5ITCLoKFYoDIAVAE8ABxQAcmD7oh4KVg84Hh5gQRA4Wm6yDArCBtweWnu21s5XkfUGIzGk2mCQKu32hyKEQMAD4msgAIT6ILTGGLPArdabHZ7A7QZAAamQHD2ICEoLuyCKw0QAGsrhYeHBqCgbqcARBqMhOMAeShKN8QMJbJ9obCoANNoipoEmjw0bSoEVMRhyuQscBXqV9LisYTiZJykwyRSNiMdlAIMRKEg1RqEeNtfFDKiPV6kAAeWbVbFGxAIVomnGc5gErEAfmQceQ5Eq2zzcYTUAW5QsNVJy1Wrq2u04cDAAFk4DdffDRgHkcG+jW642bsbMNbU34M3McxFqo78EA"
interface Car {
id: number;
model: string | null
}
const car1: Car | null = { id: 1, model: "A" }
const car2: Car | null = null
const car3: Car | null = { id: 3, model: "C" }
@bennettdams
bennettdams / index.html
Created June 23, 2020 11:13
Rotate a text 180° with TailwindCSS
<div className="flex w-1/6>
<p className="transform rotate-180 text-center" style={{ writingMode: 'vertical-rl' }}>
Text
</p>
</div>
<div x-data="{ open: false }">
<!-- This is the top navbar, and it is shown on both desktop and mobile -->
<div class="bg-gray-800 py-2 px-4 flex items-center justify-between">
<div class="text-2xl font-bold text-white">
AcmeCorp
</div>
<div class="block sm:hidden">
<button @click="open = !open" class="text-white font-semibold hover:underline focus:outline-none focus:underline">Menu</button>
</div>
@bennettdams
bennettdams / useSharedLoading.ts
Last active September 25, 2019 15:53
Custom hook to access a shared loading state in a component. The hook provides a wrapper for multiple composite function executions.
import * as React from "react";
import { useEffect, useState, useCallback } from "react";
import { render } from "react-dom";
import "./styles.css";
type UseSharedLoadingHook = () => {
isLoading: boolean;
compositeLoading: (callback: () => void) => Promise<void>;
increaseAmountRunningTasks: () => void;
/**
* Helper method to create an OAuth2 access token via MockMvc.
* Result is a string that can be used in another mvc request.
* Example:
* <p>
* MvcResult result = mvc.perform(get("/user/" + userToSearch.getUsername() + "/role")
* .header("Authorization", "Bearer " + accessToken))
* .andExpect(status().isOk())
* .andReturn();
*
@bennettdams
bennettdams / Permutations.java
Created April 30, 2019 10:57
Generate non-repetetive permutations of given numbers and utilize each combination. Example result for 4: 4! = 24 combinations
import java.util.*;
class Permutations {
public static void main(final String[] args) {
// Permutations of the numbers 0 to 3 to get all possible combinations.
// For 4 numbers the result should be 4! = 24 different combinations
// like 1-2-3-4, 1-2-4-3, 1-3-2-4, 1-3-4-2, etc.
Collection<List<Integer>> permutations = new Permutations()
.generatePermutationsWithoutRepetition(new HashSet<>(Arrays.asList(0, 1, 2, 3)));
@bennettdams
bennettdams / launch.json
Created October 9, 2018 14:54
VS Code Vue debugging launch config
{
// Use IntelliSense to learn about possible attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${file}"
@bennettdams
bennettdams / keybindings.json
Created October 5, 2018 11:59
VS Code keybindings
// Place your key bindings in this file to overwrite the defaults
[
{
"key": "ctrl+shift+f",
"command": "editor.action.formatDocument",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "shift+alt+f",
"command": "-editor.action.formatDocument",
@bennettdams
bennettdams / settings01.json
Last active February 15, 2019 11:22
Microsoft Visual Studio Code - Settings vor Vue
{
// general
"telemetry.enableTelemetry": false,
"workbench.settings.editor": "json",
"workbench.startupEditor": "newUntitledFile",
"workbench.editor.restoreViewState": false,
"workbench.colorTheme": "One Dark Theme",
"workbench.iconTheme": "vscode-icons",
"workbench.settings.useSplitJSON": true,
"window.zoomLevel": 0,