{"id":22903264,"url":"https://github.com/philihp/fast-shuffle","last_synced_at":"2025-05-08T17:00:02.838Z","repository":{"id":29554249,"uuid":"122031700","full_name":"philihp/fast-shuffle","owner":"philihp","description":"A fast, pure, side-effect-free, and deterministic implementation of Fisher-Yates Shuffle","archived":false,"fork":false,"pushed_at":"2025-05-04T15:02:17.000Z","size":716,"stargazers_count":23,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-08T16:59:39.042Z","etag":null,"topics":["array-shuffle","deterministic","random","redux-reducers","shuffle"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/fast-shuffle","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/philihp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["philihp"]}},"created_at":"2018-02-19T07:40:44.000Z","updated_at":"2025-05-04T15:02:19.000Z","dependencies_parsed_at":"2023-02-18T16:32:09.600Z","dependency_job_id":"bbaaaa6e-4802-431f-9c35-37ec770502f1","html_url":"https://github.com/philihp/fast-shuffle","commit_stats":{"total_commits":405,"total_committers":4,"mean_commits":101.25,"dds":0.2592592592592593,"last_synced_commit":"43868768990b6f7bca1d053f8c3a391eb8ea58ce"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philihp%2Ffast-shuffle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philihp%2Ffast-shuffle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philihp%2Ffast-shuffle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philihp%2Ffast-shuffle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/philihp","download_url":"https://codeload.github.com/philihp/fast-shuffle/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253112071,"owners_count":21856070,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array-shuffle","deterministic","random","redux-reducers","shuffle"],"created_at":"2024-12-14T02:35:25.997Z","updated_at":"2025-05-08T17:00:02.794Z","avatar_url":"https://github.com/philihp.png","language":"TypeScript","funding_links":["https://github.com/sponsors/philihp"],"categories":[],"sub_categories":[],"readme":"# Fast Shuffle\n\n[![Version](https://badge.fury.io/js/fast-shuffle.svg)](https://www.npmjs.com/package/fast-shuffle)\n![Tests](https://github.com/philihp/fast-shuffle/workflows/tests/badge.svg)\n[![Coverage Status](https://coveralls.io/repos/github/philihp/fast-shuffle/badge.svg?branch=main)](https://coveralls.io/github/philihp/fast-shuffle?branch=main)\n![Downloads](https://img.shields.io/npm/dt/fast-shuffle)\n![License](https://img.shields.io/npm/l/fast-shuffle)\n\nA fast, side-effect-free, and O(n) array shuffle that's safe for functional programming and use within Redux reducers.\n\n## Usage\n\n```\nnpm install --save fast-shuffle\n```\n\n```js\nimport { shuffle } from 'fast-shuffle'\n\nconst suits = ['♣', '♦', '♥', '♠']\nconst faces = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']\nconst sortedDeck = suits.map((suit) =\u003e faces.map((face) =\u003e face + suit)).flat()\n// [ '2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', ...\n\nconst shuffledDeck = shuffle(sortedDeck)\n// [ '3♥', '3♦', 'K♥', '6♦', 'J♣', '5♠', 'A♠', ...\n```\n\nThe named `shuffle` export seen above uses `Math.random` for entropy. This is the easiest way to use the library, but it may be useful to create a purely functional shuffler which takes either a random seed which is used in a [PCG](https://www.pcg-random.org/) for entropy, or a function ([as seen here](https://github.com/philihp/fast-shuffle/blob/c36f6cfb27312590301446721b5ba0539baab591/src/__tests__/index.test.ts#L62-L73)).\n\n```js\nimport { createShuffle } from 'fast-shuffle' // note the change\n\nconst letters = ['a', 'b', 'c', 'd', 'e']\nconst shuffleRed = createShuffle(12345)\nshuffleRed(letters) // [ 'a', 'b', 'c', 'd', 'e' ]\nshuffleRed(letters) // [ 'a', 'd', 'b', 'e', 'c' ]\nshuffleRed(letters) // [ 'c', 'a', 'e', 'b', 'd' ]\nshuffleRed(letters) // [ 'b', 'c', 'e', 'a', 'd' ]\n\nconst shuffleBlue = createShuffle(12345)\nshuffleBlue(letters) // [ 'a', 'b', 'c', 'd', 'e' ]\nshuffleBlue(letters) // [ 'a', 'd', 'b', 'e', 'c' ]\nshuffleBlue(letters) // [ 'c', 'a', 'e', 'b', 'd' ]\nshuffleBlue(letters) // [ 'b', 'c', 'e', 'a', 'd' ]\n```\n\nThe parameters are also curried, so it can be used in [pipelines](https://github.com/tc39/proposal-pipeline-operator).\n\n```js\nimport { createShuffle } from 'fast-shuffle'\n\nconst randomCapitalLetter =\n  ['a', 'b', 'c', 'd', 'e', 'f']   // :: () -\u003e [a]\n  |\u003e createShuffle(Math.random),       // :: [a] -\u003e [a]\n  |\u003e _ =\u003e _[0]                     // :: [a] -\u003e a\n  |\u003e _ =\u003e _.toUpperCase()          // :: a -\u003e a\n```\n\nIf you give it an array of your source array and a random seed, you'll get a shuffled array and a new random seed back. This is a pure function and the original array is not mutated, so you can use it in your Redux reducers. The returned, shuffled array is a shallow copy, so if you use this in React, [you will often avoid unnecessary rerenders](https://redux.js.org/faq/performance).\n\n```js\nimport { SHUFFLE_DECK } from './actions'\nimport { createShuffle } from 'fast-shuffle'\n\nconst initialState = {\n  ...\n  deck: ['♣', '♦', '♥', '♠'],\n  randomizer: Date.now()\n}\n\nconst dealerApp = (state = initialState, action) =\u003e {\n  switch (action.type) {\n    ...\n    case SHUFFLE_DECK:\n      const [ deck, randomizer ] = createShuffle([state.deck, state.randomizer])\n      return {\n        ...state,\n        deck,\n        randomizer,\n      }\n    ...\n    default:\n      return state\n  }\n}\n```\n\n## Why not use existing libraries?\n\n1. It doesn't mutate your source array, so it's safe for Redux reducers.\n\n2. The parameters are curried in [the correct order](https://www.youtube.com/watch?v=m3svKOdZijA), so you can use it within `|\u003e` or Ramda pipes.\n\n3. You can make it a deterministic pure function, useful for shuffling in tests.\n\n4. It's stupid-fast and scales to large arrays without breaking a sweat.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilihp%2Ffast-shuffle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphilihp%2Ffast-shuffle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilihp%2Ffast-shuffle/lists"}