-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
666 additions
and
27 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import config from '@antfu/eslint-config' | ||
|
||
export default config({ | ||
formatters: true, | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,12 @@ | |
"packageManager": "[email protected]", | ||
"description": "Some util functions.", | ||
"license": "MIT", | ||
"homepage": "https://github.com/gui117/@gui117/util#readme", | ||
"homepage": "https://github.com/gui117/util#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/gui117/@gui117/util.git" | ||
"url": "git+https://github.com/gui117/util.git" | ||
}, | ||
"bugs": "https://github.com/gui117/@gui117/util/issues", | ||
"bugs": "https://github.com/gui117/util/issues", | ||
"keywords": [], | ||
"sideEffects": false, | ||
"exports": { | ||
|
@@ -52,6 +52,7 @@ | |
"@types/node": "^22.5.0", | ||
"bumpp": "^9.5.2", | ||
"eslint": "^9.9.0", | ||
"eslint-plugin-format": "^0.1.2", | ||
"esno": "^4.7.0", | ||
"lint-staged": "^15.2.9", | ||
"simple-git-hooks": "^2.11.1", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { chunkArray } from '.' | ||
|
||
describe('array', () => { | ||
it('chunkArray', () => { | ||
expect(chunkArray([1, 2, 3, 4, 5, 6, 7], 3)).toEqual([[1, 2, 3], [4, 5, 6], [7]]) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Split an array into chunks | ||
* @param arr Array to split | ||
* @param size Number of elements in each chunk | ||
* @returns Array of chunks | ||
* | ||
* @example | ||
* ```ts | ||
* chunkArray([1, 2, 3, 4, 5, 6, 7], 3) // [[1, 2, 3], [4, 5, 6], [7]] | ||
* ``` | ||
*/ | ||
export function chunkArray(arr: unknown[], size: number) { | ||
return Array.from( | ||
{ length: Math.ceil(arr.length / size) }, | ||
(v, i) => arr.slice(i * size, i * size + size), | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { debounce, sleep, throttle } from '.' | ||
|
||
describe('event', () => { | ||
it('sleep', async () => { | ||
const fn = vi.fn() | ||
fn() | ||
sleep(100).then(fn) | ||
expect(fn).toBeCalledTimes(1) | ||
setTimeout(() => { | ||
expect(fn).toBeCalledTimes(2) | ||
}, 200) | ||
}) | ||
|
||
it('throttle', () => { | ||
const fn = vi.fn() | ||
const throttledFn = throttle(fn, 1000) | ||
throttledFn(1) | ||
throttledFn(2) | ||
expect(fn).toBeCalledTimes(1) | ||
}) | ||
|
||
it('debounce', () => { | ||
const fn = vi.fn() | ||
const debouncedFn = debounce(fn, 1000) | ||
debouncedFn(1) | ||
debouncedFn(2) | ||
debouncedFn(3) | ||
expect(fn).toBeCalledTimes(0) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Sleep function | ||
* @param ms Milliseconds to sleep | ||
* @returns Promise | ||
* | ||
* @example | ||
* ```ts | ||
* await sleep(1000) | ||
* ``` | ||
*/ | ||
export function sleep(ms: number) { | ||
return new Promise(resolve => setTimeout(resolve, ms)) | ||
} | ||
|
||
/** | ||
* Debounce function | ||
* @param fn Function to debounce | ||
* @param delay Delay in milliseconds | ||
* @returns Debounced function | ||
* | ||
* @example | ||
* ```ts | ||
* const debouncedFn = debounce((str) => { | ||
* console.log(str) | ||
* }, 1000) | ||
* debouncedFn('Hello') | ||
* ``` | ||
*/ | ||
export function debounce(fn: (...args: any[]) => void, delay: number) { | ||
let timeoutId: ReturnType<typeof setTimeout> | null = null | ||
return function (...args: any[]) { | ||
if (timeoutId) | ||
clearTimeout(timeoutId) | ||
|
||
timeoutId = setTimeout(() => { | ||
fn(...args) | ||
timeoutId = null | ||
}, delay) | ||
} | ||
} | ||
|
||
/** | ||
* Throttle function | ||
* @param fn Function to throttle | ||
* @param delay Delay in milliseconds | ||
* @returns Throttled function | ||
* | ||
* @example | ||
* ```ts | ||
* const throttledFn = throttle((str) => { | ||
* console.log(str) | ||
* }, 1000) | ||
* throttledFn('Hello') | ||
* ``` | ||
*/ | ||
export function throttle(fn: (...args: any[]) => void, delay: number) { | ||
let timeoutId: ReturnType<typeof setTimeout> | null = null | ||
return function (...args: any[]) { | ||
if (!timeoutId) { | ||
fn(...args) | ||
timeoutId = setTimeout(() => { | ||
timeoutId = null | ||
}, delay) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
export const one = 1 | ||
export const two = 2 | ||
export * from './math' | ||
export * from './string' | ||
export * from './url' | ||
export * from './array' | ||
export * from './event' | ||
export * from './match' | ||
export * from './object' |
Oops, something went wrong.