Skip to content

Instantly share code, notes, and snippets.

View David200197's full-sized avatar

David David200197

View GitHub Profile
@David200197
David200197 / MultipleProviderComponents.tsx
Last active January 1, 2025 18:57
compose multiple providers in react
import { composeProviders, createProvider } from "compose-providers"
import { LocalProvider, DateServiceProvider, LayoutProvider } from "providers"
import { ReactNode } from "react"
const Provider = composeProviders([
LocaleProvider
createProvider(DateServiceProvider, { dateServiceType: "luxon" }),
LayoutProvider,
]);
@David200197
David200197 / UrlBuilder.ts
Last active December 30, 2024 17:38
url builder for any type of urls (partial or absolute)
export class UrlBuilder {
private fullUrl: string
private queries: string[] = [];
constructor(baseUrl: string, ...urls: string[]) {
this.fullUrl = [baseUrl, ...urls].join("/");
}
public addQuery(key: string, value: string | number | boolean) {
this.queries.push(`${key}=${value}`);
@David200197
David200197 / DispatchComponent.tsx
Created December 30, 2024 17:26
control custom event with react
import { dispatchEvent } from "./event-control";
export const DispatchComponent = () => {
const onClick = () => {
dispatchEvent("ADD_COUNT")
}
return <button onClick={onClick}>Add</button>
}
@David200197
David200197 / removeAttribute.ts
Created September 10, 2024 15:08
remove attribute arrays in typescript
const removeAttribute = (dataArray: any[], attribute: string) => dataArray.map(({ [attribute]: _, ...rest }) => rest);
@David200197
David200197 / optional.ts
Last active November 5, 2025 20:44
rust optional in typescript
/**
* A container object which may or may not contain a non-null value.
* If a value is present, `isPresent()` returns `true` and `get()` returns the value.
*
* Additional methods that depend on the presence or absence of a contained value
* are provided, such as `orElse()` and `ifPresent()`.
*
* @template T The type of the value
* @example
* const name = Optional.ofNullable(user?.name)
@David200197
David200197 / example.ts
Last active November 5, 2025 20:39
rust result in typescript
/**
* Custom error types for domain-specific errors
*/
class DatabaseError extends Error {
constructor(message: string, public readonly query?: string) {
super(message);
this.name = 'DatabaseError';
}
}
// es mejor usar SETS que ARRAYS para combrobar si un valor existe dentro de un conjunto de valores
// - evitas repetir valores
// - es mas rapido de buscar con has que includes
// con arrays
const colors = ["red", "green", "blue"] as const;
const isColors = colors.includes("red") // true
// con sets
const colors = new Set(["red", "green", "blue"] as const)
@David200197
David200197 / accepts_any_not_nullish_value.ts
Last active August 14, 2024 16:14
accepts any not nullish value
type NotNullish = {}
const acceptsAnyNotNullishValue = (input: NotNullish) => {}
acceptsAnyNotNullishValue("value")
acceptsAnyNotNullishValue(123)
acceptsAnyNotNullishValue(false)
// you take a error
acceptsAnyNotNullishValue(null)
@David200197
David200197 / autocomplete_types.ts
Created August 9, 2024 16:41
autocomplete types
//https://x.com/mattpocockuk/status/1821926395380986219
//No autocomplete for "primary" and "secondary"
type Color = "primary" | "secondary" | string
// Autocomplete "primary" and "secondary", but also allows any string
type Color = "primary" | "secondary" | (string & {})
@David200197
David200197 / three_example.ts
Last active August 9, 2024 04:56
three example in typescript
class NodeThree<T> {
private id: Symbol;
private nextIds: Symbol[] = [];
private prevId: Symbol = Symbol()
private value: T
private constructor(id: Symbol, value: T) {
this.id = id;
this.value = value;
}