ExRandomString v1.0.1 ExRandomString View Source

Library for generating random strings. Based on NodeJS library randomstring.

Installation

The package can be installed by adding ex_random_string to your list of dependencies in mix.exs:

def deps do
  [
    {:ex_random_string, "~> 1.0.1"}
  ]
end

Usage

ExRandomString.generate()
# "XwPp9xazJ0ku5CZnlmgAx2Dld8SHkAeT"

ExRandomString.generate(7);
# "xqm5wXX"

ExRandomString.generate(length: 12, charset: :alphabetic)
# "AqoTIzKurxJi"

ExRandomString.generate(charset: "abc");
# "accbaabbbbcccbccccaacacbbcbbcbbc"

Link to this section Summary

Functions

Generates a random string that is 32 characters long and alphanumeric

When provided an integer, it generates a random string with the provided integer as length. String returned will be alphanumeric

Link to this section Functions

Generates a random string that is 32 characters long and alphanumeric.

When provided an integer, it generates a random string with the provided integer as length. String returned will be alphanumeric.

When provided a Keyword list, it generates a random string with properties based on the provided options.

Options

Listed below are the supported options. Each of the options are optional.

  • length — the length of the random string. (default: 32)
  • charset — define the character set for the string. (default: :alphanumeric)

    • :alphanumeric — [0-9a-zA-Z]
    • :alphabetic — [a-zA-Z]
    • :numeric — [0-9]
    • :hex — [0-9a-f]
    • any string — will generate based on the string’s set of characters
  • case — define whether the output should be lowercase / uppercase only. (default: nil)

    • :lower — forces all characters to use lower case
    • :upper — forces all characters to use upper case
    • :mix — randomizes the case of each character

Examples

ExRandomString.generate(7)
# "xqm5wXX"

ExRandomString.generate(length: 12, charset: :alphabetic)
# "AqoTIzKurxJi"

ExRandomString.generate(charset: "ABC", case: :lower)
# "abaccabaacbaabcccabacbacccacbbbb"