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 / extract-commits-from-pr.util.js
Created February 26, 2025 16:14
Simple function to extract all text commits from pull request page.
function extractCommitsFromPR() {
return [
...document
.querySelector('#commits_bucket')
.querySelectorAll('li.Box-row a.js-navigation-open'),
].map(commit => commit.textContent);
}
@gustavomorinaga
gustavomorinaga / example.svelte
Last active October 4, 2024 19:37
A customizable list component using `shadcn-svelte`. The component allows specifying the list tag type (`ul` or `ol`) and supports additional class names for styling.
<script lang="ts" context="module">
import * as Card from '$lib/components/ui/card';
import { List } from '$lib/components/ui/list';
import type { TUser } from '$lib/ts';
</script>
<script lang="ts">
const data: Array<TUser> = [
{
id: 1,
@gustavomorinaga
gustavomorinaga / example.svelte
Last active October 4, 2024 19:43
A `shadcn-svelte` wrapper component for a virtualized list using the virtua/svelte library. The file includes TypeScript script tags to import necessary types and define component properties, events, and slots. The key feature of this component is its ability to efficiently render large lists by only displaying items currently in the viewport, i…
<script lang="ts" context="module">
import * as Card from '$lib/components/ui/card';
import { VirtualList } from '$lib/components/ui/virtual-list';
import type { TUser } from '$lib/ts';
</script>
<script lang="ts">
const data: Array<TUser> = [
{
id: 1,
@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);
@gustavomorinaga
gustavomorinaga / framer-number-animation.tsx
Created January 22, 2022 11:14
Code to make number animation on Framer Motion
import { animate } from "framer-motion";
import React, { useEffect, useRef, useState } from "react";
import "./styles.css";
function Counter({ from, to }) {
const nodeRef = useRef();
useEffect(() => {
const node = nodeRef.current;