source: node_modules/randexp/README.md

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.7 KB
Line 
1# randexp.js
2
3randexp will generate a random string that matches a given RegExp Javascript object.
4
5[![Build Status](https://secure.travis-ci.org/fent/randexp.js.svg)](http://travis-ci.org/fent/randexp.js)
6[![Dependency Status](https://david-dm.org/fent/randexp.js.svg)](https://david-dm.org/fent/randexp.js)
7[![codecov](https://codecov.io/gh/fent/randexp.js/branch/master/graph/badge.svg)](https://codecov.io/gh/fent/randexp.js)
8
9# Usage
10
11```js
12const RandExp = require('randexp');
13
14// supports grouping and piping
15new RandExp(/hello+ (world|to you)/).gen();
16// => hellooooooooooooooooooo world
17
18// sets and ranges and references
19new RandExp(/<([a-z]\w{0,20})>foo<\1>/).gen();
20// => <m5xhdg>foo<m5xhdg>
21
22// wildcard
23new 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
27new RandExp(/xxx xtreme dragon warrior xxx/i).gen();
28// => xxx xtReME dRAGON warRiOR xXX
29
30// dynamic regexp shortcut
31new RandExp('(sun|mon|tue|wednes|thurs|fri|satur)day', 'i');
32// is the same as
33new RandExp(new RegExp('(sun|mon|tue|wednes|thurs|fri|satur)day', 'i'));
34```
35
36If you're only going to use `gen()` once with a regexp and want slightly shorter syntax for it
37
38```js
39const randexp = require('randexp').randexp;
40
41randexp(/[1-6]/); // 4
42randexp('great|good( job)?|excellent'); // great
43```
44
45If you miss the old syntax
46
47```js
48require('randexp').sugar();
49
50/yes|no|maybe|i don't know/.gen(); // maybe
51```
52
53# Motivation
54
55Regular 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
57Thanks 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
61The default generated character range includes printable ASCII. In order to add or remove characters,
62a `defaultRange` attribute is exposed. you can `subtract(from, to)` and `add(from, to)`
63```js
64const randexp = new RandExp(/random stuff: .+/);
65randexp.defaultRange.subtract(32, 126);
66randexp.defaultRange.add(0, 65535);
67randexp.gen();
68// => random stuff: 湐箻ໜ䫴␩⶛㳸長���邓蕲뤀쑡篷皇硬剈궦佔칗븛뀃匫鴔事좍ﯣ⭼ꝏ䭍詳蒂䥂뽭
69```
70
71You can also change the default range by changing `RandExp.prototype.defaultRange`.
72
73# Custom PRNG
74
75The default randomness is provided by `Math.random()`. If you need to use a seedable or cryptographic PRNG, you
76can 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
80Repetitional 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
83const randexp = new RandExp(/no{1,}/);
84randexp.max = 1000000;
85```
86
87With `RandExp.sugar()`
88
89```js
90const regexp = /(hi)*/;
91regexp.max = 1000000;
92```
93
94# Bad Regular Expressions
95
96There 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
110Use 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
121Download the [minified version](https://github.com/fent/randexp.js/releases) from the latest release.
122
123
124# Tests
125
126Tests are written with [mocha](https://mochajs.org)
127
128```bash
129npm test
130```
131
132# Integration with TypeScript
133
134RandExp includes TypeScript definitions.
135
136```typescript
137import * as RandExp from "randexp";
138const randexp = new RandExp(/[a-z]{6}/);
139randexp.gen();
140```
141
142Use dtslint to check the definition file.
143
144 npm install -g dtslint
145 npm run dtslint
Note: See TracBrowser for help on using the repository browser.