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 ParenthesisPosition = [number, number, ParenthesisPosition[]]; | |
export const parenthesisParse = (str: string) => { | |
let cursor = -1; | |
const result: ParenthesisPosition[] = []; | |
const stack: ParenthesisPosition[] = []; | |
let error = ''; | |
while (!error) { | |
if (!stack.length) { | |
const startIndex = str.indexOf('(', cursor + 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
const fileSystemEntryParse = async (data: FileSystemEntry|DataTransferItemList) => { | |
const entries = data instanceof DataTransferItemList ? [...data].map(dataTransferItem => dataTransferItem.webkitGetAsEntry()) : [data]; | |
const stack = [...entries]; | |
let target: FileSystemEntry; | |
const result: File[] = []; | |
while (target = stack.shift() as FileSystemEntry) { | |
if (target.isDirectory) { | |
const reader = (target as FileSystemDirectoryEntry).createReader(); |
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 createPaginationIndex = (currentPage: number, total: number, limit = 10, listLength = 10) => { | |
const totalPages = Math.ceil(total / limit); | |
const startSetIndex = Math.floor((currentPage - 1) / listLength); | |
const startList = listLength * startSetIndex + 1; | |
const length = startList + listLength - 1 > totalPages ? | |
listLength - (startList + listLength - 1 - totalPages) : | |
listLength; | |
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 { useEffect, RefObject } from 'react'; | |
const useOutsideClick = <T extends HTMLElement>(f: (ev: MouseEvent) => void, ref: RefObject<T>) => { | |
useEffect(() => { | |
const upHandler = (ev: MouseEvent) => { | |
ev.stopPropagation(); | |
document.removeEventListener('click', upHandler, true); | |
f(ev); | |
}; | |
const downHandler = (ev: PointerEvent) => { |
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 createRangePagination = (page: number, limit: number, total: number, range: number) => { | |
const size = range * 2 + 1; | |
const maxPage = Math.ceil(total / limit); | |
const start = range >= page ? 1 | |
: maxPage - range < page ? maxPage - size + 1 | |
: page - range; | |
const end = size > page + range ? size | |
: maxPage < page + range ? maxPage | |
: page + range; | |
return Array.from({length: end - start + 1}, (_, i) => i + start); |
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 args = [ | |
[1,2,3], | |
[1], | |
['a', 'b'], | |
['c', 'd', 'e'], | |
[5], | |
]; | |
const r = [[]] as any; | |
let i = 0; |
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 * backTracking(...args) { | |
const r = [[]]; | |
while (r.length) { | |
const t = r.shift(); | |
if (t.length === args.length) yield t; | |
else r.unshift(...args[t.length].map(i => [...t, i])); | |
} | |
} | |
const iter = backTracking([true, false], [1, 2, 3], [3, 4, 5, 6, 7], ['a', 'b'], ['c']); | |
[...iter]; |
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
class TPromise<T> extends Promise<T> { | |
static TimeoutError = class extends Error {}; | |
constructor( | |
f: ( | |
resolve: (value: T | PromiseLike<T>) => void, | |
reject: (reason?: any) => void | |
) => void, | |
timeout: number = Infinity | |
) { |
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 loadImage = ( | |
src: string, | |
{ | |
save = true, | |
timeout = Infinity, | |
crossOrigin, | |
}: Partial<{ | |
save: boolean; | |
timeout: number; | |
crossOrigin: string; |
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
class TPromise<T> extends Promise<T> { | |
static TimeoutError = class extends Error {}; | |
constructor( | |
f: ( | |
resolve: (value: T | PromiseLike<T>) => void, | |
reject: (reason?: any) => void | |
) => void, | |
timeout: number = Infinity | |
) { |