1 | # randexp.js
|
---|
2 |
|
---|
3 | randexp will generate a random string that matches a given RegExp Javascript object.
|
---|
4 |
|
---|
5 | [](http://travis-ci.org/fent/randexp.js)
|
---|
6 | [](https://david-dm.org/fent/randexp.js)
|
---|
7 | [](https://codecov.io/gh/fent/randexp.js)
|
---|
8 |
|
---|
9 | # Usage
|
---|
10 |
|
---|
11 | ```js
|
---|
12 | const RandExp = require('randexp');
|
---|
13 |
|
---|
14 | // supports grouping and piping
|
---|
15 | new RandExp(/hello+ (world|to you)/).gen();
|
---|
16 | // => hellooooooooooooooooooo world
|
---|
17 |
|
---|
18 | // sets and ranges and references
|
---|
19 | new RandExp(/<([a-z]\w{0,20})>foo<\1>/).gen();
|
---|
20 | // => <m5xhdg>foo<m5xhdg>
|
---|
21 |
|
---|
22 | // wildcard
|
---|
23 | new RandExp(/random stuff: .+/).gen();
|
---|
24 | // => random stuff: l3m;Hf9XYbI [YPaxV>U*4-_F!WXQh9>;rH3i l!8.zoh?[utt1OWFQrE ^~8zEQm]~tK
|
---|
25 |
|
---|
26 | // ignore case
|
---|
27 | new RandExp(/xxx xtreme dragon warrior xxx/i).gen();
|
---|
28 | // => xxx xtReME dRAGON warRiOR xXX
|
---|
29 |
|
---|
30 | // dynamic regexp shortcut
|
---|
31 | new RandExp('(sun|mon|tue|wednes|thurs|fri|satur)day', 'i');
|
---|
32 | // is the same as
|
---|
33 | new RandExp(new RegExp('(sun|mon|tue|wednes|thurs|fri|satur)day', 'i'));
|
---|
34 | ```
|
---|
35 |
|
---|
36 | If you're only going to use `gen()` once with a regexp and want slightly shorter syntax for it
|
---|
37 |
|
---|
38 | ```js
|
---|
39 | const randexp = require('randexp').randexp;
|
---|
40 |
|
---|
41 | randexp(/[1-6]/); // 4
|
---|
42 | randexp('great|good( job)?|excellent'); // great
|
---|
43 | ```
|
---|
44 |
|
---|
45 | If you miss the old syntax
|
---|
46 |
|
---|
47 | ```js
|
---|
48 | require('randexp').sugar();
|
---|
49 |
|
---|
50 | /yes|no|maybe|i don't know/.gen(); // maybe
|
---|
51 | ```
|
---|
52 |
|
---|
53 | # Motivation
|
---|
54 |
|
---|
55 | Regular expressions are used in every language, every programmer is familiar with them. Regex can be used to easily express complex strings. What better way to generate a random string than with a language you can use to express the string you want?
|
---|
56 |
|
---|
57 | Thanks to [String-Random](http://search.cpan.org/~steve/String-Random-0.22/lib/String/Random.pm) for giving me the idea to make this in the first place and [randexp](https://github.com/benburkert/randexp) for the sweet `.gen()` syntax.
|
---|
58 |
|
---|
59 | # Default Range
|
---|
60 |
|
---|
61 | The default generated character range includes printable ASCII. In order to add or remove characters,
|
---|
62 | a `defaultRange` attribute is exposed. you can `subtract(from, to)` and `add(from, to)`
|
---|
63 | ```js
|
---|
64 | const randexp = new RandExp(/random stuff: .+/);
|
---|
65 | randexp.defaultRange.subtract(32, 126);
|
---|
66 | randexp.defaultRange.add(0, 65535);
|
---|
67 | randexp.gen();
|
---|
68 | // => random stuff: 湐箻ໜ䫴㳸長���邓蕲뤀쑡篷皇硬剈궦佔칗븛뀃匫鴔事좍ﯣ⭼ꝏ䭍詳蒂䥂뽭
|
---|
69 | ```
|
---|
70 |
|
---|
71 | You can also change the default range by changing `RandExp.prototype.defaultRange`.
|
---|
72 |
|
---|
73 | # Custom PRNG
|
---|
74 |
|
---|
75 | The default randomness is provided by `Math.random()`. If you need to use a seedable or cryptographic PRNG, you
|
---|
76 | can override `RandExp.prototype.randInt` or `randexp.randInt` (where `randexp` is an instance of `RandExp`). `randInt(from, to)` accepts an inclusive range and returns a randomly selected number within that range.
|
---|
77 |
|
---|
78 | # Infinite Repetitionals
|
---|
79 |
|
---|
80 | Repetitional tokens such as `*`, `+`, and `{3,}` have an infinite max range. In this case, randexp looks at its min and adds 100 to it to get a useable max value. If you want to use another int other than 100 you can change the `max` property in `RandExp.prototype` or the RandExp instance.
|
---|
81 |
|
---|
82 | ```js
|
---|
83 | const randexp = new RandExp(/no{1,}/);
|
---|
84 | randexp.max = 1000000;
|
---|
85 | ```
|
---|
86 |
|
---|
87 | With `RandExp.sugar()`
|
---|
88 |
|
---|
89 | ```js
|
---|
90 | const regexp = /(hi)*/;
|
---|
91 | regexp.max = 1000000;
|
---|
92 | ```
|
---|
93 |
|
---|
94 | # Bad Regular Expressions
|
---|
95 |
|
---|
96 | There are some regular expressions which can never match any string.
|
---|
97 |
|
---|
98 | * Ones with badly placed positionals such as `/a^/` and `/$c/m`. Randexp will ignore positional tokens.
|
---|
99 |
|
---|
100 | * Back references to non-existing groups like `/(a)\1\2/`. Randexp will ignore those references, returning an empty string for them. If the group exists only after the reference is used such as in `/\1 (hey)/`, it will too be ignored.
|
---|
101 |
|
---|
102 | * Custom negated character sets with two sets inside that cancel each other out. Example: `/[^\w\W]/`. If you give this to randexp, it will return an empty string for this set since it can't match anything.
|
---|
103 |
|
---|
104 |
|
---|
105 | # Projects based on randexp.js
|
---|
106 |
|
---|
107 |
|
---|
108 | ## JSON-Schema Faker
|
---|
109 |
|
---|
110 | Use generators to populate JSON Schema samples. See: [jsf on github](https://github.com/json-schema-faker/json-schema-faker/) and [jsf demo page](http://json-schema-faker.js.org/).
|
---|
111 |
|
---|
112 |
|
---|
113 | # Install
|
---|
114 |
|
---|
115 | ### Node.js
|
---|
116 |
|
---|
117 | npm install randexp
|
---|
118 |
|
---|
119 | ### Browser
|
---|
120 |
|
---|
121 | Download the [minified version](https://github.com/fent/randexp.js/releases) from the latest release.
|
---|
122 |
|
---|
123 |
|
---|
124 | # Tests
|
---|
125 |
|
---|
126 | Tests are written with [mocha](https://mochajs.org)
|
---|
127 |
|
---|
128 | ```bash
|
---|
129 | npm test
|
---|
130 | ```
|
---|
131 |
|
---|
132 | # Integration with TypeScript
|
---|
133 |
|
---|
134 | RandExp includes TypeScript definitions.
|
---|
135 |
|
---|
136 | ```typescript
|
---|
137 | import * as RandExp from "randexp";
|
---|
138 | const randexp = new RandExp(/[a-z]{6}/);
|
---|
139 | randexp.gen();
|
---|
140 | ```
|
---|
141 |
|
---|
142 | Use dtslint to check the definition file.
|
---|
143 |
|
---|
144 | npm install -g dtslint
|
---|
145 | npm run dtslint
|
---|