A collection of utility functions for JavaScript/TypeScript.
npm install @supuwoerc/toolkit
or using pnpm:
pnpm add @supuwoerc/toolkit
or using yarn:
yarn add @supuwoerc/toolkit
import { array2Tree, pick, shuffle, sleep, tree2Array, withTimeout } from '@supuwoerc/toolkit'
// Convert flat array to tree
const flat = [
{ id: 1, parentId: null },
{ id: 2, parentId: 1 },
{ id: 3, parentId: 1 },
]
const tree = array2Tree(flat, { idKey: 'id', parentKey: 'parentId' })
console.log(tree)
// Shuffle an array
const arr = [1, 2, 3, 4, 5]
const shuffled = shuffle(arr)
console.log(shuffled)
// Pick properties from an object
const obj = { a: 1, b: 2, c: 3 }
const picked = pick(obj, ['a', 'c']) // { a: 1, c: 3 }
// Sleep for 1 second
await sleep(1000)
// Timeout a promise
try {
const result = await withTimeout(fetch('https://example.com'), 5000)
} catch (err) {
console.log('Timeout or error', err)
}
const { shuffle } = require('@supuwoerc/toolkit')
Full API documentation is available at https://supuwoerc.github.io/toolkit/.
You can also generate documentation locally:
pnpm run doc
Then open docs/index.html in your browser.
git clone https://github.com/supuwoerc/toolkit.git
pnpm install
pnpm run build
pnpm test # run tests once
pnpm test:watch # run tests in watch mode
pnpm test:ui # open Vitest UI
pnpm test:coverage # generate coverage report
pnpm run lint
Releases are automated via release-it. To create a new release:
pnpm run release
This will run tests, build, update changelog, tag, and publish to npm.
Contributions are welcome! Please open an issue or submit a pull request.