This file contains 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 toBase64 = (str: string): string => { | |
return typeof window === 'undefined' ? Buffer.from(str).toString('base64') : btoa(str) | |
} | |
export const fromBase64 = (str: string): string => { | |
return typeof window === 'undefined' ? Buffer.from(str, 'base64').toString() : atob(str) | |
} |
This file contains 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 getChunks = <T = unknown>(arr: T[], length: number = 1): T[][] => { | |
const chunks = [] | |
for (let index = 0; index < arr.length; index += length) { | |
chunks.push(arr.slice(index, index + length)) | |
} | |
return chunks | |
} |
This file contains 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
// Universal, non case-sensitive, protocol-agnostic approach: | |
const reg = new RegExp('^(?:[a-z]+:)?//', 'i') | |
export const isAbsoluteUrl = (value: string): boolean => reg.test(value) |
This file contains 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 { uniqueInOrder } from './unique-in-order' | |
const obj1 = { id: '1' } | |
const obj2 = { id: '2' } | |
const obj3 = { id: '3' } | |
const arr1 = [1, 2, 3] | |
const arr2 = [4, 5, 6] | |
const arr3 = [7, 8, 9] |
This file contains 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 { toSequence } from './to-sequence' | |
test('`toSequence` should work correctly', () => { | |
expect(toSequence([])).toEqual([]) | |
expect(toSequence(['a'])).toEqual([['a', undefined]]) | |
expect(toSequence(['a', 'b'])).toEqual([['a', 'b']]) | |
expect(toSequence(['a', 'b', 'c'])).toEqual([['a', 'b'], ['b', 'c']]) |
This file contains 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 * as React from 'react' | |
import { useSelector } from 'react-redux' | |
import type { OutputSelector, OutputParametricSelector } from 'redux-views' | |
type SelectorWithIdAndUse = (OutputSelector<any, any, any> | OutputParametricSelector<any, any, any, any>) & { | |
use?: (...args: any[]) => any | |
idSelector?: (...args: any[]) => any | |
} | |
export const usePropsSelector = <T extends SelectorWithIdAndUse = SelectorWithIdAndUse>( |
This file contains 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 type { MutableRefObject } from 'react' | |
import { useRef } from 'react' | |
const noop = {} | |
export function useLazyRef<T>(init: () => T) { | |
const ref = useRef<T | typeof noop>(noop) | |
if (ref.current === noop) { | |
ref.current = init() |
This file contains 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 { FlightSearchStep, getNextStep, getPrevStep, SeatType } from './index' | |
const VALID_STEPS_MAP: { | |
[key: string]: { | |
[key: string]: { | |
prev: FlightSearchStep | undefined | |
next: FlightSearchStep | undefined | |
} | |
} | |
} = { |
This file contains 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 ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
const MIN_RADIX = 2 | |
const MAX_RADIX = ALPHABET.length | |
function chekRadix(radix: number): void { | |
if (radix < MIN_RADIX || radix > MAX_RADIX) { | |
throw new Error(`radix argument must be between ${MIN_RADIX} and ${MAX_RADIX}`) | |
} | |
} |
This file contains 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 useEffectOnce = (effect: EffectCallback) => { | |
const destroyFunc = useRef<ReturnType<EffectCallback>>() | |
const calledOnce = useRef(false) | |
const renderAfterCalled = useRef(false) | |
if (calledOnce.current) { | |
renderAfterCalled.current = true | |
} | |
useEffect(() => { |