@algorithm.ts/shuffle

中文文档
4.0.4发布于 2026/03/23MITTypeScriptESM + CJS零依赖近 30 天下载 1.2万

Knuth shuffle

原始使用文档
文档目录3 节
  1. Install
  2. Usage
  3. Related

@algorithm.ts/shuffle


A typescript implementation of the Knuth-Shuffle algorithm.

Knuth-Shuffle is a shuffle algorithm, which can complete the shuffle in $O(N)$ time complexity on the basis of only using a constant level of extra space.

If you are curious about this algorithm, you can visit here for more details.

Install

  • npm

    npm install --save @algorithm.ts/shuffle
    
  • pnpm

    pnpm add @algorithm.ts/shuffle
    

Usage

  • Shuffle nums.

    import { knuthShuffle } from '@algorithm.ts/shuffle'
    
    knuthShuffle([1, 2, 3, 4, 5])
    
  • Shuffle complex data nodes.

    import { knuthShuffle } from '@algorithm.ts/shuffle'
    
    interface Node {
      name: string
      email: string
      age: number
    }
    
    const nodes: Node[] = [
      { name: 'alice', email: '[email protected]', age: 40 },
      /*... omit ...*/
      { name: 'bob', email: '[email protected]', age: 40 },
    ]
    knuthShuffle(nodes)
    
  • Shuffle the elements which indexes in the customized contiguous range.

    import { knuthShuffle } from '@algorithm.ts/shuffle'
    
    // shuffle { a[2], a[3], a[4], a[5], a[6] }
    knuthShuffle([1, 2, 3, 4, 5, 6, 7, 8, 9], 2, 7)
    

版本与发布记录

@algorithm.ts/shuffle 共发布 17 个版本,首次发布于 2022/09/12;过去 12 个月发布了 1 个版本。

版本发布日期距今
4.0.4latest2026/03/237 个月前
4.0.32024/10/062 年前
4.0.22024/09/142 年前
4.0.12024/05/292 年前
4.0.02024/03/302 年前
3.1.12023/06/103 年前

关于 @algorithm.ts/shuffle 的常见问题

@algorithm.ts/shuffle 是什么?

Knuth shuffle

如何安装 @algorithm.ts/shuffle?

在项目目录运行 npm install @algorithm.ts/shuffle,或使用 pnpm add / yarn add / bun add。

@algorithm.ts/shuffle 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@algorithm.ts/shuffle 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@algorithm.ts/shuffle 可以商用吗?

@algorithm.ts/shuffle 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@algorithm.ts/shuffle 的下载量有多少?

根据 npm 官方下载统计,@algorithm.ts/shuffle 在 2026/10/06 – 2026/11/04 这 30 天内被下载 12,277 次,日均约 409 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@algorithm.ts/shuffle 还在维护吗?

最新版本 4.0.4 发布于 2026/03/23(7 个月前);至今共发布 17 个版本,其中过去 12 个月发布了 1 个;首次发布于 2022/09/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。