Skip to content

Commit c874ab8

Browse files
Merge branch 'release/1.0.0'
2 parents 18ccf2b + 688046c commit c874ab8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+4611
-0
lines changed

Diff for: .editorconfig

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
indent_style = tab
11+
12+
[{package.json,package-lock.json}]
13+
indent_style = space
14+
indent_size = 2

Diff for: .eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/coverage/
2+
/demo/lib/
3+
/lib/

Diff for: .eslintrc.json

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"parser": "@typescript-eslint/parser",
3+
"extends": [
4+
"airbnb-base",
5+
"plugin:jest/recommended",
6+
"plugin:@typescript-eslint/eslint-recommended",
7+
"plugin:@typescript-eslint/recommended",
8+
"prettier",
9+
"prettier/@typescript-eslint"
10+
],
11+
"parserOptions": {
12+
"ecmaVersion": 2018
13+
},
14+
"rules": {
15+
"import/no-cycle": "off",
16+
"import/no-extraneous-dependencies": [
17+
"error",
18+
{
19+
"devDependencies": ["**/*.spec.{js,ts}", "**/jest.config.js"]
20+
}
21+
],
22+
"import/no-unresolved": [
23+
"error",
24+
{
25+
"ignore": [
26+
"history",
27+
// Workaround for alexgorbatchev/eslint-import-resolver-typescript#17
28+
"regexp-tree/ast",
29+
"RegexColorizer"
30+
]
31+
}
32+
],
33+
"import/prefer-default-export": "off",
34+
"no-else-return": "off",
35+
"no-plusplus": "off",
36+
// This rule excludes too many escape sequences we need, like \p
37+
"no-useless-escape": "off",
38+
// Allow ForOfStatement, preserve the rest of Airbnb's restrictions
39+
"no-restricted-syntax": [
40+
"error",
41+
"ForInStatement",
42+
"LabeledStatement",
43+
"WithStatement"
44+
],
45+
"@typescript-eslint/explicit-function-return-type": "off"
46+
},
47+
"settings": {
48+
"import/parsers": {
49+
"@typescript-eslint/parser": [".ts"]
50+
},
51+
"import/resolver": {
52+
"typescript": {}
53+
}
54+
}
55+
}

Diff for: .gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package-lock.json
2+
3+
coverage/
4+
demo/lib/
5+
node_modules/
6+
lib/

Diff for: .npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

Diff for: .prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package-lock.json
2+
package.json

Diff for: .prettierrc.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "es5",
4+
"useTabs": true
5+
}

Diff for: .publishrc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"validations": {
3+
"vulnerableDependencies": false,
4+
"uncommittedChanges": true,
5+
"untrackedFiles": true,
6+
"sensitiveData": true,
7+
"branch": "master",
8+
"gitTag": true
9+
},
10+
"confirm": true,
11+
"publishCommand": "npm publish",
12+
"publishTag": "latest",
13+
"prePublishScript": "npm run publish-please-prereqs",
14+
"postPublishScript": false
15+
}

Diff for: .snyk

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
2+
version: v1.13.5
3+
ignore: {}
4+
# patches apply the minimum changes required to fix a vulnerability
5+
patch:

Diff for: .travis.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- 'node'
4+
- '8'
5+
script:
6+
- npm run travisci
7+
cache:
8+
directories:
9+
- node_modules

