Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@igorskyflyer/scramble

igorskyflyer103MIT1.1.0TypeScript support: included

🃏 Scrambles (rearranges randomly) Strings and Arrays. 🎋

scramble, shuffle, string, array, text, word, obfuscate, randomize, utility, typescript, fisher-yates, trimWhitespace, pure-function, generic, open-source, cli-ready, zero-dependency, test-data, mocking, UX, puzzle, creative-tools, data-anonymization, igor dimitrijević, igorskyflyer

readme

Icon of ScRaMbLe

ScRaMbLe


🃏 Scrambles (rearranges randomly) Strings and Arrays. 🎋



📃 Table of Contents



🤖 Features

  • 🎯 Scrambles words while keeping first and last letters intact for readability
  • 🧩 Shuffles array elements with a proven Fisher Yates algorithm
  • ✂️ Optional whitespace trimming before scrambling text
  • 🛡️ Validates inputs and throws clear, descriptive errors
  • ⚡ Zero dependencies for lightweight, fast execution
  • 🔄 Pure functions that don’t mutate original data
  • 🧠 Works with any array type thanks to TypeScript generics
  • 📝 Well-documented with clear parameter and return descriptions
  • 🧪 Perfect for games, puzzles, mock data, and creative text effects


🕵🏼 Usage

Install it by executing any of the following, depending on your preferred package manager:

pnpm add @igorskyflyer/scramble
yarn add @igorskyflyer/scramble
npm i @igorskyflyer/scramble


🤹🏼 API

scrambleString(input: string, options?: IStringOptions): string

Scrambles the characters of each word in a given string.

input - The string to be scrambled.


ℹ️ NOTE

Since Fisher-Yates algorithm is used for scrambling, a length of > 3 is needed for a word to be able to get scrambled.


options - Options for scrambling, optional.


options are defined via an interface IStringOptions:

interface IStringOptions {
  trimWhitespace?: boolean
}

trimWhitespace, if true all whitespace is removed from the input string prior to scrambling its contents.


Returns the scrambled string.

Will throw an error if the input is not a string.


scrambleArray<ArrayType>(input: ArrayType[]): ArrayType[]

Scrambles the elements of an array.

input - The array to be scrambled.


Returns the scrambled array.

Will throw an error if the input is not an array.



🗒️ Examples

example.mts

import { scrambleString, scrambleArray } from '@igorskyflyer/scramble'

// these are just sample outputs
// since each invocation reorders
// elements randomly

console.log(scrambleString(
  'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
)) // returns 'Leorm isupm dloor sit aetm, ccotnsueetr asdincipig eitl.'

console.log(scrambleString(
  'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', { trimWhitespace: true }
)) // returns 'Lpetssgerodcsctcmuaunelrteiisoomirlait,toindmipe.'

console.log(scrambleArray([1, 2, 3, 4, 5, 6, 7, 8])) // [5, 7, 1, 4, 2, 8, 3, 6]


📝 Changelog

📑 The changelog is available here, CHANGELOG.md.



🪪 License

Licensed under the MIT license which is available here, MIT license.



💖 Support

I work hard for every project, including this one and your support means a lot to me!
Consider buying me a coffee. ☕

Donate to igorskyflyer

Thank you for supporting my efforts! 🙏😊


@igorskyflyer/uarray

🎉 Provides UArray, an Array type that supports negative indices/indexes, just wrap your regular JavaScript array with UArray() and you are all set! 🙌


@igorskyflyer/magic-string

🧵 An expressive and chainable library for advanced string manipulations. Supports appending, prepending, trimming, quoting, and path formatting with customizable whitespace handling. Makes advanced String manipulations a piece of cake. 🦥


@igorskyflyer/strip-html

🥞 Removes HTML code from the given string. Can even extract text-only from the given an HTML string. ✨


@igorskyflyer/extendable-string

🦀 ExtendableString allows you to create strings on steroids that have custom transformations applied to them, unlike common, plain strings. 🪀


@igorskyflyer/duoscribi

✒ DúöScríbî allows you to convert letters with diacritics to regular letters. 🤓




👨🏻‍💻 Author

Created by Igor Dimitrijević (@igorskyflyer).

changelog

📒 Changelog

of @igorskyflyer/scramble


v1.1.0 (29-Aug-2025)

  • 💻 dev: upgrade Node to >= v22
  • 💻 dev: upgrade dependencies


v1.0.1 (04-Aug-2024)

  • 💻 dev: add more tests
  • 📜 docs: fix documentation


v1.0.0 (04-Aug-2024)

  • 🚀 launch: initial release 🎉