Skip to content

Instantly share code, notes, and snippets.

View theer1k's full-sized avatar
🏠
Working from home

Erik Henrique theer1k

🏠
Working from home
View GitHub Profile
@theer1k
theer1k / History|-1017dc29|entries.json
Last active September 7, 2022 20:53
VS Code Settings
{"version":1,"resource":"file:///home/erik/Projects/ecommerce-front/src/hooks/useInView/index.tsx","entries":[{"id":"afXk.tsx","source":"Fix all fixable ESLint issues","timestamp":1658516663546},{"id":"yblD.tsx","source":"Add import from \"@/helpers/ssrHelper\"","timestamp":1658516710521},{"id":"NxMr.tsx","timestamp":1658516715321},{"id":"D7Nn.tsx","source":"Fix all fixable ESLint issues","timestamp":1659460328205}]}
@theer1k
theer1k / throttle.spec.ts
Last active September 22, 2022 12:20
Throttle function
import { throttle } from './throttle';
beforeEach(() => {
jest.useFakeTimers('modern');
});
describe('helpers/throttle', () => {
it('when delayed', () => {
const fn = jest.fn();
const throttleFn = throttle(fn, 3000);
@theer1k
theer1k / reactComposer.tsx
Created February 4, 2022 18:38
React Composer
import { ReactNode } from 'react';
export type ComponentWithChildren = (props: {
children: ReactNode;
}) => JSX.Element;
export const composer = (...providers: ComponentWithChildren[]) => {
const ComposedComponents: ComponentWithChildren = ({ children }) => {
return (
<>
@theer1k
theer1k / mostFrequentDays.js
Last active April 12, 2022 13:07
[Javascript] Most Frequent Days
const mostFrequentDays = year => {
let dayName = d => d.toLocaleString('en',{ weekday:'long' });
let firstDay = new Date(year, 0, 1);
let day = dayName(firstDay);
let lastDay = dayName(new Date(year, 11, 31));
let daysResult = day == lastDay? [day] : [day, lastDay];
return firstDay.getDay() ? daysResult : daysResult.reverse();
};
@theer1k
theer1k / __fileName__.spec.tsx__tmpl__
Created April 28, 2022 20:38
Nx reactfc generator
import React from 'react';
import { render } from '@testing-library/react';
import { <%= className %> } from './<%= fileName %>';
describe('<<%= className %>/>', () => {
it('should render successfully', () => {
const { baseElement } = render(<<%= className %> />);
expect(baseElement).toBeTruthy();
});
@theer1k
theer1k / pick.ts
Last active March 4, 2023 23:12
[Typescript] - Pick
const pick = <T, K extends keyof T>(obj: T, properties: Array<K>) =>
Object.fromEntries(properties.map((key) => [key, obj[key]])) as Pick<T, K>;
interface ExampleObject {
id: number;
name: string;
birthDate: string;
idol: string;
}
@theer1k
theer1k / useRunOnce.tsx
Last active September 22, 2022 12:19
useRunOnce
import React, { useEffect, useRef } from "react";
export type useRunOnceProps = {
fn: () => any;
sessionKey?: string;
};
const useRunOnce: React.FC<useRunOnceProps> = ({ fn, sessionKey }) => {
const triggered = useRef<boolean>(false);
@theer1k
theer1k / useThrottledValue.tsx
Created August 18, 2022 01:05
useThrottledValue
import {
useCallback, useEffect, useRef, useState,
} from 'react'
const DEFAULT_THROTTLE_MS = 3000
const getRemainingTime = (lastTriggeredTime: number, throttleMs: number) => {
const elapsedTime = Date.now() - lastTriggeredTime
const remainingTime = throttleMs - elapsedTime
@theer1k
theer1k / useThrottledFunction.tsx
Created August 18, 2022 01:06
useThrottledFunction
import { useCallback, useEffect, useRef } from 'react'
const DEFAULT_THROTTLE_MS = 800
const getRemainingTime = (lastTriggeredTime: number, throttleMs: number) => {
const elapsedTime = Date.now() - lastTriggeredTime
const remainingTime = throttleMs - elapsedTime
return (remainingTime < 0) ? 0 : remainingTime
}
@theer1k
theer1k / namedComponent.ts
Created September 20, 2022 17:10
Named component
export const namedComponent = async <T, N extends keyof T>(
modPromise: Promise<T>,
exportName: N,
) => {
const mod = await modPromise;
return mod[exportName];
};