Skip to content

Instantly share code, notes, and snippets.

View gustavomorinaga's full-sized avatar
🎯
Focusing

Gustavo Morinaga gustavomorinaga

🎯
Focusing
View GitHub Profile
@gustavomorinaga
gustavomorinaga / search-keywords.util.ts
Last active December 9, 2023 19:08
A simple search engine that mimics Google's search engine in TypeScript.
/**
* Represents the search keywords for a search engine.
*/
type TSearchKeywords = {
exact: Array<string>;
partial: Array<string>;
exclude: Array<string>;
};
/**
@gustavomorinaga
gustavomorinaga / handle-multiple-promises-with-signals.ts
Last active December 7, 2023 21:58
This code efficiently handles asynchronous requests, measures their performance, structures responses for later processing or logging, and cancels network operations that time out.
/**
* Represents a request object.
*/
export type TRequest = {
id: number;
title: string;
url: string;
timeout: number;
};
@gustavomorinaga
gustavomorinaga / overridable.store.ts
Created September 18, 2023 02:57
Overridable Store for Svelte
import type { Updater, Writable } from 'svelte/store';
export type ChangeFn<T> = (args: { curr: T; next: T }) => T;
export const overridable = <T>(store: Writable<T>, onChange?: ChangeFn<T>) => {
const update = (updater: Updater<T>, sideEffect?: (newValue: T) => void) =>
store.update((curr) => {
const next = updater(curr);
let res: T = next;
if (onChange) res = onChange({ curr, next });
@gustavomorinaga
gustavomorinaga / validator.util.ts
Created July 21, 2023 17:59
Best way to parse and validate data with Zod in any context.
import { AnyZodObject, ZodError, z } from 'zod';
import { fromZodError } from 'zod-validation-error';
export async function zParse<T extends AnyZodObject>(
schema: T,
data: any
): Promise<z.infer<T>> {
try {
return await schema.parseAsync(data);
} catch (error) {
@gustavomorinaga
gustavomorinaga / groupBy.js
Created March 11, 2022 04:20
Agrupando dados de um array com JS
const users = [
{ id: 1, name: 'Bruno', group: 'admin' },
{ id: 2, name: 'João', group: 'coder' },
{ id: 3, name: 'Maria', group: 'coder' },
];
// --- Solution 01 ---
function groupBy(array, callback) {
return array.reduce((accumulator, item) => {
const key = callback(item);
@omariosouto
omariosouto / ButtonLink.js
Last active May 22, 2025 13:50
Aula 01 - Códigos Extras
<h1 align="center">
<br>
<img src="YOUR_LOGO_URL" alt="YOUR_PROJECT_NAME" width="120">
<br>
<br>
YOUR_PROJECT_NAME
</h1>
<p align="center">A little description about your project</p>
@fernandosavio
fernandosavio / pt-br.js
Created September 1, 2014 13:09
Locale pt-br of Moment.js
// moment.js locale configuration
// locale : brazilian portuguese (pt-br)
// author : Caio Ribeiro Pereira : https://github.com/caio-ribeiro-pereira
(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['moment'], factory); // AMD
} else if (typeof exports === 'object') {
module.exports = factory(require('../moment')); // Node
} else {