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 { useEffect, useState } from 'react'; | |
type Callback = (element?: string | number) => void | |
type TargetKey = string | number | Array<string|number> | |
const isNumber = (value: unknown): value is number => typeof value === 'number'; | |
const isString = (value: unknown): value is string => typeof value === 'string'; | |
const isNumberArray = (object: unknown): object is number[] => | |
Array.isArray(object) && object.every(e => isNumber(e)); |
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 { useState, useCallback } from 'react' | |
export type Counter = { | |
count: number | |
increment: (value?: number | undefined) => void | |
decrement: (value?: number | undefined) => void | |
reset: (value?: number | undefined) => void | |
} | |
export default (initialValue: number = 0): Counter => { |
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 default <T>(value: T | null | undefined): value is T => !!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
export const hasKey = <T>(object: T, key: keyof any): key is keyof T => key in object |
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
type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = | |
Pick<T, Exclude<keyof T, Keys>> | |
& { | |
[K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>> | |
}[Keys] |
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
type RequireOnlyOne<T, Keys extends keyof T = keyof T> = | |
Pick<T, Exclude<keyof T, Keys>> | |
& { | |
[K in Keys]-?: | |
Required<Pick<T, K>> | |
& Partial<Record<Exclude<Keys, K>, undefined>> | |
}[Keys] |
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
/** | |
* Creates an array of elements split into groups the length of size | |
* | |
* @template T | |
* @param {T[]} input | |
* @param {number} size | |
* @returns {T[][]} | |
*/ | |
export const chunk = <T>(input: T[], size: number): T[][] => { | |
return input.reduce((array, item, index) => { |
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
/** | |
* Returns the values from array that are not present in the other arrays | |
* | |
* @param {unknown[]} first | |
* @param {unknown[]} second | |
* @returns {unknown[]} | |
*/ | |
export const difference = (first: unknown[], second: unknown[]): unknown[] => { | |
return first.filter(item => !second.includes(item)); | |
}; |
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
/** | |
* Returns the values from array that are present in each of the other arrays | |
* | |
* @param {...unknown[]} arrays | |
* @returns {unknown[]} | |
*/ | |
export const intersection = (...arrays: unknown[]): unknown[] => { | |
return arrays.reduce((a, b) => a.filter((c: any) => b.includes(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
declare global { | |
interface Array<T> { | |
/** | |
* Creates an tuple of grouped elements limited by the length of the first array. | |
* @template U | |
* @param {U} list | |
*/ | |
zip<U>(list: U[]): [T, U][] | |
} | |
} |
OlderNewer