Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/falso/src/lib/core/core.ts
#	packages/falso/src/lib/text.ts
  • Loading branch information
Ryan Smee committed Jun 13, 2022
2 parents f29cca6 + ccde521 commit e7dac07
Show file tree
Hide file tree
Showing 28 changed files with 957 additions and 48 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Create massive amounts of fake data in the browser and NodeJS. Tree Shakeable &
 195 Functions
 Tree Shakable
 Fully Typed
 Factory Functions
 Entity Functions
 Single and Array Result

Expand Down
36 changes: 36 additions & 0 deletions docs/docs/factories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
sidebar_label: Factories
title: Factories
---

### ```incrementalNumber```

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

const factory = incrementalNumber()
factory() // returns 1
factory() // returns 2

const factory = incrementalNumber({from: 10, to: 100, step: 10})
factory() // returns 10
factory() // returns 20
...
```

### ```incrementalDate```

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

const dateFactory = incrementalDate()
// seeds factory with new Date()
dateFactory() // returns seed
dateFactory() // returns seed + 1 day

const dateFactory = incrementalDate({from: new Date(2022,1,1), to: new Date(2022,1,3), step: 10})
// seeds factory with `from` value
dateFactory() // returns seed
dateFactory() // returns seed + 10ms
...
```
3 changes: 2 additions & 1 deletion docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
const glob = require('glob');

const items = [
...glob.sync('./docs/*.md?(x)'),
...glob.sync('./docs/getting-started.mdx'),
...glob.sync('./docs/factories.mdx'),
...glob.sync('./docs/auto-generated/*.md?(x)')
].map(p => p.replace(/\.\/docs\/(.+)\.mdx?/, '$1'));

