This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const lazy = (componentFactoryPromise) => { | |
const componentPromise = componentFactoryPromise() | |
let Component | |
componentPromise.then(C => { | |
Component = C.default | |
}) | |
return (props) => { | |
if(!Component) { | |
throw componentPromise |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function delay(ms) { | |
return new Promise(resolve => { | |
setTimeout(resolve, ms); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverseInPlace(arr) { | |
let length = arr.length, | |
middle = Math.floor(length / 2), | |
temp = null | |
for(let i = 0; i < middle; i++) { | |
temp = arr[i] | |
arr[i] = arr[arr.length - 1 - i] | |
arr[arr.length - 1 - i] = temp | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Grid from 'muuri' | |
interface GapFillerProps { | |
height: number | |
columnId: number | |
gapHeight?: number | |
} | |
export const calculateGaps = (grid: Grid): GapFillerProps[] => { | |
if (!grid) return [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useRouter } from 'next/router'; | |
import { useRef } from 'react'; | |
export const usePreviousRoute = () => { | |
const router = useRouter(); | |
const ref = useRef<string | null>(null); | |
router.events?.on('routeChangeStart', () => { | |
ref.current = router.asPath; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useState, useEffect } from 'react' | |
export function useScreenSize(screenSize: number): boolean[] { | |
const [isActive, setIsActive] = useState<boolean>(false) | |
const handleScreenResize = useCallback(() => { | |
const isActive = window.matchMedia(`(min-width: ${screenSize}px)`) | |
setIsActive(isActive.matches) | |
}, [setIsActive, screenSize]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function bind(target, { type, listener, options }) { | |
target.addEventListener(type, listener, options); | |
return function unbind() { | |
target.removeEventListener(type, listener, options); | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const priceUtility = (pr: string | number): string => { | |
// remove spaces | |
const price: string = pr.toString().replaceAll(' ', ''); | |
// get hole part | |
const holePart: number = parseInt(price); | |
// get float part of price if exist | |
const floatPart: string = | |
parseFloat(price) - holePart === 0 | |
? '' | |
: (parseFloat(price) - holePart).toFixed(2).substring(1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from 'react' | |
export function useImageToBase64( | |
src: string, | |
callback: (res: string) => void, | |
outputFormat?: string | |
) { | |
useEffect(() => { | |
let img = new Image() // Image constructor equivalent to document.createElement | |
img.crossOrigin = 'Anonymous' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getType(obj) { | |
const lowerCaseTheFirstLetter = (str) => str[0].toLowerCase() + str.slice(1); | |
const type = typeof obj; | |
if (type !== 'object') { | |
return type; | |
} | |
return lowerCaseTheFirstLetter( | |
Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1') | |
); |
NewerOlder