-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into feat/220-unique-values
# Conflicts: # packages/falso/src/lib/core/core.ts # packages/falso/src/lib/text.ts
- Loading branch information
Showing
28 changed files
with
957 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
Oops, something went wrong.