Skip to content

Commit

Permalink
feat: support locale definitions directly from faker.fake
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed May 3, 2022
1 parent 18614fb commit 0abe8b6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/modules/fake/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,26 @@ export class Fake {
// split the method into module and function
const parts = method.split('.');

if (this.faker[parts[0]] == null) {
throw new FakerError(`Invalid module: ${parts[0]}`);
let currentModuleOrMethod: unknown = this.faker;
let currentDefinitions: unknown = this.faker.definitions;

for (const part of parts) {
currentModuleOrMethod = currentModuleOrMethod?.[part];
currentDefinitions = currentDefinitions?.[part];
}

if (this.faker[parts[0]][parts[1]] == null) {
throw new FakerError(`Invalid method: ${parts[0]}.${parts[1]}`);
let fn: (args?: unknown) => string;
if (typeof currentModuleOrMethod === 'function') {
fn = currentModuleOrMethod as (args?: unknown) => string;
} else if (Array.isArray(currentDefinitions)) {
fn = () => this.faker.random.arrayElement(currentDefinitions as []);
} else {
throw new FakerError(`Invalid module method or definition: ${method}
faker.${method} = ${typeof currentModuleOrMethod} (must be function)
faker.definitions.${method} = ${typeof currentDefinitions} (must be an array)`);
}

// assign the function from the module.function namespace
let fn: (args?: unknown) => string = this.faker[parts[0]][parts[1]];
fn = fn.bind(this);

// If parameters are populated here, they are always going to be of string type
Expand Down
36 changes: 34 additions & 2 deletions test/fake.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,52 @@ describe('fake', () => {

it('does not allow invalid module name', () => {
expect(() => faker.fake('{{foo.bar}}')).toThrowError(
new FakerError('Invalid module: foo')
new FakerError(`Invalid module method or definition: foo.bar
faker.foo.bar = undefined (must be function)
faker.definitions.foo.bar = undefined (must be an array)`)
);
});

it('does not allow missing method name', () => {
expect(() => faker.fake('{{address}}')).toThrowError(
new FakerError(`Invalid module method or definition: address
faker.address = object (must be function)
faker.definitions.address = object (must be an array)`)
);
});

it('does not allow invalid method name', () => {
expect(() => faker.fake('{{address.foo}}')).toThrowError(
new FakerError('Invalid method: address.foo')
new FakerError(`Invalid module method or definition: address.foo
faker.address.foo = undefined (must be function)
faker.definitions.address.foo = undefined (must be an array)`)
);
});

it('does not allow invalid definitions data', () => {
expect(() => faker.fake('{{finance.credit_card}}')).toThrowError(
new FakerError(`Invalid module method or definition: finance.credit_card
faker.finance.credit_card = undefined (must be function)
faker.definitions.finance.credit_card = object (must be an array)`)
);
});

it('should be able to return empty strings', () => {
expect(faker.fake('{{helpers.repeatString}}')).toBe('');
});

it('should be able to return locale definition strings', () => {
expect(faker.definitions.cell_phone.formats).toContain(
faker.fake('{{cell_phone.formats}}')
);
});

it('should be able to return locale definition strings that starts with the name of an existing module', () => {
expect(faker.definitions.address.city_name).toContain(
faker.fake('{{address.city_name}}')
);
});

it('should be able to handle only {{ brackets', () => {
expect(faker.fake('{{hello')).toBe('{{hello');
expect(faker.fake('hello{{')).toBe('hello{{');
Expand Down

0 comments on commit 0abe8b6

Please sign in to comment.