Skip to content

Instantly share code, notes, and snippets.

View emkis's full-sized avatar

Nicolas Jardim emkis

View GitHub Profile
@emkis
emkis / a
Last active January 21, 2024 01:15
#/ something weird like migration and churches *(
...
@emkis
emkis / event-emitter.test.ts
Last active October 9, 2023 10:57
Simple TypeScript implementation of the event emitter pattern
import { createEventEmitter } from './event-emitter'
type ExampleEvents = {
add_todo: { id: string; text: string }
complete_todo: { id: string }
delete_todo: { id: string }
}
beforeEach(jest.clearAllMocks)
@emkis
emkis / create-safe-query-param.ts
Last active December 6, 2023 22:05
Simple abstraction for managing safe query params
export type SerializedString = string;
export type SafeQueryParamOptions<Value> = {
getUnsafeQueryParam(): SerializedString | undefined;
serialize(safeValue: Value): SerializedString;
deserialize(unsafeValue: SerializedString): Value;
parse(unsafeValue: Value): Value;
onParseError(error: unknown): void;
};
@emkis
emkis / call-all.ts
Last active June 21, 2023 13:32
A TypeScript version of the callAll function
/* eslint-disable @typescript-eslint/no-explicit-any */
type CallBack<Params extends any[]> = {
(...args: Params): void;
};
export function callAll<Params extends any[]>(...fns: Array<CallBack<Params> | undefined>) {
return (...args: Params) => fns.forEach((fn) => typeof fn === 'function' && fn(...args));
}
@emkis
emkis / clean-package-json.ts
Created January 23, 2023 00:51
Script to clean `package.json` properties before publishing a package
import packageJson from '../package.json'
import { writeFile } from 'fs/promises'
import { resolve } from 'node:path'
const unwantedProperties = ['keywords', 'files', 'scripts', 'devDependencies']
unwantedProperties.forEach(deleteProperty)
rewritePackageJson()
function deleteProperty(key: string) {
@emkis
emkis / api-usage.js
Last active November 22, 2022 23:04
JavaScript composable validation
import { composeValidation, v } from './validation'
const validateRequirement = composeValidation([
v.min(10, 'cannot be empty'),
v.hasUpperCase('at least one uppercase letter'),
v.hasNumber('at least one number character'),
v.hasSpecialCharacter('at least one special character'),
v.max(64, 'max character limit (64) reached'),
]);
@emkis
emkis / builder.ts
Created May 14, 2022 03:45
TypeScript Patterns in a functional approach
type ShallowObject = Record<string, string | number | boolean>
type User = Partial<{
name: string
email: string
}> & ShallowObject
type UserBuilder = {
addName: (name: string) => UserBuilder
addEmail: (email: string) => UserBuilder
@emkis
emkis / git-configs.js
Last active December 4, 2023 13:16
Node.js script (npx) to set all my Git configurations.
#!/usr/bin/env node
const shell = require('shelljs')
function setGitAlias({ name, command }) {
return shell.exec(`git config --global alias.${name} "!${command}"`)
}
shell.echo('💅 Setting git configurations...')
// User
@emkis
emkis / backwards-compatible-styles.css
Created May 2, 2022 14:40
CCS Rules for applying focus on Keyboard only
.CoolButton {
/*  Your regular Button styles */
background: #000;
padding: 16px 32px;
border-radius: 8px;
color: #fff;
font-size: 16px;
font-weight: bold;
box-sizing: border-box;
border: 0;
@emkis
emkis / script.js
Last active July 7, 2022 10:39
Memhack easy click automation
let isRateRunning = false
init()
function init() {
removeAnimations()
watchKeyboardShortcut()
}
function watchKeyboardShortcut() {
const { start, stop } = setAutomaticRate()