Skip to content

Instantly share code, notes, and snippets.

View gtrabanco's full-sized avatar
:octocat:

Gabriel Trabanco gtrabanco

:octocat:
  • Spain
View GitHub Profile
@gtrabanco
gtrabanco / typescript.json
Last active July 16, 2023 15:44
Snippets for typescript VSCode
{
"zschema": {
"prefix": "zschema",
"body": [
"export const $1Schema = z.$2($3);",
"export type ${1/(.*)/${1:/pascalcase}/} = z.infer<typeof $1Schema>;",
"",
"$0"
],
"description": "Creates a function wrapper for a model's attribute."
@gtrabanco
gtrabanco / runtime.js
Last active August 24, 2023 16:20
Get the runtime, browser or web worker
function isBun() {
return Boolean(globalThis.Bun);
}
function isDeno() {
return Boolean(globalThis.Deno);
}
function isNode() {
return Boolean(globalThis.process?.versions?.node);
@gtrabanco
gtrabanco / snake-camel.ts
Created June 27, 2023 16:32
Snake & Camel cases conversors
77
export type RemoveUnderscoreFirstLetter<S extends string> =
S extends `${infer FirstLetter}${infer U}`
? `${FirstLetter extends '_' ? U : `${FirstLetter}${U}`}`
: S;
export type CamelToSnakeCase<S extends string> =
S extends `${infer T}${infer U}`
? `${T extends Capitalize<T> ? '_' : ''}${RemoveUnderscoreFirstLetter<
Lowercase<T>
@gtrabanco
gtrabanco / parser.ts
Last active May 23, 2023 00:54
Idea to parse document
import { genericMapToType } from './generic-map-to-type.ts';
export type ScraperSetter = (property: string, value?: any) => void;
type Key = Exclude<any, Function>;
type Value = Exclude<any, Function>;
export type ScrapeHandler = {
selector: string;
handler: HTMLRewriterTypes.HTMLRewriterElementContentHandlers;
@gtrabanco
gtrabanco / bun-sse.ts
Created November 15, 2022 15:00
Bun Server Sent Events
// bun --hot sse.ts
import { randomUUID } from "node:crypto";
import { EventEmitter } from "node:events";
const sseEvents = new EventEmitter();
export const sse = (data) => {
sseEvents.emit(
"sse",
`id: ${randomUUID()}\ndata: ${JSON.stringify(data)}\n\n`
@gtrabanco
gtrabanco / sse-server.js
Created November 7, 2022 22:07
sse express sample
import { randomUUID } from "crypto";
import { EventEmitter } from "events";
import express from "express";
function formatEvent(event) {
const { event = "message", data = {}, id = randomUUID(), retry = undefined } = event;
const resultString = retry ? `retry: ${retry}\n` : "";
resultString += `id: ${id}\n`;
resultString += `event: ${event}\n`;
@gtrabanco
gtrabanco / use-stopwatch.ts
Created August 23, 2022 23:55
hook to make a stopwatch or countdown
import { useEffect, useState } from "react";
import { StopwatchLap } from "types/schemas/stopwatch-lap";
import getTimeFromMiliseconds from "./lib/get-time-from-miliseconds";
const STOPWATCH_TICK_MILISECONDS = 10;
export declare interface UseStopwatchValues {
isRunning: boolean;
isEnded: boolean;
totalTime: number; // total time if always forward but the calculation can be backwards
@gtrabanco
gtrabanco / useKeyboardShortcut.js
Created May 20, 2022 13:32
Keyboard shortcuts
import { useCallback, useEffect, useState } from 'react';
const commonEvent = (event) => {
event?.preventDefault?.();
event?.stopPropagation?.();
event?.stopImmediatePropagation?.();
};
export default function useKeyboardShortcut() {
const [shortcuts, setShortcuts] = useState([]);
@gtrabanco
gtrabanco / parseURLWithParams.mjs
Created February 16, 2022 11:47
Parse urls like express router or react router to accept params in url and replace params in url
function getURLParams(urlWithParams) {
const paramsURLParts = urlWithParams.split('/');
let params = {};
for (const index in paramsURLParts) {
let parsedParam;
let part = paramsURLParts.at(index);
if (part.match(/:([\w_]{1}[\w0-9-_]+)/i)) {
part = part.replace(':', '');
}
// Optional
@gtrabanco
gtrabanco / device_config.ini
Created January 12, 2022 18:48
Device Configuration: Conga 5090
[Device_Config]
software_version=S3.2.6.25.06R
hardware_version=1.0.1
robot_app_version=5
algorithm_version=2.0.1.180301_Alpha25
stm32_version=1.0.0_19052806
lidar_version=LIDAR_VERSION_UNKNOW
device_id=3.506000e+03
device_type=23
voice_enable=1