Diff for: README.md

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# regex-to-strings
2+
3+
[![npm package](https://badge.fury.io/js/regex-to-strings.svg)](https://badge.fury.io/js/regex-to-strings)
4+
![node version](https://img.shields.io/node/v/regex-to-strings.svg)
5+
![npm type definitions](https://img.shields.io/npm/types/regex-to-strings)
6+
[![Build Status](https://travis-ci.org/wimpyprogrammer/regex-to-strings.svg?branch=master)](https://travis-ci.org/wimpyprogrammer/regex-to-strings)
7+
[![codecov](https://codecov.io/gh/wimpyprogrammer/regex-to-strings/branch/master/graph/badge.svg)](https://codecov.io/gh/wimpyprogrammer/regex-to-strings)
8+
[![Known Vulnerabilities](https://snyk.io/test/github/wimpyprogrammer/regex-to-strings/badge.svg)](https://snyk.io/test/github/wimpyprogrammer/regex-to-strings)
9+
10+
Generate strings that match a Regular Expression pattern. Efficiently generate all possible matches, or only the quantity you need.
11+
12+
Have you ever:
13+
14+
- scrutinized a Regular Expression while trying to visualize what it does and doesn't match?
15+
- crafted a list of strings to test whether a Regular Expression is working as intended?
16+
- needed to generate filler data like phone numbers, addresses, zip codes, and email addresses?
17+
18+
`regex-to-strings` helps with problems like these!
19+
20+
## <a href="https://www.wimpyprogrammer.com/regex-to-strings/">Demo</a>
21+
22+
## API
23+
24+
### `expand(regExPattern)`
25+
26+
The `regExPattern` parameter supports three formats:
27+
28+
1. A `RegExp` object, like `/[a-z]/i`
29+
1. A `string` that looks like a `RegExp` object, like `"/[a-z]/i"`
30+
1. A `string` containing just a Regular Expression pattern, like `"[a-z]"`
31+
32+
The returned object contains two properties:
33+
34+
- `count`: The total number of strings that match `regExPattern`
35+
- `getIterator()`: A [generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator) that yields strings matched by `regExPattern`
36+
37+
```js
38+
import { expand } from 'regex-to-strings';
39+
40+
const phoneNumberPattern = /((\(555\) ?)|(555-))?\d{3}-\d{4}/;
41+
const phoneNumberExpander = expand(phoneNumberPattern);
42+
43+
console.log(phoneNumberExpander.count); // 40000000
44+
45+
for (const phoneNumber of phoneNumberExpander.getIterator()) {
46+
console.log(phoneNumber);
47+
// (555)547-4836
48+
// 476-2063
49+
// 467-2475
50+
// (555) 194-2532
51+
// (555)403-4986
52+
// 555-838-9771
53+
// etc.
54+
}
55+
```
56+
57+
---
58+
59+
### `count(regExPattern)`
60+
61+
A shortcut to the `count` property of `expand(regExPattern)`.
62+
63+
```js
64+
import { count } from 'regex-to-strings';
65+
66+
const numStrings = count(/[a-z]{5}/i);
67+
console.log(numStrings); // 380204032
68+
```
69+
70+
---
71+
72+
### `expandN(regExPattern, n)`
73+
74+
A shortcut to take `n` strings from `expand(regExPattern).getIterator()`.
75+
76+
```js
77+
import { expandN } from 'regex-to-strings';
78+
79+
const strings = expandN(/\d{3,5}/, 5);
80+
console.log(strings); // ['84504', '94481', '3971', '69398', '7792']
81+
```
82+
83+
If the Regular Expression matches fewer than `n` strings, the returned array will contain fewer than `n` elements.
84+
85+
```js
86+
import { expandN } from 'regex-to-strings';
87+
88+
const strings = expandN(/[abc]/, 100);
89+
console.log(strings); // ['b', 'a', 'c']
90+
```
91+
92+
---
93+
94+
### `expandAll(regExPattern)`
95+
96+
A shortcut to get all strings from `expand(regExPattern).getIterator()`.
97+
98+
```js
99+
import { expandAll } from 'regex-to-strings';
100+
101+
const strings = expandAll(/\d/);
102+
console.log(strings); // ['6', '5', '0', '2', '7', '9', '4', '3', '1', '8']
103+
```
104+
105+
## Notes
106+
107+
### Supported Regular Expression syntax
108+
109+
`regex-to-strings` uses [`regexp-tree`](https://www.npmjs.com/package/regexp-tree) to parse your Regular Expression, and so the Regular Expression syntax you can use is largely determined by that library. If your pattern is not recognized by `regex-to-strings`, [try parsing it with `regexp-tree`](https://astexplorer.net/#/gist/4ea2b52f0e546af6fb14f9b2f5671c1c/39b55944da3e5782396ffa1fea3ba68d126cd394) to see if the syntax is supported.
110+
111+
`regex-to-strings` also includes [extensive positive **and** negative tests](https://github.com/wimpyprogrammer/regex-to-strings/blob/master/src/pattern.spec.ts) that track which Regular Expression features are supported.
112+
113+
### Regular Expressions with unbounded repetition
114+
115+
Regular Expressions support many techniques for matching an unlimited number of characters. For example, the following patterns will match as many `a`'s as possible:
116+
117+
- `/a*/`
118+
- `/a+/`
119+
- `/a{7,}/`
120+
121+
When `regex-to-strings` encounters a repetition with no upper bound, it artificially sets an upper bound of **100**. This is done for many reasons, chiefly so that a simple pattern like `expandAll(/a+/)` will not cause an infinite loop.
122+
123+
This also affects the `count` calculation; `expand(/a+/).count` returns `100` rather than `Infinity`.
124+
125+
### Randomizing the results
126+
127+
`regex-to-strings` goes to great lengths to randomize the generated strings. Otherwise the results would be predictable, uninteresting, and probably unhelpful.
128+
129+
```js
130+
// How results might appear without randomization
131+
const strings = expandN(/\d+/, 10);
132+
console.log(strings); // ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
133+
```
134+
135+
Random selections occur throughout the string generation process to give you a thorough sampling of matching strings.
136+
137+
## Credits
138+
139+
`regex-to-strings` relies heavily on the [`regexp-tree`](https://www.npmjs.com/package/regexp-tree) Regular Expression parser by [Dmitry Soshnikov](https://github.com/DmitrySoshnikov). Thanks!

0 commit comments

Comments
 (0)