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 insteadforEach
ormap
method
Quick Start
- Installation
- Usage
-
Utilities
- Equivalent
- Omit by Index - Remove specific element based on the index.
- Generate Charset - Generate a Charset with custom pattern.
- Random Number - Generate Random Number with Min & Max.
- Shuffle - Shuffle String or Array.
- Pick Random - Pick random elements from String or Array.
Installation
NPM
npm install carret
Yarn
yarn add carret
Usage
ESM
import { equivalent, omitByIndex } from 'carret'
Don't worry about unnecessary
import
. It's will removed automatically with Tree Shaking.
CommonJS
const { equivalent, omitByIndex } = require('carret)
CDN
<script src="https://cdn.jsdelivr.net/npm/carret@latest"></script>
<script>
const { equivalent, omitByIndex } = carret
</script>
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
equivalent(1502, '0123456789')
// Output: 1502
equivalent(2, 'abcdef')
// Output: c
equivalent(25, 'abcdefghijklmnopqrstuvwxyz')
// Output: z
equivalent(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
omitByIndex('0123456789', 5)
// Output: 012346789
omitByIndex('abcdefg', 2)
// Output: abdefg
omitByIndex(['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
// 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 incustom
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
randomNumber(0, 5)
// Output: 4
randomNumber(0, 5)
// Output: 0
randomNumber(0, 5)
// Output: 3
randomNumber(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
shuffle('abc')
// Output: bca
shuffle('abcd')
// Output: dacb
shuffle('0123456')
// Output: 2146530
shuffle(['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 fromtarget
Example
pickRandom('abc', 2)
// Output: ca
pickRandom('abcd', 1)
// Output: b
pickRandom('0123456', 5)
// Output: 21465
pickRandom(['a', 'b', 'c'], 2)
// Output: ['c', 'b']