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
- 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.
NPM
npm install carret
Yarn
yarn add carret
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:
import * as carret from 'carret'
const { equivalent, omitByIndex } = carret
const { equivalent, omitByIndex } = require('carret)
<script src="https://cdn.jsdelivr.net/npm/carret@latest"></script>
<script>
const { equivalent, omitByIndex } = carret
</script>
Function used to get the equivalent of a index
in the form of the entered charset
.
equivalent(index, charset) : string
index
- Desired index valuecharset
- Target charset
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
- Generate Alphabet Increment ID like
aaaa
,aaab
,aaac
, etc.
Function used to remove specific element from a string or an array.
omitByIndex(target: string | string[], index: number) : string | string[]
target
- Target to omitcharset
- The index of element to be removed
omitByIndex('0123456789', 5)
// Output: 012346789
omitByIndex('abcdefg', 2)
// Output: abdefg
omitByIndex(['a', 'b', 'c', 'd'], 3)
// Output: ['a', 'b', 'c']
Generate charset using given pattern.
generateCharset(pattern, ?custom) : string
pattern
- Pattern used to fill the charset
// 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
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
Generate random number between min(inclusive) and max(inclusive).
randomNumber(min, max) : number
min
- Minimal random value will be generated (inclusive)max
- Max random value will be generated (inclusive)
randomNumber(0, 5)
// Output: 4
randomNumber(0, 5)
// Output: 0
randomNumber(0, 5)
// Output: 3
randomNumber(0, 8)
// Output: 8
Shuffle the entered target.
shuffle(target: string | string[]) : string | string[]
target
- Target String or Array to be shuffled.
shuffle('abc')
// Output: bca
shuffle('abcd')
// Output: dacb
shuffle('0123456')
// Output: 2146530
shuffle(['a', 'b', 'c'])
// Output: ['c', 'a', 'b']
- Shuffling the existing charset.
- Shuffling the dataset.
Pick random elements from String or Array uniquely.
pickRandom(target: string | string[], total?: number) : string | string[]
target
- Target String or Arraytotal
- Total elements to be picked fromtarget
pickRandom('abc', 2)
// Output: ca
pickRandom('abcd', 1)
// Output: b
pickRandom('0123456', 5)
// Output: 21465
pickRandom(['a', 'b', 'c'], 2)
// Output: ['c', 'b']