Skip to content

Commit a2c48b3

Browse files
committed
feat: make falso instantiable
1 parent 96a9c10 commit a2c48b3

File tree

6 files changed

+65
-3
lines changed

6 files changed

+65
-3
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,25 @@ yarn add @ngneat/falso
6161

6262
### Usage
6363

64+
#### Static Usage (default)
65+
6466
```ts
65-
import { randEmail, randFullName } from '@ngneat/falso';
67+
import falso, { randEmail, randFullName } from '@ngneat/falso';
6668

6769
const user = { email: randEmail(), name: randFullName() };
68-
6970
const emails = randEmail({ length: 10 });
71+
72+
// Or using the default instance
73+
const email = falso.randEmail();
74+
```
75+
76+
#### Instantiable Usage
77+
78+
```ts
79+
import { Falso } from '@ngneat/falso';
80+
81+
const myFalso = new Falso();
82+
const email = myFalso.randEmail();
7083
```
7184

7285
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.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/falso/src/falso-class.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as generators from './index';
2+
3+
class Falso {
4+
[key: string]: any;
5+
6+
constructor() {
7+
// Optionally, allow for custom seeding or config here
8+
}
9+
}
10+
11+
// Attach all generator functions to the Falso prototype
12+
Object.entries(generators).forEach(([key, fn]) => {
13+
if (typeof fn === 'function') {
14+
Falso.prototype[key] = fn;
15+
}
16+
});
17+
18+
export { Falso };
19+
export const falso = new Falso();

packages/falso/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { Falso, falso } from './falso-class';
12
export { randAbbreviation } from './lib/abbreviation';
23
export { randAccessory } from './lib/accessory';
34
export { randAccount } from './lib/account';

packages/falso/src/tests/address.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as randCountyFunctions from '../lib/county';
44
import * as randCountryFunctions from '../lib/country';
55
import * as randZipCodeFunctions from '../lib/zip-code';
66
import { randAddress } from '../lib/address';
7+
import { Falso } from '../falso-class';
78

89
describe('randAddress', () => {
910
let randStreetAddressSpy: jest.SpyInstance;
@@ -49,6 +50,25 @@ describe('randAddress', () => {
4950
});
5051
});
5152

53+
it('should create address entity using Falso instance', () => {
54+
randStreetAddressSpy.mockReturnValue('221B Baker Street');
55+
randCitySpy.mockReturnValue('London');
56+
randCountySpy.mockReturnValue('Greater London');
57+
randCountrySpy.mockReturnValue('United Kingdom');
58+
randZipCodeSpy.mockReturnValue('NW1 6XE');
59+
60+
const falso = new Falso();
61+
const result = falso.randAddress();
62+
63+
expect(result).toEqual({
64+
street: '221B Baker Street',
65+
city: 'London',
66+
county: 'Greater London',
67+
country: 'United Kingdom',
68+
zipCode: 'NW1 6XE',
69+
});
70+
});
71+
5272
describe('includeCounty IS passed', () => {
5373
let includeCounty: boolean;
5474

packages/falso/src/tests/amount.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { randAmount } from '../lib/amount';
2+
import { Falso } from '../falso-class';
23

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

12+
it('should return a random amount using Falso instance', () => {
13+
const [min, max] = [1.0, 9999.99];
14+
const falso = new Falso();
15+
const res = falso.randAmount();
16+
expect(res).toBeGreaterThanOrEqual(min);
17+
expect(res).toBeLessThanOrEqual(max);
18+
});
19+
1120
it('should return a list of random amount between min and max default values', () => {
1221
const [min, max] = [1.0, 9999.99];
1322
const res = randAmount({ min, max, length: 10 });

0 commit comments

Comments
 (0)