Expand Down
15 changes: 14 additions & 1 deletion package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"cz-conventional-changelog": "3.3.0",
"eslint": "8.12.0",
"eslint-config-prettier": "8.1.0",
"expect-type": "0.13.0",
"git-cz": "4.8.0",
"glob": "7.2.0",
"husky": "6.0.0",
Expand All @@ -56,12 +57,12 @@
"jsdoc-to-markdown": "7.1.0",
"json-update": "5.3.0",
"lint-staged": "11.1.2",
"nx": "14.1.4",
"prettier": "2.6.2",
"terser": "5.10.0",
"ts-jest": "27.1.4",
"ts-morph": "13.0.2",
"typescript": "4.4.3",
"nx": "14.1.4"
"typescript": "4.4.3"
},
"config": {
"commitizen": {
Expand Down
53 changes: 53 additions & 0 deletions packages/falso/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,59 @@

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).

# [5.7.0](https://github.com/ngneat/falso/compare/falso-5.6.1...falso-5.7.0) (2022-06-07)

### Features

- 🔥 add i18n firstname locale ([e99af4d](https://github.com/ngneat/falso/commit/e99af4d77b2965b322f3eccfc7081e3bad31c904))

## [5.6.1](https://github.com/ngneat/falso/compare/falso-5.6.0...falso-5.6.1) (2022-06-05)

### Bug Fixes

- remove unexpected "ą" from names without accents ([fe71a6f](https://github.com/ngneat/falso/commit/fe71a6f6b5dc18ed7dfe6e9557ccdff5d281b444))

# [5.6.0](https://github.com/ngneat/falso/compare/falso-5.5.0...falso-5.6.0) (2022-06-03)

### Bug Fixes

- allow string and number as input ([50fbd30](https://github.com/ngneat/falso/commit/50fbd3061674ae2ffcca31b3284dd5131b3bb826))
- expose interface types ([d6af604](https://github.com/ngneat/falso/commit/d6af60478e294f7b8f963a4911607e10246bfd95))

### Features

- 🔥 add randIncrementalDate function ([e35c364](https://github.com/ngneat/falso/commit/e35c36433d22690a53c8d6855748dcfddc349946)), closes [#277](https://github.com/ngneat/falso/issues/277)

# [5.5.0](https://github.com/ngneat/falso/compare/falso-5.4.1...falso-5.5.0) (2022-06-02)

### Bug Fixes

- 🐞 change randIncrementalNumber to incrementalNumber ([2ca7d85](https://github.com/ngneat/falso/commit/2ca7d8544a1fc937d91cb32f47428eb21704c157))
- cleanup after review ([0df444d](https://github.com/ngneat/falso/commit/0df444d9ea1d8d9198f0fb9c7ba0cf5e82327ee4))

### Features

- 🔥 add randIncrementalNumber function ([de0afbb](https://github.com/ngneat/falso/commit/de0afbb2c51cff94aedb84cc0e605c8da1a3f977)), closes [#278](https://github.com/ngneat/falso/issues/278)

## [5.4.1](https://github.com/ngneat/falso/compare/falso-5.4.0...falso-5.4.1) (2022-06-01)

### Bug Fixes

- 🐞 support readonly arrays ([1cb0c63](https://github.com/ngneat/falso/commit/1cb0c637864051497e5d836ff508d8274a83afa2)), closes [#275](https://github.com/ngneat/falso/issues/275)

# [5.4.0](https://github.com/ngneat/falso/compare/falso-5.3.0...falso-5.4.0) (2022-05-29)

### Bug Fixes

- 🐞 Fix full name tests ([6f8348d](https://github.com/ngneat/falso/commit/6f8348dc6f877f70d9d3282ae5944014432e2e9f))
- 🐞 Generate uuid from seed ([0825d8f](https://github.com/ngneat/falso/commit/0825d8f6d6062559c9055bdd4910981877c22d30)), closes [#232](https://github.com/ngneat/falso/issues/232)
- **phone-numbers:** fixed typos ([23137c7](https://github.com/ngneat/falso/commit/23137c7bd7f1bba655d825579cba84fa2f9539e7))
- **phone-numbers:** fixed typos ([cadfc36](https://github.com/ngneat/falso/commit/cadfc36f6ec469d19eedd568a9359ecb9ffaff44))

### Features

- 🔥 allow date strings for betweenDate ([e4da11d](https://github.com/ngneat/falso/commit/e4da11db3fc652c4769c99f92d5b05e323cfdbe2))

# [5.3.0](https://github.com/ngneat/falso/compare/falso-5.2.1...falso-5.3.0) (2022-05-12)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion packages/falso/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngneat/falso",
"version": "5.3.0",
"version": "5.7.0",
"license": "MIT",
"description": "All the Fake Data for All Your Real Needs",
"keywords": [
Expand Down
8 changes: 8 additions & 0 deletions packages/falso/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,11 @@ export { randAirport } from './lib/airport';
export { randAirportName } from './lib/airport-name';
export { randAirportCode } from './lib/airport-code';
export { randCreditCard } from './lib/credit-card';
export {
incrementalNumber,
IncrementalNumberOptions,
} from './lib/factories/incremental-number';
export {
incrementalDate,
IncrementalDateOptions,
} from './lib/factories/incremental-date';
8 changes: 4 additions & 4 deletions packages/falso/src/lib/between-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { randNumber } from './number';
import { dateIsUnique } from './core/unique-validators';

interface BetweenOptions extends FakeOptions {
from: Date;
to: Date;
from: Date | string;
to: Date | string;
}

/**
Expand All @@ -28,8 +28,8 @@ interface BetweenOptions extends FakeOptions {
export function randBetweenDate<Options extends BetweenOptions = never>(
options: Options
) {
const from = options.from.getTime();
const to = options.to.getTime();
const from = new Date(options.from).getTime();
const to = new Date(options.to).getTime();

if (from >= to) {
throw new Error('From must be before to');
Expand Down
59 changes: 59 additions & 0 deletions packages/falso/src/lib/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,62 @@ export function getRandomInRange({
}: RandomInRangeOptions = {}) {
return Number((random() * (max - min) + min).toFixed(fraction));
}

/*
import { random } from '../random';
export interface FakeOptions {
length?: number;
locale?: any | string[];
}
export type Return<T, O extends FakeOptions> = [O] extends [never]
? T
: O['length'] extends number
? T[]
: T;
type FactoryFunction<T> = (i: number) => T;
export function fake<T, Options extends FakeOptions>(
data: Readonly<T[]> | FactoryFunction<T>,
options?: Options
): Return<T, Options> {
const dataSource = Array.isArray(data)
? () => randElement(data)
: (data as FactoryFunction<T>);
if (options?.length === undefined) {
return dataSource(0) as any;
}
return Array.from({ length: options.length }, (_, index) =>
dataSource(index)
) as any;
}
export function randElement<T>(arr: Readonly<T[]>) {
return arr[Math.floor(random() * arr.length)];
}
export interface RandomInRangeOptions {
min?: number;
max?: number;
fraction?: number;
}
export function getRandomInRange({
min = 1.0,
max = 9999.99,
fraction = 0,
}: RandomInRangeOptions = {}) {
return Number((random() * (max - min) + min).toFixed(fraction));
}
*/
3 changes: 3 additions & 0 deletions packages/falso/src/lib/core/isNotNil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isNotNil<T>(value: T | null | undefined): value is T {
return value !== null || value !== undefined;
}
68 changes: 68 additions & 0 deletions packages/falso/src/lib/factories/incremental-date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { isNotNil } from '../core/isNotNil';
import { incrementalNumber } from './incremental-number';

export interface IncrementalDateOptions {
from: Date | string | number;
to?: Date | string | number;
step: number;
}

const millisecondsPerDay = 24 * 60 * 60 * 1_000;

/**
* Generate incremental numbers.
*
* @category factories
*
* @example
*
* const factory = incrementalDate()
* // seeds factory with new Date()
* factory() // returns seed
* factory() // returns seed + 1 day
*
* @example
*
* const factory = incrementalDate({from: new Date(2022,1,1), to: new Date(2022,1,3), step: 10})
* // seeds factory with `from` value
* factory() // returns seed
* factory() // returns seed + 10ms
* ...
*
*/
export function incrementalDate(
options: IncrementalDateOptions = {
from: new Date(),
step: millisecondsPerDay,
}
): () => Date | undefined {
const from = new Date(options.from);
const to = options.to ? new Date(options.to) : undefined;

if (options.step === 0) {
throw new Error(
'`step` should be a number different from 0, for example: {from: new Date(), step: 10}'
);
}

if (isNotNil(to)) {
if (from > to && options.step > 0) {
throw new Error('`to` should be a date greater than or equal to `from`');
}

if (from < to && options.step < 0) {
throw new Error('`to` should be a date lower than or equal to `from`');
}
}

const numberFactory = incrementalNumber({
from: from.getTime(),
to: to?.getTime(),
step: options.step,
});

return () => {
const next = numberFactory();
return next ? new Date(next) : undefined;
};
}
Loading

0 comments on commit e7dac07

Please sign in to comment.