-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parseregex): introduces parseRegex helper
+ Add helpers.parseRegex to wrap RandExp and support replacements object + Install RandExp to generate random data from regex
- Loading branch information
1 parent
de922b4
commit 8e3a343
Showing
7 changed files
with
97 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}/ | ||
) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters