Skip to content

Commit

Permalink
Merge pull request #233 from goodeggs/feat/add-state-generator
Browse files Browse the repository at this point in the history
Add 2 letter state generator
  • Loading branch information
aliceslin91 authored Nov 15, 2021
2 parents 711bd11 + 2c98e94 commit 6b82663
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ Chooses one of the elements of the provided `array`. The given array cannot be e

`() => string`

Generates a random sentence beginning with a capitalized letter and ending with a period.

#### `fake.state`

`() => string`

Generates a random two letter state code, e.g., `CA`.

#### `fake.string`

`(length?: number, charset?: string) => string`
Expand Down
15 changes: 15 additions & 0 deletions src/generators/state/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Chance} from 'chance';

import createStateGenerator from '.';

describe('state', () => {
it('generates a random state string', (): void => {
const chance = new Chance();
const state = createStateGenerator(chance);

const stateValue = state();

expect(stateValue).not.toBe(null);
expect(stateValue).toMatch(/^[A-Z][A-Z]$/);
});
});
10 changes: 10 additions & 0 deletions src/generators/state/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Chance} from 'chance';

/**
* Returns a random 2 letter state (e.g., 'CA').
*/
const createStateGenerator = (chance: Chance.Chance) => (): string => {
return chance.state();
};

export default createStateGenerator;
1 change: 1 addition & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('the default export', function () {
const values = ['a', 'b', 'c'];
expect(values).toContain(fake.sample(values));
expect(fake.digit()).toEqual(expect.any(Number));
expect(fake.state()).toEqual(expect.any(String));
expect(fake.string()).toEqual(expect.any(String));
expect(fake.uri()).toEqual(expect.any(String));
expect(fake.zip()).toEqual(expect.any(String));
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import createProducerGenerators from './generators/producer';
import createProductGenerators from './generators/product';
import createSampleGenerator from './generators/sample';
import createSentenceGenerator from './generators/sentence';
import createStateGenerator from './generators/state';
import createStringGenerator from './generators/string';
import createTzidGenerator from './generators/tzid';
import createUriGenerator from './generators/uri';
Expand Down Expand Up @@ -101,6 +102,7 @@ export const createFakeEggs = ({
product: createProductGenerators(chance),
sample: createSampleGenerator(chance),
sentence: createSentenceGenerator(chance),
state: createStateGenerator(chance),
string: createStringGenerator(chance),
tzid: createTzidGenerator(chance),
unique: createUniqueGenerator(chance),
Expand Down

0 comments on commit 6b82663

Please sign in to comment.