Skip to content

Instantly share code, notes, and snippets.

View g4rcez's full-sized avatar
🇧🇷
🎧

Allan Garcez g4rcez

🇧🇷
🎧
View GitHub Profile
@g4rcez
g4rcez / use-axios.ts
Last active November 30, 2020 05:16
use-axios hook to avoid memory leak on unmout react components
import axios, { AxiosInstance, AxiosRequestConfig, AxiosStatic, CancelTokenSource } from "axios";
import { useCallback, useEffect, useMemo, useRef } from "react";
type Tokens = { [k: string]: CancelTokenSource | undefined };
const createUid = () =>
Math.random()
.toString(36)
.substring(16);
@g4rcez
g4rcez / joked-components.tsx
Created October 28, 2020 03:22
A complete example for how to create your styled-components
import classNamesDedupe from "classnames/dedupe";
import React, { useState, DependencyList, useMemo } from "react";
type ClassArray = ClassValue[];
type ClassDictionary = { [id: string]: any };
export type ClassValue = string | number | ClassDictionary | ClassArray | undefined | null | boolean;
export const useClassNames = (dependency: DependencyList, ...classes: ClassValue[]) =>
import React, { useLayoutEffect, useMemo, useState } from "react";
import { useClassNames } from "styleguide";
type Html = React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
type StyledArgs<E, T> = ((args: E & T) => string | number) | string | number;
function Styled<ExtraProps = unknown, Element = Html>(tag: string) {
return ([first, ...placeholders]: TemplateStringsArray, ...a: StyledArgs<Element, ExtraProps>[]) => {
return ({ children, ...props }: Html & ExtraProps) => {
@g4rcez
g4rcez / gist:160005a0d392d25b3150f95b51e926b7
Last active October 21, 2020 03:41
Emulate zoaded-components.tsx for div component
import React, { useLayoutEffect, useMemo } from "react";
import { useClassNames } from "styleguide";
const AD_REPLACER_R = /(a)(d)/gi;
const charsLength = 52;
const getAlphabeticChar = (code: number) => String.fromCharCode(code + (code > 25 ? 39 : 97));
function generateAlphabeticName(code: number) {
let name = "";
let x;
import { add, differenceInDays, eachDayOfInterval, endOfMonth, isSameMonth, startOfMonth, sub } from "date-fns";
import { dec, inc } from "ramda";
import React, { useEffect, useMemo, useState } from "react";
import "styleguide/dist/index.css";
const getWeekDays = (locale: string) =>
Array.from({ length: 7 }).map((_, v) =>
new Date(Date.UTC(1970, 0, 5 + v)).toLocaleDateString(locale, { weekday: "short" })
);
type Locale = {
decimal: ".";
thousands: ",";
grouping: [3];
currency: ["$", ""];
minus: "-";
numerals?: string;
percent?: string;
nan?: string;
};
import { useState, useEffect } from "react";
type Checks = Map<string, boolean>;
const createFromMap = (list: Input[]): Checks => new Map(list.map((x) => [x.name!, x.checked!]));
const getOnlyChecked = (map: Checks) => {
const values: string[] = [];
map.forEach((checked, key) => (checked ? values.push(key) : undefined));
return values;
interface PromiseLike<T> {
then<R1 = T, R2 = never>(
resolve?: ((value: T) => R1 | PromiseLike<R1>) | undefined | null,
reject?: ((reason: any) => R2 | PromiseLike<R2>) | undefined | null
): PromiseLike<R1 | R2>;
}
type ReadonlyPromise<T> = PromiseLike<Readonly<T>> | Readonly<T>;
const PromiseAll = async <T>(...values: ReadonlyPromise<T>[]): Promise<T> => {
export const equals = (a: any, b: any): boolean => {
if (a === b) {
return true;
}
if (a instanceof Date && b instanceof Date) {
return a.getTime() === b.getTime();
}
if (!a || !b || (typeof a !== "object" && typeof b !== "object")) {
return a === b;
}
class SymbolMap<K extends object, V> implements Map<K, V> {
private map: Map<K, V>;
constructor() {
this.map = new Map<K, V>();
this.size = 0;
}
clear(): void {
this.map.clear();