Skip to content

Instantly share code, notes, and snippets.

View emkis's full-sized avatar

Nicolas Jardim emkis

View GitHub Profile
@emkis
emkis / Comment.vue
Last active May 15, 2021 22:25
Dynamic Attachments in Vue.js 3
<template>
<h1>This is my comment</h1>
<p>
Current attachment type: <strong>{{ attachment.type }}</strong>
</p>
<button @click="() => (attachment.type = 'fake-type')">Invalid type</button>
<button @click="() => (attachment.type = 'Default')">Default</button>
<button @click="() => (attachment.type = 'Button')">Button</button>
@emkis
emkis / toFixedWithoutRounding.test.ts
Last active July 14, 2021 17:29
Number.toFixed but without rounding the numbers
describe('toFixedWithoutRounding()', () => {
it('should return a string', () => {
const result = toFixedWithoutRounding(3271.24);
expect(typeof result).toBe('string');
});
it('should return expected numbers', () => {
expect(toFixedWithoutRounding(7933.2982426892, 1)).toBe('7933.2');
expect(toFixedWithoutRounding(7933.2982426892, 2)).toBe('7933.29');
expect(toFixedWithoutRounding(7933.2982426892, 3)).toBe('7933.298');
@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 / 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 / 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()
@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 / 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 / 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 / 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 / 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