From 2c98e949c369be41e361b15dda76f61c17a6fbb8 Mon Sep 17 00:00:00 2001 From: Alice Lin Date: Fri, 12 Nov 2021 12:02:37 -0800 Subject: [PATCH] Add 2 letter state generator --- readme.md | 8 ++++++++ src/generators/state/index.test.ts | 15 +++++++++++++++ src/generators/state/index.ts | 10 ++++++++++ src/index.test.ts | 1 + src/index.ts | 2 ++ 5 files changed, 36 insertions(+) create mode 100644 src/generators/state/index.test.ts create mode 100644 src/generators/state/index.ts diff --git a/readme.md b/readme.md index b4b4dbb..65e0812 100644 --- a/readme.md +++ b/readme.md @@ -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` diff --git a/src/generators/state/index.test.ts b/src/generators/state/index.test.ts new file mode 100644 index 0000000..e0ced13 --- /dev/null +++ b/src/generators/state/index.test.ts @@ -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]$/); + }); +}); diff --git a/src/generators/state/index.ts b/src/generators/state/index.ts new file mode 100644 index 0000000..3622186 --- /dev/null +++ b/src/generators/state/index.ts @@ -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; diff --git a/src/index.test.ts b/src/index.test.ts index 1ca7e3e..f131600 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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)); diff --git a/src/index.ts b/src/index.ts index 18b1c37..82866e9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -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),