Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/caioceccon/random_string_generator
A Elixir module to generate a random string based on a given string pattern.
https://github.com/caioceccon/random_string_generator
Last synced: 2 months ago
JSON representation
A Elixir module to generate a random string based on a given string pattern.
- Host: GitHub
- URL: https://github.com/caioceccon/random_string_generator
- Owner: caioceccon
- License: gpl-3.0
- Created: 2017-08-30T22:07:08.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-04T09:13:40.000Z (over 5 years ago)
- Last Synced: 2024-10-07T09:36:38.906Z (3 months ago)
- Language: Elixir
- Size: 30.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - A module to generate a random string based on a given string pattern. (Text and Numbers)
- fucking-awesome-elixir - RandomStringGenerator - A module to generate a random string based on a given string pattern. (Text and Numbers)
- awesome-elixir - RandomStringGenerator - A module to generate a random string based on a given string pattern. (Text and Numbers)
README
# RandomStringGenerator
[![Build Status](https://travis-ci.org/caioceccon/random_string_generator.svg?branch=master)](https://travis-ci.org/caioceccon/random_string_generator) [![codecov](https://codecov.io/gh/caioceccon/random_string_generator/branch/master/graph/badge.svg)](https://codecov.io/gh/caioceccon/random_string_generator)
**A module to generate a random string based on a given string pattern**
## Installation
The package can be installed by adding `random_string_generator` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:random_string_generator, "~> 1.0.0"}
]
end
```## Usage
#### Accepted string patterns:
Use `l` for lower case letter from a to z
Use `L` for upper case letter from A to Z
Use `d` for digit from 0 to 9
Use `p` for punctuation
Use `c` for custom character
#### Punctuation is any character on the following group:
`!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`, `*`, `+`, `,`, `-`,
`.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`, `[`, ` \ `, `]`, `^`,
`_`, `{`, `|`,`}`, `~` and `` ` ``##### Generate a string containing 2 lower case letters followed by 2 digits.
```elixir
iex> RandomStringGenerator.generate("lldd")
"ol68"
```##### Generate a string containing 2 upper case letters.
```elixir
iex> RandomStringGenerator.generate("LL")
"VR"
```##### Generate a string containing 2 punctuations.
```elixir
iex> RandomStringGenerator.generate("pp")
"?!"
```**Delimiters**
Everything that is not `l`,`L`,`d`,`p` and `c` is treated as a delimiter so
the pattern `-dl?` is interpreted as a hyphen followed by a digit followed by
a lower case letter followed by a question mark.##### Generate a string containing 2 letters followed by a hyphen.
```elixir
iex> RandomStringGenerator.generate("ll-")
"yz-"
```**Scape**
In order to generate a string containing the characters `l`,`L`,`d` and `p`
as a delimiter, you need to use the backslash twice in order to scape it.##### Generate a string containing 2 digits followed by the letters `lLdp`.
```elixir
iex> RandomStringGenerator.generate("dd\\l\\L\\d\\p")
"39lLdp"
```**Custom Chars**
I order to generate a string based on a given custom pattern `ccc` a list
of possible values need to be passed as an argument.##### Generate a string containing 3 custom chars from the list `["+", "-", "/", "*"]`.
```elixir
iex> RandomStringGenerator.generate("ccc", ["+", "-", "/", "*"])
"+/*"
```If no custom char list is passed the with character `c` it will be treated
as a **delimiter** as in the example below where it generates a string containing
one digit followed by the letter c followed by another digit```elixir
iex> RandomStringGenerator.generate("dcd")
"2c1"
```**Shuffling**
In order to generate a string based on a given patter `Lldd` where those
characters are placed in a random order, the `shuffle/1` function should
be used.##### Generate a string containing 2 lower case letters, 2 digits in random order.
```elixir
iex> RandomStringGenerator.generate("Lldd") |> RandomStringGenerator.shuffle()
"s22E"
```Full documentation at [https://hexdocs.pm/random_string_generator](https://hexdocs.pm/random_string_generator).