Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/igorskyflyer/npm-scramble

๐Ÿƒ Scrambles (rearranges randomly) Strings and Arrays. ๐ŸŽ‹
https://github.com/igorskyflyer/npm-scramble

algorithm array back-end fisher-yates-shuffle igorskyflyer javascript js node random scramble shuffle string ts typescript

Last synced: 8 days ago
JSON representation

๐Ÿƒ Scrambles (rearranges randomly) Strings and Arrays. ๐ŸŽ‹

Awesome Lists containing this project

README

        

ScRaMbLe



๐Ÿƒ Scrambles (rearranges randomly) Strings and Arrays. ๐ŸŽ‹








๐Ÿ’– Support further development


I work hard for every project, including this one


and your support means a lot to me!




Consider buying me a coffee. โ˜•


Thank you for supporting my efforts! ๐Ÿ™๐Ÿ˜Š





Donate to igorskyflyer




@igorskyflyer










## ๐Ÿ“ƒ Table of contents

- [Features](#-features)
- [Usage](#-usage)
- [API](#-api)
- [scrambleString()](#scramblestringinput-string-options-istringoptions-string)
- [scrambleArray()](#scramblearrayarraytypeinput-arraytype-arraytype)
- [Examples](#-examples)
- [Changelog](#-changelog)
- [License](#-license)
- [Related](#-related)
- [Author](#-author)

---

## ๐Ÿค– Features

- ๐ŸซŽ reorders elements randomly
- ๐Ÿ” works for strings and arrays
- ๐Ÿฒ uses `Fisher-Yates` algorithm


## ๐Ÿ•ต๐Ÿผ Usage

Install it by executing:

```shell
npm i '@igor.dvlpr/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`:

```ts
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(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`
```ts
import { scrambleString, scrambleArray } from '@igor.dvlpr/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](https://github.com/igorskyflyer/npm-scramble/blob/main/CHANGELOG.md).

---

## ๐Ÿชช License

Licensed under the MIT license which is available here, [MIT license](https://github.com/igorskyflyer/npm-scramble/blob/main/LICENSE).

---

## ๐Ÿงฌ Related

[@igor.dvlpr/uarray](https://www.npmjs.com/package/@igor.dvlpr/uarray)

> _๐ŸŽ‰ Provides UArray, an Array type that supports negative indices/indexes, just wrap your regular JavaScript array with UArray() and you are all set! ๐Ÿ™Œ_


[@igor.dvlpr/magic-string](https://www.npmjs.com/package/@igor.dvlpr/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. ๐Ÿฆฅ_


[@igor.dvlpr/strip-html](https://www.npmjs.com/package/@igor.dvlpr/strip-html)

> _๐Ÿฅž Removes HTML code from the given string. Can even extract text-only from the given an HTML string. โœจ_


[@igor.dvlpr/extendable-string](https://www.npmjs.com/package/@igor.dvlpr/extendable-string)

> _๐Ÿฆ€ ExtendableString allows you to create strings on steroids that have custom transformations applied to them, unlike common, plain strings. ๐Ÿช€_


[@igor.dvlpr/duoscribi](https://www.npmjs.com/package/@igor.dvlpr/duoscribi)

> _โœ’ DรบรถScrรญbรฎ allows you to convert letters with diacritics to regular letters. ๐Ÿค“_

---

## ๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป Author
Created by **Igor Dimitrijeviฤ‡** ([*@igorskyflyer*](https://github.com/igorskyflyer/)).