Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: accept dates as params for Date methods #200

Merged
merged 62 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
4b6e357
Accept dates as params for Date methods
pkuczynski Jan 17, 2022
302dbd6
Fix ts errors
pkuczynski Jan 17, 2022
6d42f5f
Update date.ts
pkuczynski Jan 17, 2022
8a00487
Update helpers.ts
pkuczynski Jan 17, 2022
80cfffa
Update date.ts
pkuczynski Jan 17, 2022
82ac248
fix: add/fix tests for date class methods accepting Date object
pkuczynski Jan 17, 2022
ca82fea
Updated documentation in api/date.md to account for type definition c…
LawrenceWooding Jan 21, 2022
1eb7e14
Merge pull request #1 from LawrenceWooding/patch-1
pkuczynski Jan 21, 2022
b36fc7b
Apply suggestions from code review
pkuczynski Jan 21, 2022
8560055
toMilliseconds
pkuczynski Jan 21, 2022
f518ceb
chore(docs): fix imports (#154)
Shinigami92 Jan 17, 2022
6a45e9a
chore: migrate image providers to TS (#178)
Shinigami92 Jan 18, 2022
e750453
refactor: Use datatype.number() function instead of random.number() (…
luciferreeves Jan 18, 2022
410d6a6
fix(types): fixed datetime types for optional argument (#192)
jared-fraser Jan 18, 2022
2c8e368
chore: add commit conventions doc (#204)
mbokinala Jan 18, 2022
81d7d5e
feat!: support tree-shaking (#152)
Shinigami92 Jan 18, 2022
bc8ecbd
docs: adding new logo (#205)
JessicaSachs Jan 18, 2022
2140661
ci: add node version matrix (#179)
MohdImran001 Jan 18, 2022
2fdbb20
chore: add CODEOWNERS (#158)
damienwebdev Jan 19, 2022
c75257c
fix: ts support for locales (#169)
Shinigami92 Jan 19, 2022
1e48d79
chore: switch to pnpm (#153)
Shinigami92 Jan 19, 2022
2ad1187
docs(readme.md): api methods table with description (#160)
fzn0x Jan 19, 2022
b5e5c4c
chore: add support for readonly arrays (#189)
clarkerican Jan 19, 2022
b33d847
fix(types): fixed exampleEmail arguments types (#241)
sunadoi Jan 20, 2022
944a978
feat: migrate locales to typescript (#238)
ST-DDT Jan 20, 2022
e74433a
docs(readme.md): add repository badges (#161)
fzn0x Jan 20, 2022
6979a9b
docs: shorten commit convention to a permalink instead (#231)
fzn0x Jan 20, 2022
5a66650
docs: add c# port to list (#227)
JSanchezIO Jan 20, 2022
e644950
feat: use export default for locales (#249)
ST-DDT Jan 21, 2022
960d5c6
chore: delete old examples and doc folder (#155)
damienwebdev Jan 21, 2022
8544767
chore: add JSDoc comments to animal.ts (#250)
mbokinala Jan 21, 2022
53a1cf3
chore(test): migrate to vitest (#235)
Shinigami92 Jan 21, 2022
1be244a
fix: add/fix tests for date class methods accepting Date object
pkuczynski Jan 17, 2022
c98e64c
migrate test to new framework
pkuczynski Jan 21, 2022
b54e3c4
Merge branch 'main' into patch-1
pkuczynski Jan 21, 2022
f84c7f5
fix
pkuczynski Jan 21, 2022
c03bd39
missing test
pkuczynski Jan 21, 2022
1a02f5c
toISOString
pkuczynski Jan 23, 2022
64517bc
use loops for avoid test dup
pkuczynski Jan 23, 2022
69c2ac3
Merge branch 'main' into patch-1
Shinigami92 Jan 25, 2022
8926341
date !== undefined
pkuczynski Jan 25, 2022
1d7d6e2
new date
pkuczynski Jan 25, 2022
400b886
fix
pkuczynski Jan 25, 2022
b00859e
revert: needs to clone date
pkuczynski Jan 25, 2022
4012d2b
Merge branch 'main' into patch-1
pkuczynski Jan 27, 2022
4111193
Merge branch 'main' into patch-1
pkuczynski Jan 27, 2022
39826c5
Merge branch 'main' into patch-1
pkuczynski Feb 1, 2022
8ea70ca
add tests
pkuczynski Feb 1, 2022
d3139ca
fix tests
pkuczynski Feb 1, 2022
59e8793
noArgs
pkuczynski Feb 1, 2022
c479af0
remove todo
pkuczynski Feb 1, 2022
97ebab2
feedback
pkuczynski Feb 1, 2022
5d14bdf
Merge branch 'main' into patch-1
pkuczynski Feb 1, 2022
f0c492a
fix tests
pkuczynski Feb 1, 2022
edd01e3
Merge branch 'main' into patch-1
pkuczynski Mar 23, 2022
4b3f80f
Merge
pkuczynski Mar 23, 2022
dcbe48e
remove private methods
pkuczynski Mar 23, 2022
360815f
null !=
pkuczynski Mar 23, 2022
7a8c5dc
chore: apply review suggestions
Shinigami92 Mar 23, 2022
7d1c09d
docs: add jsdoc to the conversion functions
pkuczynski Mar 23, 2022
7632415
docs: add jsdoc params
pkuczynski Mar 23, 2022
720a35d
Merge branch 'main' into patch-1
Shinigami92 Mar 24, 2022
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
87 changes: 46 additions & 41 deletions src/date.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
import type { Faker } from '.';
import type { DateEntryDefinition } from './definitions';

/**
* Converts date passed as a string or Date to a Date object. If nothing passed, takes current date.
*
* @param date Date
*/
function toDate(date?: string | Date): Date {
if (date != null) {
return new Date(date instanceof Date ? date : Date.parse(date));
}

return new Date();
}

/**
* Converts date passed as a string or Date to milliseconds. If nothing passed, takes current date.
*
* @param date Date
*/
function toMilliseconds(date?: string | Date): number {
if (date != null) {
return date instanceof Date ? date.getTime() : Date.parse(date);
}

return new Date().getTime();
}

/**
* Module to generate dates.
*/
Expand Down Expand Up @@ -28,12 +54,8 @@ export class _Date {
* faker.date.past(10) // '2017-10-25T21:34:19.488Z'
* faker.date.past(10, '2020-01-01T00:00:00.000Z') // '2017-08-18T02:59:12.350Z'
*/
past(years?: number, refDate?: string): Date {
let date = new Date();
if (typeof refDate !== 'undefined') {
date = new Date(Date.parse(refDate));
}

past(years?: number, refDate?: string | Date): Date {
const date = toDate(refDate);
const range = {
min: 1000,
max: (years || 1) * 365 * 24 * 3600 * 1000,
Expand All @@ -59,12 +81,8 @@ export class _Date {
* faker.date.future(10) // '2030-11-23T09:38:28.710Z'
* faker.date.future(10, '2020-01-01T00:00:00.000Z') // '2020-12-13T22:45:10.252Z'
*/
future(years?: number, refDate?: string): Date {
let date = new Date();
if (typeof refDate !== 'undefined') {
date = new Date(Date.parse(refDate));
}

future(years?: number, refDate?: string | Date): Date {
const date = toDate(refDate);
const range = {
min: 1000,
max: (years || 1) * 365 * 24 * 3600 * 1000,
Expand All @@ -86,13 +104,12 @@ export class _Date {
* @example
* faker.date.between('2020-01-01T00:00:00.000Z', '2030-01-01T00:00:00.000Z') // '2026-05-16T02:22:53.002Z'
*/
between(from: string, to: string): Date {
const fromMilli = Date.parse(from);
const dateOffset = this.faker.datatype.number(Date.parse(to) - fromMilli);

const newDate = new Date(fromMilli + dateOffset);
between(from: string | Date, to: string | Date): Date {
const fromMs = toMilliseconds(from);
const toMs = toMilliseconds(to);
const dateOffset = this.faker.datatype.number(toMs - fromMs);

return newDate;
return new Date(fromMs + dateOffset);
}

/**
Expand All @@ -112,22 +129,18 @@ export class _Date {
* faker.date.betweens('2020-01-01T00:00:00.000Z', '2030-01-01T00:00:00.000Z', 2)
* // [ 2023-05-02T16:00:00.000Z, 2026-09-01T08:00:00.000Z ]
*/
betweens(from: string, to: string, num?: number): Date[] {
betweens(from: string | Date, to: string | Date, num?: number): Date[] {
if (typeof num === 'undefined') {
num = 3;
}
const newDates: Date[] = [];
let fromMilli = Date.parse(from);
const dateOffset = (Date.parse(to) - fromMilli) / (num + 1);
let lastDate: string | Date = from;

const dates: Date[] = [];

for (let i = 0; i < num; i++) {
// TODO @Shinigami92 2022-01-11: It may be a bug that `lastDate` is passed to parse if it's a `Date` not a `string`
// @ts-expect-error
fromMilli = Date.parse(lastDate);
lastDate = new Date(fromMilli + dateOffset);
newDates.push(lastDate);
dates.push(this.between(from, to));
}
return newDates;

return dates.sort((a, b) => a.getTime() - b.getTime());
}

/**
Expand All @@ -143,12 +156,8 @@ export class _Date {
* faker.date.recent(10) // '2022-01-29T06:12:12.829Z'
* faker.date.recent(10, '2020-01-01T00:00:00.000Z') // '2019-12-27T18:11:19.117Z'
*/
recent(days?: number, refDate?: string): Date {
let date = new Date();
if (typeof refDate !== 'undefined') {
date = new Date(Date.parse(refDate));
}

recent(days?: number, refDate?: string | Date): Date {
const date = toDate(refDate);
const range = {
min: 1000,
max: (days || 1) * 24 * 3600 * 1000,
Expand All @@ -174,12 +183,8 @@ export class _Date {
* faker.date.soon(10) // '2022-02-11T05:14:39.138Z'
* faker.date.soon(10, '2020-01-01T00:00:00.000Z') // '2020-01-01T02:40:44.990Z'
*/
soon(days?: number, refDate?: string): Date {
let date = new Date();
if (typeof refDate !== 'undefined') {
date = new Date(Date.parse(refDate));
}

soon(days?: number, refDate?: string | Date): Date {
const date = toDate(refDate);
const range = {
min: 1000,
max: (days || 1) * 24 * 3600 * 1000,
Expand Down
2 changes: 0 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,6 @@ export class Helpers {
email: this.faker.internet.email(userName),
dob: this.faker.date.past(
50,
// TODO @Shinigami92 2022-01-14: We may need to convert this to a string
// @ts-expect-error
new Date('Sat Sep 20 1992 21:35:02 GMT+0200 (CEST)')
),
phone: this.faker.phone.phoneNumber(),
Expand Down
Loading