@supuwoerc/toolkit
    Preparing search index...

    @supuwoerc/toolkit



    README in English 简体中文文档

    @supuwoerc/toolkit

    A collection of utility functions for JavaScript/TypeScript.

    • TypeScript support: Fully typed with generics.
    • Tested: Comprehensive test suite with Vitest.
    • Documented: Auto-generated API documentation via TypeDoc.
    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.

    • Node.js >= 18
    • pnpm (recommended) or npm
    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.

    1. Fork the repository.
    2. Create a feature branch.
    3. Make your changes.
    4. Add tests for your changes.
    5. Ensure linting passes.
    6. Submit a pull request.

    MIT © Idris. See LICENSE for details.