Skip to content
Open
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
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,25 @@ yarn add @ngneat/falso

### Usage

#### Static Usage (default)

```ts
import { randEmail, randFullName } from '@ngneat/falso';
import falso, { randEmail, randFullName } from '@ngneat/falso';

const user = { email: randEmail(), name: randFullName() };

const emails = randEmail({ length: 10 });

// Or using the default instance
const email = falso.randEmail();
```

#### Instantiable Usage

```ts
import { Falso } from '@ngneat/falso';

const myFalso = new Falso();
const email = myFalso.randEmail();
```

You can specify the length of elements you want to generate. Below is an example of generating 10 emails with length equal or smaller than 20 characters.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions packages/falso/src/falso-class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as generators from './index';

class Falso {
[key: string]: any;

constructor() {
// Optionally, allow for custom seeding or config here
}
}

// Attach all generator functions to the Falso prototype
Object.entries(generators).forEach(([key, fn]) => {
if (typeof fn === 'function') {
Falso.prototype[key] = fn;
}
});

export { Falso };
export const falso = new Falso();
1 change: 1 addition & 0 deletions packages/falso/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Falso, falso } from './falso-class';
export { randAbbreviation } from './lib/abbreviation';
export { randAccessory } from './lib/accessory';
export { randAccount } from './lib/account';
Expand Down
20 changes: 20 additions & 0 deletions packages/falso/src/tests/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as randCountyFunctions from '../lib/county';
import * as randCountryFunctions from '../lib/country';
import * as randZipCodeFunctions from '../lib/zip-code';
import { randAddress } from '../lib/address';
import { Falso } from '../falso-class';

describe('randAddress', () => {
let randStreetAddressSpy: jest.SpyInstance;
Expand Down Expand Up @@ -49,6 +50,25 @@ describe('randAddress', () => {
});
});

it('should create address entity using Falso instance', () => {
randStreetAddressSpy.mockReturnValue('221B Baker Street');
randCitySpy.mockReturnValue('London');
randCountySpy.mockReturnValue('Greater London');
randCountrySpy.mockReturnValue('United Kingdom');
randZipCodeSpy.mockReturnValue('NW1 6XE');

const falso = new Falso();
const result = falso.randAddress();

expect(result).toEqual({
street: '221B Baker Street',
city: 'London',
county: 'Greater London',
country: 'United Kingdom',
zipCode: 'NW1 6XE',
});
});

describe('includeCounty IS passed', () => {
let includeCounty: boolean;

Expand Down
9 changes: 9 additions & 0 deletions packages/falso/src/tests/amount.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { randAmount } from '../lib/amount';
import { Falso } from '../falso-class';

describe('amount', () => {
it('should return a random amount between min and max default values', () => {
Expand All @@ -8,6 +9,14 @@ describe('amount', () => {
expect(res).toBeLessThanOrEqual(max);
});

it('should return a random amount using Falso instance', () => {
const [min, max] = [1.0, 9999.99];
const falso = new Falso();
const res = falso.randAmount();
expect(res).toBeGreaterThanOrEqual(min);
expect(res).toBeLessThanOrEqual(max);
});

it('should return a list of random amount between min and max default values', () => {
const [min, max] = [1.0, 9999.99];
const res = randAmount({ min, max, length: 10 });
Expand Down