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 pick<OriginalObj extends Record<string, unknown>, Keys extends Partial<keyof OriginalObj>>( | |
obj: Partial<OriginalObj>, | |
keys: Keys[] | |
) { | |
type ReturnObj = Record<Keys, OriginalObj[Keys]>; | |
return keys | |
.map(k => (k in obj ? { [k]: obj[k] } : {})) | |
.reduce((res, o) => Object.assign(res, o), {}) as ReturnObj; | |
} |
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 { type SetStateAction, useState } from 'react' | |
// import deepMerge from './deepmerge' | |
// this would be necessary if deep merge is used | |
// type Subset<K> = { | |
// [attr in keyof K]?: K[attr] extends object | |
// ? Subset<K[attr]> | |
// : K[attr] extends object | null | |
// ? Subset<K[attr]> | null | |
// : K[attr] extends object | null | undefined |
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 { useState } from 'react' | |
function useCycleState<T>(values: readonly T[]) { | |
const [currentIndex, setCurrentIndex] = useState(0) | |
const currentValue = values[currentIndex] as T | |
const nextAndPrev = (isPrev: boolean) => { | |
const valPlus = (currentIndex + 1) % values.length | |
let valMinus = (currentIndex - 1) % values.length |
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 { useState, useEffect } from 'react'; | |
const useFetchData = <T extends Record<string, unknown>>( | |
url: string, | |
options?: RequestInit, | |
mockData?: Promise<T>, | |
timeout = 600 | |
) => { | |
const [data, setData] = useState<T | null>(null); | |
const [error, setError] = useState<Error | null>(null); |
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 React, { ForwardedRef, forwardRef, MouseEvent } from 'react'; | |
type ClickableListProps<T> = { | |
items: T[]; | |
onSelect: ( | |
item: T, | |
event: MouseEvent<HTMLButtonElement, globalThis.MouseEvent>, | |
) => void; | |
}; |
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 flatten from "flat"; | |
const OriginalObj = { | |
key1: 'key1Value', | |
key2: { | |
key2a: 'key2a value', | |
key2b: 3231 | |
} | |
}; |
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
<div class="ajax"> | |
<p>This is an external html file, fetched with ajax</p> | |
<ol> | |
<li>Info</li> | |
<li>Info</li> | |
<li>Info</li> | |
<li>Info</li> | |
</ol> | |
</div> |