Skip to content

Commit

Permalink
feat: add functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
gui117 committed Aug 23, 2024
1 parent 6f80dfe commit 214e52e
Show file tree
Hide file tree
Showing 18 changed files with 666 additions and 27 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ Some util functions.

[npm-version-src]: https://img.shields.io/npm/v/@gui117/util
[npm-version-href]: https://npmjs.com/package/@gui117/util

[npm-downloads-src]: https://img.shields.io/npm/dm/@gui117/util
[npm-downloads-href]: https://npmjs.com/package/@gui117/util

[bundle-src]: https://img.shields.io/bundlephobia/minzip/@gui117/util
[bundle-href]: https://bundlephobia.com/result?p=@gui117/util

[license-src]: https://img.shields.io/github/license/gui117/util.svg
[license-href]: https://github.com/gui117/@gui117/util/blob/main/LICENSE
10 changes: 0 additions & 10 deletions eslint.config.js

This file was deleted.

5 changes: 5 additions & 0 deletions eslint.config.mjs
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,
})
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
75 changes: 73 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/array/index.test.ts
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]])
})
})
17 changes: 17 additions & 0 deletions src/array/index.ts
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),
)
}
31 changes: 31 additions & 0 deletions src/event/index.test.ts
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)
})
})
66 changes: 66 additions & 0 deletions src/event/index.ts
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)
}
}
}
9 changes: 7 additions & 2 deletions src/index.ts
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'
Loading

0 comments on commit 214e52e

Please sign in to comment.