Skip to content

Instantly share code, notes, and snippets.

View David200197's full-sized avatar

David David200197

View GitHub Profile
@David200197
David200197 / ease-navigate-in-object.ts
Created July 30, 2024 04:51
ease navigate in object
const get = (data: any, pathStr: ) => {
const path = pathStr.split(".");
while (path.length) data = data[path.shift()!]
return data
}
const data = [{ items: [{ name: "David" }] }]
const name = get(data, "items.0.name") // => "David"
@David200197
David200197 / easy-class.ts
Last active August 9, 2024 01:27
easy class
// extract non-function properties from the prototype
type IsProp<T, K extends keyof T> = T[K] extends (...args: any[]) => any
? never
: K;
type ExtractProps<T extends { prototype: unknown }> = {
[k in keyof T["prototype"] as IsProp<T["prototype"], k>]: T["prototype"][k];
};
class Player {
name: string
@David200197
David200197 / delete_file_from_git_history.sh
Last active August 9, 2024 01:27
delete file from git history batch
# https://lavaldi.com/blog/como-eliminar-archivos-completamente-historial-git
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD
https://www.youtube.com/watch?v=RRuONS3NaNQ&ab_channel=midudev
1 - revisa en el error cual header falta
2 - ir a network
3 - vas a la response y vas a darle a agregar header y pones el header que falta
puedes usar un interceptor para solucionar el problema del header
NOTA: este caso se usa para casos donde el backend no maneja el cors (por ejemplo, al usar ngrok), o para pruebas.
Lo ideal es que siempre lo maneje el backend
@David200197
David200197 / prettify.ts
Created June 20, 2024 22:30
prettify yours unions
// merits to @mattpocockuk: https://x.com/mattpocockuk/status/1803348270799896789
type Prettify = { [K in keyof T]: T[K] } & {};
type Intersected = AdminUser & UserWithPerm & User
type Show = Prettify<Intersected>
/*
* type Show = {
* name: string;
@David200197
David200197 / fetch-interceptor.js
Last active May 30, 2024 21:54
feth interceptor to mock, validate and manipulate your requests and responses
// Credits to Indermohan Singh
// https://blog.logrocket.com/intercepting-javascript-fetch-api-requests-responses/
const fetchInterceptor = ({ requestInterceptor, responseInterceptor } = {}) => {
const { fetch: originalFetch } = window;
window.fetch = async (...args) => {
let [resource, config] = args;
let resRequestInterceptor;
@David200197
David200197 / load-file.ts
Last active May 29, 2024 22:43
load file without input:file
type Options = { multiple?: boolean; accept?: string };
export const loadFile = async ({ accept, multiple }: Options = {}) => {
return new Promise<FileList | null>((resolve)=>{
const selectorFile = document.createElement("input");
selectorFile.type = "file";
if (multiple) selectorFile.multiple = multiple;
if (accept) selectorFile.accept = accept;
selectorFile.addEventListener("cancel", () => {
@David200197
David200197 / randomInt.ts
Last active August 9, 2024 16:58 — forked from rodrigograca31/randomInt.ts
Random Integer generator (Typescript)
/**
* Generates a random integer between min and max (inclusive)
* @param {number} min
* @param {number} max
* @returns randomly generated integer
*/
const randomInt = (min: number, max: number): number => {
return Math.floor(Math.random() * (max - min + 1) + min);
};
@David200197
David200197 / README.md
Created February 7, 2024 01:10 — forked from backendi/README.md
Ejecutar Python desde Node.js

Ejecutar un script de Python desde Node.js

Código que acompaña al video Ejecutar Python desde Node.js

Descripción

Script de Node.js que ejecuta un script de Python. script_node.js invoca a script_python.py como un subproceso utilizando la función spawn. Se establece una comunicación mediante stdin/stdout donde script_node.js envía un texto simple a script_python.py

Pre-requisitos

@David200197
David200197 / decorator_in_vite.md
Last active August 9, 2024 01:28
Using decorators in vite apps

Step - 1: install reflect-metadata

npm install reflect-metadata
pnpm add reflect-metadata
yarn add reflect-metadata