https://github.com/lamualfa/carret
🌸 The collections of lightweight utilities to make your life easier.
https://github.com/lamualfa/carret
charset generator javascript random shuffle utilities
Last synced: about 1 month ago
JSON representation
🌸 The collections of lightweight utilities to make your life easier.
- Host: GitHub
- URL: https://github.com/lamualfa/carret
- Owner: lamualfa
- License: wtfpl
- Created: 2020-12-27T05:47:00.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-08-09T18:02:14.000Z (almost 4 years ago)
- Last Synced: 2025-03-29T07:33:30.668Z (2 months ago)
- Topics: charset, generator, javascript, random, shuffle, utilities
- Language: TypeScript
- Homepage: https://github.com/lamualfa/carret
- Size: 214 KB
- Stars: 16
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/carret)
[](https://github.com/lamualfa/carret/blob/master/LICENSE)

[](https://codeclimate.com/github/lamualfa/carret/maintainability)
[](https://codecov.io/gh/lamualfa/carret)# Carret
The collections of lightweight utilities to make your life easier 🌸.
**Advantages**
- ☑️ 100% Test Coverage
- 📦 Zero Dependencies
- 🔀 Cross Browser Support
- ⚡ Blazingly Fast
- Typescript support by default
- Support CommonJS, UMD, ESM and Modern JS format.
- Use native `for` loop instead `forEach` or `map` method## Quick Start
- [Installation](#installation)
- [Usage](#usage)
- [ESM](#esm)
- [CommonJS](#commonjs)
- [CDN](#cdn)
- [Utilities](#utilities)
- [Equivalent](#equivalent)
- [Omit by Index](#omit-by-index) - Remove specific element based on the index.
- [Generate Charset](#generate-charset) - Generate a Charset with custom pattern.
- [Random Number](#random-number) - Generate Random Number with Min & Max.
- [Shuffle](#shuffle) - Shuffle String or Array.
- [Pick Random](#pick-random) - Pick random elements from String or Array.## Installation
**NPM**
```bash
npm install carret
```**Yarn**
```bash
yarn add carret
```## Usage
### ESM
```js
import { equivalent, omitByIndex } from 'carret'
```> Don't worry about unnecessary `import`. It's will removed automatically with Tree Shaking.
**Warning**
You might get an error if you using the import method above in project like Next.js. To solve the error, just import like this instead:
```js
import * as carret from 'carret'const { equivalent, omitByIndex } = carret
```### CommonJS
```js
const { equivalent, omitByIndex } = require('carret)
```### CDN
```html
const { equivalent, omitByIndex } = carret
```
## Utilities
### Equivalent
Function used to get the equivalent of a `index` in the form of the entered `charset`.
#### Formats
**`equivalent(index, charset) : string`**
- `index` - Desired index value
- `charset` - Target charset#### Example
```js
equivalent(1502, '0123456789')
// Output: 1502equivalent(2, 'abcdef')
// Output: cequivalent(25, 'abcdefghijklmnopqrstuvwxyz')
// Output: zequivalent(26, 'abcdefghijklmnopqrstuvwxyz')
// Output: ba// With .padStart
equivalent(6, '0123456789').padStart(4, '0')
// Output: 0006
```#### Use Case
- Generate Alphabet Increment ID like `aaaa`, `aaab`, `aaac`, etc.
### Omit by Index
Function used to remove specific element from a string or an array.
#### Formats
**`omitByIndex(target: string | string[], index: number) : string | string[]`**
- `target` - Target to omit
- `charset` - The index of element to be removed#### Example
```js
omitByIndex('0123456789', 5)
// Output: 012346789omitByIndex('abcdefg', 2)
// Output: abdefgomitByIndex(['a', 'b', 'c', 'd'], 3)
// Output: ['a', 'b', 'c']
```
### Generate Charset
Generate charset using given pattern.
#### Formats
**`generateCharset(pattern, ?custom) : string`**
- `pattern` - Pattern used to fill the charset
#### Example
```js
// Alphabet (lower) & Alphabet (upper)
generateCharset('aA')
// Output: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ// Numeric & Alphabet (lower)
generateCharset('0a')
// Output: 0123456789abcdefghijklmnopqrstuvwxyz// Hex (lower)
generateCharset('h')
// Output: 0123456789abcdef// Hex (lower) + Custom
generateCharset('ch', 'blabla')
// Output: blabla0123456789abcdef
```#### Available symbol pattern
- `a` - Alphabet Lower Case (`abcdefghijklmnopqrstuvwxyz`)
- `A` - Alphabet Upper Case (`ABCDEFGHIJKLMNOPQRSTUVWXYZ`)
- `0` - Numeric (`0123456789`)
- `h` - Hex Lower Case (`0123456789abcdef`)
- `H` - Hex Upper Case (`0123456789ABCDEF`)
- `u` - URL Friendly Characters (`0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-._~`)
- `s` - URL Frinedly Symbol Only (`-._~`)
- `c` - Custom characters that you pass in `custom` parameter
### Random Number
Generate random number between min(inclusive) and max(inclusive).
#### Formats
**`randomNumber(min, max) : number`**
- `min` - Minimal random value will be generated (inclusive)
- `max` - Max random value will be generated (inclusive)#### Example
```js
randomNumber(0, 5)
// Output: 4randomNumber(0, 5)
// Output: 0randomNumber(0, 5)
// Output: 3randomNumber(0, 8)
// Output: 8
```
### Shuffle
Shuffle the entered target.
#### Formats
**`shuffle(target: string | string[]) : string | string[]`**
- `target` - Target String or Array to be shuffled.
#### Example
```js
shuffle('abc')
// Output: bcashuffle('abcd')
// Output: dacbshuffle('0123456')
// Output: 2146530shuffle(['a', 'b', 'c'])
// Output: ['c', 'a', 'b']
```#### Use Case
- Shuffling the existing charset.
- Shuffling the dataset.
### Pick Random
Pick random elements from String or Array uniquely.
#### Formats
**`pickRandom(target: string | string[], total?: number) : string | string[]`**
- `target` - Target String or Array
- `total` - Total elements to be picked from `target`#### Example
```js
pickRandom('abc', 2)
// Output: capickRandom('abcd', 1)
// Output: bpickRandom('0123456', 5)
// Output: 21465pickRandom(['a', 'b', 'c'], 2)
// Output: ['c', 'b']
```