Skip to content

Commit

Permalink
feat(parseregex): introduces parseRegex helper
Browse files Browse the repository at this point in the history
+ Add helpers.parseRegex to wrap RandExp and support replacements object
+ Install RandExp to generate random data from regex
  • Loading branch information
samir-araujo committed Apr 11, 2020
1 parent de922b4 commit 8e3a343
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const anotherName = faker.name.firstName(); // Random first name: Marilyne
- randomArrayElement
- randomArrayElements
- randomNumber
- parseRegex

## Upcoming methods

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"lint-staged": ">=10",
"lodash": "^4.17.15",
"np": "^6.2.0",
"randexp": "^0.5.3",
"semantic-release": "^17.0.4",
"ts-jest": "^25.3.0",
"ts-loader": "^6.2.2",
Expand Down
12 changes: 8 additions & 4 deletions src/helpers/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import helpers from '..';

describe('Helpers', () => {
it('has randomNumber module', () => {
it('should have randomNumber module', () => {
expect(helpers).toHaveProperty('randomNumber');
});

it('has randomArrayElement module', () => {
it('should have randomArrayElement module', () => {
expect(helpers).toHaveProperty('randomArrayElement');
});

it('has randomArrayElements module', () => {
it('should have randomArrayElements module', () => {
expect(helpers).toHaveProperty('randomArrayElements');
});

it('has getLocale module', () => {
it('should have getLocale module', () => {
expect(helpers).toHaveProperty('getLocale');
});

it('should have parseRegex module', () => {
expect(helpers).toHaveProperty('parseRegex');
});
});
43 changes: 43 additions & 0 deletions src/helpers/__tests__/parseRegex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import parseRegex from '../parseRegex';

describe('Helpers | parseRegex', () => {
it('should create a random string from a RegEx', () => {
const result = parseRegex(/\d{1,5}/);

expect(result).toEqual(expect.stringMatching(/\d{1,5}/));
});

it('should create a random string from an array of keys', () => {
const result = parseRegex(/:suffix :firstName/, {
firstName: 'foo',
suffix: 'bar',
});

expect(result).toEqual('bar foo');
});

it('should create a random string from a combination of RegEx and array of keys', () => {
const result = parseRegex(/:street - \d{1}/, {
street: 'foobar',
});

expect(result).toEqual(expect.stringMatching(/foobar - \d{1}/));
});

it('should create complex string from regex', () => {
const result = parseRegex(
/:firstName :lastName, you're invited to our party. Please meet us at :street, \d{2}/,
{
firstName: 'William',
lastName: 'Wallace',
street: 'nowhere',
}
);

expect(result).toEqual(
expect.stringMatching(
/William Wallace, you're invited to our party. Please meet us at nowhere, \d{2}/
)
);
});
});
9 changes: 8 additions & 1 deletion src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import getLocale from './getLocale';
import parseRegex from './parseRegex';
import randomArrayElement from './randomArrayElement';
import randomArrayElements from './randomArrayElements';
import randomNumber from './randomNumber';

export default { getLocale, randomArrayElement, randomArrayElements, randomNumber };
export default {
getLocale,
parseRegex,
randomArrayElement,
randomArrayElements,
randomNumber,
};
18 changes: 18 additions & 0 deletions src/helpers/parseRegex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import RandExp from 'randexp';

export default function parseRegex(
regex: RegExp,
replacements?: { [key: string]: string }
): string {
let result = new RandExp(regex).gen();

if (replacements !== undefined) {
Object.keys(replacements).forEach((key) => {
const pattern = new RegExp(`:${key}`, 'g');

result = result.replace(pattern, replacements[key]);
});
}

return result;
}
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2994,6 +2994,11 @@ dotenv@^5.0.1:
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-5.0.1.tgz#a5317459bd3d79ab88cff6e44057a6a3fbb1fcef"
integrity sha512-4As8uPrjfwb7VXC+WnLCbXK7y+Ueb2B3zgNCePYfhxS1PYeaO1YTeplffTEcbfLhvFNGLAz90VvJs9yomG7bow==

drange@^1.0.2:
version "1.1.1"
resolved "https://registry.yarnpkg.com/drange/-/drange-1.1.1.tgz#b2aecec2aab82fcef11dbbd7b9e32b83f8f6c0b8"
integrity sha512-pYxfDYpued//QpnLIm4Avk7rsNtAtQkUES2cwAYSvD/wd2pKD71gN2Ebj3e7klzXwjocvE8c5vx/1fxwpqmSxA==

duplexer2@~0.1.0:
version "0.1.4"
resolved "https://registry.yarnpkg.com/duplexer2/-/duplexer2-0.1.4.tgz#8b12dab878c0d69e3e7891051662a32fc6bddcc1"
Expand Down Expand Up @@ -7388,6 +7393,14 @@ qw@~1.0.1:
resolved "https://registry.yarnpkg.com/qw/-/qw-1.0.1.tgz#efbfdc740f9ad054304426acb183412cc8b996d4"
integrity sha1-77/cdA+a0FQwRCassYNBLMi5ltQ=

randexp@^0.5.3:
version "0.5.3"
resolved "https://registry.yarnpkg.com/randexp/-/randexp-0.5.3.tgz#f31c2de3148b30bdeb84b7c3f59b0ebb9fec3738"
integrity sha512-U+5l2KrcMNOUPYvazA3h5ekF80FHTUG+87SEAmHZmolh1M+i/WyTCxVzmi+tidIa1tM4BSe8g2Y/D3loWDjj+w==
dependencies:
drange "^1.0.2"
ret "^0.2.0"

rc@^1.0.1, rc@^1.1.6, rc@^1.2.8:
version "1.2.8"
resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed"
Expand Down Expand Up @@ -7828,6 +7841,11 @@ restore-cursor@^3.1.0:
onetime "^5.1.0"
signal-exit "^3.0.2"

ret@^0.2.0:
version "0.2.2"
resolved "https://registry.yarnpkg.com/ret/-/ret-0.2.2.tgz#b6861782a1f4762dce43402a71eb7a283f44573c"
integrity sha512-M0b3YWQs7R3Z917WRQy1HHA7Ba7D8hvZg6UE5mLykJxQVE2ju0IXbGlaHPPlkY+WN7wFP+wUMXmBFA0aV6vYGQ==

ret@~0.1.10:
version "0.1.15"
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
Expand Down

0 comments on commit 8e3a343

Please sign in to comment.