2c6ea5ee72fd093cb4bcf78fa3ced906237fcb4d3a915804a76c0aaffecd0cf52bb1a651d320bb4b7a7c90bf007069463b42a9513ddba8e925dadedf2b8eb9 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # randomatic [![NPM version](https://img.shields.io/npm/v/randomatic.svg?style=flat)](https://www.npmjs.com/package/randomatic) [![NPM monthly downloads](https://img.shields.io/npm/dm/randomatic.svg?style=flat)](https://npmjs.org/package/randomatic) [![NPM total downloads](https://img.shields.io/npm/dt/randomatic.svg?style=flat)](https://npmjs.org/package/randomatic) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/randomatic.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/randomatic)
  2. > Generate randomized strings of a specified length using simple character sequences. The original generate-password.
  3. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  4. ## Install
  5. Install with [npm](https://www.npmjs.com/):
  6. ```sh
  7. $ npm install --save randomatic
  8. ```
  9. ## Usage
  10. ```js
  11. var randomize = require('randomatic');
  12. ```
  13. ## API
  14. ```js
  15. randomize(pattern, length, options);
  16. randomize.isCrypto;
  17. ```
  18. * `pattern` **{String}**: (required) The pattern to use for randomizing
  19. * `length` **{Number}**: (optional) The length of the string to generate
  20. * `options` **{Object}**: (optional) See available [options](#options)
  21. * `randomize.isCrypto` will be `true` when a cryptographically secure function is being used to generate random numbers. The value will be `false` when the function in use is `Math.random`.
  22. ### pattern
  23. > The pattern to use for randomizing
  24. Patterns can contain any combination of the below characters, specified in any order.
  25. **Example:**
  26. To generate a 10-character randomized string using all available characters:
  27. ```js
  28. randomize('*', 10);
  29. //=> 'x2_^-5_T[$'
  30. randomize('Aa0!', 10);
  31. //=> 'LV3u~BSGhw'
  32. ```
  33. * `a`: Lowercase alpha characters (`abcdefghijklmnopqrstuvwxyz'`)
  34. * `A`: Uppercase alpha characters (`ABCDEFGHIJKLMNOPQRSTUVWXYZ'`)
  35. * `0`: Numeric characters (`0123456789'`)
  36. * `!`: Special characters (`~!@#$%^&()_+-={}[];\',.`)
  37. * `*`: All characters (all of the above combined)
  38. * `?`: Custom characters (pass a string of custom characters to the options)
  39. ### length
  40. > The length of the string to generate
  41. **Examples:**
  42. * `randomize('A', 5)` will generate a 5-character, uppercase, alphabetical, randomized string, e.g. `KDJWJ`.
  43. * `randomize('0', 2)` will generate a 2-digit random number
  44. * `randomize('0', 3)` will generate a 3-digit random number
  45. * `randomize('0', 12)` will generate a 12-digit random number
  46. * `randomize('A0', 16)` will generate a 16-character, alpha-numeric randomized string
  47. If `length` is left undefined, the length of the pattern in the first parameter will be used. For example:
  48. * `randomize('00')` will generate a 2-digit random number
  49. * `randomize('000')` will generate a 3-digit random number
  50. * `randomize('0000')` will generate a 4-digit random number...
  51. * `randomize('AAAAA')` will generate a 5-character, uppercase alphabetical random string...
  52. These are just examples, [see the tests](./test.js) for more use cases and examples.
  53. ## options
  54. > These are options that can be passed as the third argument.
  55. #### chars
  56. Type: `String`
  57. Default: `undefined`
  58. Define a custom string to be randomized.
  59. **Example:**
  60. * `randomize('?', 20, {chars: 'jonschlinkert'})` will generate a 20-character randomized string from the letters contained in `jonschlinkert`.
  61. * `randomize('?', {chars: 'jonschlinkert'})` will generate a 13-character randomized string from the letters contained in `jonschlinkert`.
  62. #### exclude
  63. Type: `String|Array`
  64. Default: `undefined`
  65. Specify a string or array of characters can are excluded from the possible characters used to generate the randomized string.
  66. **Example:**
  67. * `randomize('*', 20, { exclude: '0oOiIlL1' })` will generate a 20-character randomized string using all of possible characters except for `0oOiIlL1`.
  68. ## Usage Examples
  69. * `randomize('A', 4)` (_whitespace insenstive_) would result in randomized 4-digit uppercase letters, like, `ZAKH`, `UJSL`... etc.
  70. * `randomize('AAAA')` is equivelant to `randomize('A', 4)`
  71. * `randomize('AAA0')` and `randomize('AA00')` and `randomize('A0A0')` are equivelant to `randomize('A0', 4)`
  72. * `randomize('aa')`: results in double-digit, randomized, lower-case letters (`abcdefghijklmnopqrstuvwxyz`)
  73. * `randomize('AAA')`: results in triple-digit, randomized, upper-case letters (`ABCDEFGHIJKLMNOPQRSTUVWXYZ`)
  74. * `randomize('0', 6)`: results in six-digit, randomized numbers (`0123456789`)
  75. * `randomize('!', 5)`: results in single-digit randomized, _valid_ non-letter characters (`~!@#$%^&()_+-={}[]
  76. * `randomize('A!a0', 9)`: results in nine-digit, randomized characters (any of the above)
  77. _The order in which the characters are defined is insignificant._
  78. ## About
  79. <details>
  80. <summary><strong>Contributing</strong></summary>
  81. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  82. </details>
  83. <details>
  84. <summary><strong>Running Tests</strong></summary>
  85. Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
  86. ```sh
  87. $ npm install && npm test
  88. ```
  89. </details>
  90. <details>
  91. <summary><strong>Building docs</strong></summary>
  92. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  93. To generate the readme, run the following command:
  94. ```sh
  95. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  96. ```
  97. </details>
  98. ### Related projects
  99. You might also be interested in these projects:
  100. * [pad-left](https://www.npmjs.com/package/pad-left): Left pad a string with zeros or a specified string. Fastest implementation. | [homepage](https://github.com/jonschlinkert/pad-left "Left pad a string with zeros or a specified string. Fastest implementation.")
  101. * [pad-right](https://www.npmjs.com/package/pad-right): Right pad a string with zeros or a specified string. Fastest implementation. | [homepage](https://github.com/jonschlinkert/pad-right "Right pad a string with zeros or a specified string. Fastest implementation.")
  102. * [repeat-string](https://www.npmjs.com/package/repeat-string): Repeat the given string n times. Fastest implementation for repeating a string. | [homepage](https://github.com/jonschlinkert/repeat-string "Repeat the given string n times. Fastest implementation for repeating a string.")
  103. ### Contributors
  104. | **Commits** | **Contributor** |
  105. | --- | --- |
  106. | 56 | [jonschlinkert](https://github.com/jonschlinkert) |
  107. | 6 | [doowb](https://github.com/doowb) |
  108. | 4 | [kivlor](https://github.com/kivlor) |
  109. | 2 | [realityking](https://github.com/realityking) |
  110. | 2 | [ywpark1](https://github.com/ywpark1) |
  111. | 1 | [TrySound](https://github.com/TrySound) |
  112. | 1 | [drag0s](https://github.com/drag0s) |
  113. | 1 | [paulmillr](https://github.com/paulmillr) |
  114. | 1 | [sunknudsen](https://github.com/sunknudsen) |
  115. | 1 | [faizulhaque-tp](https://github.com/faizulhaque-tp) |
  116. | 1 | [michaelrhodes](https://github.com/michaelrhodes) |
  117. ### Author
  118. **Jon Schlinkert**
  119. * [GitHub Profile](https://github.com/jonschlinkert)
  120. * [Twitter Profile](https://twitter.com/jonschlinkert)
  121. * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
  122. ### License
  123. Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
  124. Released under the [MIT License](LICENSE).
  125. ***
  126. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on October 23, 2018._