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 type FN<I extends unknown[] = [], O = void> = (...args: I) => O | |
| // usage | |
| type IProps<T> = {callback?: FN; onChange: FN<[T]>; calculate: FN<T[], T>} |
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
| #!/bin/bash | |
| cd ./src/epub | |
| # remove old file (if it exists) | |
| rm -f ../../build/ebook.epub | |
| # create an uncompressed file with no metadata as the first entry | |
| zip -0X ../../build/ebook.epub mimetype | |
| # recursively compress all files in directory (while omitting the mimetype file) | |
| zip -rX -u ../../build/ebook.epub ./ -x "mimetype" |
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
| $('.ql-editor[contenteditable="true"]').innerHTML | |
| /** @note escape characters */ | |
| .replace(/\#/g, '\\#') | |
| .replace(/\*/g, '\\*') | |
| /** @note convert headings */ | |
| .replace(/<\/h[1-6]>/g, '\n') | |
| .replace(/<h1>/g, '# ') | |
| .replace(/<h2>/g, '## ') | |
| .replace(/<h3>/g, '### ') | |
| .replace(/<h4>/g, '#### ') |
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 useStateChangeEffect = <S>( | |
| initialState: S, | |
| effect: (arg: S) => void, | |
| runIf: (arg: S) => boolean = isTrue => !!isTrue, | |
| ) => { | |
| const [state, setState] = useState(initialState); | |
| const prevState = useRef(state); | |
| const triggerEffect = runIf(state) && state !== prevState.current; | |
| useEffect(() => void (triggerEffect && effect(state)), [effect, state, triggerEffect]); |
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 renameObjKey = <T extends Record<string, any>>( | |
| oldKey: string, | |
| newKey: string, | |
| {[oldKey]: value, ...obj}: T, | |
| ) => Object.assign({[newKey]: value}, obj); |
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
| // note: the existing item needs to appear after so that this can put stuff at the start of the array | |
| // note: if an i > length is used, it will just be added to the last place | |
| const insertAt = <T>(arr: T[], i:number, item: T) => | |
| arr.slice(0, i).concat([item]).concat(arr.slice(i)); // prettier-ignore | |
| const removeAt = <T>(arr: T[], i: number) => | |
| arr.slice(0, i).concat(arr.slice(i + 1)) // prettier-ignore | |
| const setAt = <T>(arr: T[], i: number, item: T) => | |
| arr.slice(0, i).concat([item]).concat(arr.slice(i + 1)); // prettier-ignore |
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
| items.map((result, item, i, {[i+1]: nextItem}) => [item, nextItem]); |
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'; | |
| export const useVector = <T>(initialValue: T, arity = 3) => { | |
| const [vector, setVector] = useState<T[]>([...Array(arity)].fill(initialValue)); | |
| const updateVector = (newValue: T) => setVector([newValue].concat(vector.slice(1))); | |
| return [vector, updateVector] as const; | |
| }; |
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
| /\ | |
| _\/_/ | |
| / / | |
| /\ | |
| / / |
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 spaceCase = txtWithNoSpaces => | |
| txtWithNoSpaces | |
| .split(/_|-/g) // snake_case & kebab-case | |
| .join(' ') | |
| .replace(/([A-Z])/g, ' $1') // PascalCase & camelCase | |
| .split(' ') | |
| .map(word => word.charAt(0).toUpperCase() + word.slice(1)) | |
| .join(' ') | |
| .replace(/\s+/, ' ') // dashed entries create two spaces, bump that down to one | |
| .replace(/([A-Z])\s(?=[A-Z]\b)/g, '$1') // e.g. "U U I D" => "UUID" |
NewerOlder