1.0.4 • Published 3 years ago
carret v1.0.4
Carret
The Collection of charset utilities or helpers with zero dependencies.
Advantages
- 100% Test Coverage
- Zero Dependencies
- Cross Browser Support
- Blazing Fast
- Use native
for
loop insteadforEach
ormap
method
Quick Start
- Installation
- Usage
- Utilities
- Equivalent
- Generate Charset - Generate a Charset with custom pattern
- Random Number - Generate Random Number with Min & Max
- Shuffle - for String & Array
Installation
npm install carret
# Or
yarn add carret
Usage
Import All
If you want to import a whole of the module, you can import like this:
const { shuffle } = require('carret');
Import Specific
or if you want to import only specific function to reduce the bundle size, please import like this:
const shuffle = require('carret/shuffle')
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 valuecharset
- 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.
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, 8);
// Output: 8
Shuffle
Shuffle the entered charset.
Formats
shuffle(charset) : string | Array<string>
charset
- Target charset
Example
shuffle('abc');
// Output: bca
shuffle('abcd');
// Output: dacb
shuffle('0123456');
// Output: 214653
shuffle(['a', 'b', 'c']);
// Output: ['c', 'a', 'b']
Use Case
- Shuffling the existing charset.
- Shuffling the dataset.