Skip to content

Instantly share code, notes, and snippets.

View ortense's full-sized avatar
💀
Coding...

Marcus Ortense ortense

💀
Coding...
View GitHub Profile
@ortense
ortense / getBrowserName.ts
Created January 17, 2024 14:09
get browser name by user agent
export type BrowserName = 'edge' | 'chrome' | 'firefox' | 'safari' | 'opera' | 'ie';
export type BrowserCheck = (userAgent: UserAgent) => boolean;
export type UserAgent = string;
const BrowserCheckMap: Record<BrowserName, BrowserCheck> = Object.freeze({
edge: agent => /Edg/.test(agent),
chrome: agent => /Chrome/.test(agent),
firefox: agent => /Firefox/.test(agent),
safari: agent => /Safari/i.test(agent) && !/Chrome/i.test(agent),
opera: agent => /Opera/.test(agent),
@ortense
ortense / functional-decorators.md
Last active December 27, 2023 21:07
Uso de Decoradores funcionais em projeto typescript

Uso de Decoradores funcionais em projeto typescript

Introdução

Proposta de adoção de decorators funcionaais em projetos typescript para melhorar a modularidade e flexibilidade do código. O padrão de decoradores permite adicionar comportamentos adicionais a funções existentes sem a necessidade de modificar diretamente o código dessas funções.

Padrão de Decoradores

O padrão de decorator é uma técnica de design que segue o princípio aberto/fechado do SOLID, permitindo a extensão de funcionalidades sem modificar o código existente. Os decorators funcionais são funções que envolvem outras funções para adicionar ou alterar comportamentos.

@ortense
ortense / Heading.tsx
Created December 19, 2023 22:52
Typography react component
import { Typography, TypographyProps } from './Typography'
import { Size } from './types'
export type HeadingLevel = '1' | '2' | '3' | '4' | '5' | '6'
export type HeadingProps = Omit<TypographyProps, 'size'> & {
level?: HeadingLevel
}
export const HeadingLevelSize: Record<HeadingLevel, Size> = {
@ortense
ortense / Exemple.tsx
Last active December 18, 2023 13:31
Simple components for resposive layout in react
import { Box, VerticalStack, HorizontalStack, Center } from './Flex';
export function Example() {
return (
<Center
as="section"
style={{
backgroundColor: '#292929',
height: '100vh',
padding: '1rem',
@ortense
ortense / node-environment-setup.ts
Created November 2, 2023 18:43
Node app safe configuration
import { parseArgs } from 'node:util'
import { env } from 'node:process'
import { Input, Issue, Output, boolean, custom, object, safeParse, string, transform } from 'valibot'
// @infra/environment/environment-error.ts
export class EnvironmentError extends Error {
constructor(public readonly issues: Issue[]) {
super('Environment Error')
}
const BASE_URL = 'https://pokeapi.co/api/v2/pokemon/'
function fetchJSON(...args) {
return fetch(...args).then(response => response.json())
}
export async function fetchPokemonWithDetails() { // maybe pass the offset and limit as an argument
const results = await fetchJSON(BASE_URL)
.then(data => data.results)
@ortense
ortense / builder.ts
Created November 1, 2021 18:14
Generic objet builder in typescript
export type Builder<T> = {
build(): Partial<T>;
append<K extends keyof T>(key: K, value: T[K]): Builder<T>;
};
export function createBuilder<T>(): Builder<T> {
const obj: Partial<T> = {};
const builder: Builder<T> = {
build: () => ({ ...obj }),
@ortense
ortense / middleware.spec.ts
Created December 1, 2020 17:23
How create a unit test a express middleware writed in typescript with jest
import { Application, Request, Response, NextFunction, RequestHandler } from 'express'
describe('How to test a express middleware', () => {
const simpleMiddleware: RequestHandler = (req, res) => {
return res.status(200).json({
message: `App running at http://localhost:${req.app.locals.config.port}`
})
}
const mockApp: Partial<Application> = {
@ortense
ortense / deep-clone.js
Last active March 17, 2020 20:54
javascript deep clone without JSON.parse
const cloneArray = array => array.map(deepClone)
const cloneObject = object =>
Object.keys(object)
.reduce((clone, key) => ({ ...clone, [key]: deepClone(object[key])}), {})
export const deepClone = value =>
Array.isArray(value)
? cloneArray(value)
: value !== null && typeof value === 'object'
#ANDROID
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
alias avd='$ANDROID_HOME/emulator/emulator'
#FLUTTER
export FLUTTER_PATH=$HOME/flutter/bin