Skip to content

randomString ​

Generate a random string with a length of length. The options parameter specifies the character range.

Added in v0.0.2

Usage ​

ts
import { randomString } from 'parsnip-kit'

randomString(10)
// a string including uppercase letters, lowercase letters, numbers, for example 'Dij1mzPzyW'

randomString(10, { number: false })
// a string only including letters, for example 'iYyyWSReNw'

randomString(10, { uppercase: false, lowercase: false })
// a string only including numbers, for example '2398543147'

randomString(10, { symbol: true })
// a string including uppercase letters, lowercase letters, numbers and symbols
// for example 'gI(CThCMK%'

randomString(
  10,
  {
    uppercase: false, lowercase: false, number: false,
    customized: 'αβγδεζηθικλμνξοπρστυφχψω'
  }
)
// a string only including lowercase Greek letters, for example 'γμχβωζπθοχ'

API ​

Arguments ​

ArgTypeOptionalDefaultDescription
lengthnumberfalseundefinedLength of random string
optionsRandomStringOptionstrueundefinedString generation options
options.lowercasebooleantruetrueWhether to include lowercase letters
options.uppercasebooleantruetrueWhether to include uppercase letters
options.numberbooleantruetrueWhether to include numbers
options.symbolbooleantruefalseWhether to include symbols: `'!@#$%^&*()_+-=[]{}
options.customizedstringtrueundefinedIncluded custom characters

Returns ​

Type
string

RandomStringOptions ​

The options parameter of the randomString function.

Added in v0.0.2

Source ​

typescript
export interface RandomStringOptions {
  lowercase?: boolean
  uppercase?: boolean
  number?: boolean
  symbol?: boolean
  customized?: string
}