Skip to content

lamualfa/carret

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

17 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

NPM Version Github License CI Maintainability Coverage

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

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.

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

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 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

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 from target

Example

pickRandom('abc', 2)
// Output: ca

pickRandom('abcd', 1)
// Output: b

pickRandom('0123456', 5)
// Output: 21465

pickRandom(['a', 'b', 'c'], 2)
// Output: ['c', 'b']