Skip to content

Instantly share code, notes, and snippets.

View Minozzzi's full-sized avatar

Guilherme Minozzi Minozzzi

View GitHub Profile
@Minozzzi
Minozzzi / atLeastOne.ts
Created March 15, 2024 21:53
This type accepts at least one property
type AtLeastOnePropertyOf<T, K extends keyof T> = K extends K ? Record<K, T[K]> : never
type User = {
name: string;
age: number;
}
type UpdateUser = AtLeastOnePropertyOf<User, keyof User> & Partial<User>
const user = {
name: 'name',
@Minozzzi
Minozzzi / exactlyOne.ts
Created March 15, 2024 19:22
This type accepts only one property
type ExactlyOne<T> = {
[K in keyof T]: Pick<T, K> & Partial<Record<Exclude<keyof T, K>, never>>
}[keyof T]
type User = {
name: string;
age: number;
}
function updateExactlyOne(user: User, newUser: ExactlyOne<User>) {
@Minozzzi
Minozzzi / kill_ports.sh
Created February 15, 2024 12:20
Shell script to terminate processes on ports
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Uso: $0 PORTA"
exit 1
fi
PORTA=$1
PIDS=$(sudo lsof -t -i:$PORTA)
@Minozzzi
Minozzzi / namedGroupRegex.js
Created August 3, 2022 18:42
Named Group Regex
const re = /(?<name>\w+)\s(?<age>\d+)/;
const matches = re.exec('John 30');
const [match, name, age] = re.exec('John 30'); // ['John 30', 'John', '30']
console.log(matches.groups); // { name: 'John', age: '30' }
@Minozzzi
Minozzzi / goBack.js
Created May 6, 2022 12:57
Send user to back page
const navigateBack = () => history.back();
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@Minozzzi
Minozzzi / searchWord.ts
Last active May 6, 2022 13:04
This gist is useful to look up a word in a given string
const searchWord = (text: string, wordToSearchInText: string): boolean => text.includes(wordToSearchInText)