Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2 letter state generator #233

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps preemptively nest this under fake.location? See #76.


`() => 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));
Copy link
Contributor

@serhalp serhalp Nov 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a comment at the top of this file that says this file shouldn't really exist! We should be adding new/updated tests to their own method-specific files.

Oh, which you did actually! So just remove this line please.

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