Skip to content

Instantly share code, notes, and snippets.

View ppeelman's full-sized avatar

Philippe Peelman ppeelman

View GitHub Profile
export const setCorrectViewportHeightUnit = (interval = 1000): void => {
let currentHeight: number | null = null;
const update = () => {
if(currentHeight !== window.innerHeight) {
currentHeight = window.innerHeight
const vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty('--vh', `${vh}px`);
}
}
export const firstLetter = (str: string): string => str[0];
export const toUppercase = (str: string): string => str.toUpperCase();
export const toLowercase = (str: string): string => str.toLowerCase();
export const trim = (str: string) => str.trim();
// https://stackoverflow.com/a/5717133/11351782
export function isValidUrl(str: string): boolean {
const pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
@ppeelman
ppeelman / .zshrc
Last active November 24, 2022 09:04
Switch Node version automatically using package.json(engines) or .nvmrc or .node-version
# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
# Get node version from package.json engines section (remove 'v' if present)
local package_json_node_version="$([ -r package.json ] && cat package.json | jq '.engines.node' --raw-output)"
@ppeelman
ppeelman / functional-utils.ts
Last active April 12, 2023 09:45
Functional programming in TypeScript
// If you are new to functional programming in JavaScript, the following is a must read!
// https://github.com/MostlyAdequate/mostly-adequate-guide
// Pipe and compose
// =================
// https://dev.to/ascorbic/creating-a-typed-compose-function-in-typescript-3-351i
export const pipe = <T extends any[], R>(fn1: (...args: T) => R, ...fns: Array<(a: R) => R>) => {
const piped = fns.reduce(