Skip to content

Commit 2f0ad2e

Browse files
authored
Merge branch 'main' into fix-various-todos
2 parents b20f2e9 + 301a6d2 commit 2f0ad2e

File tree

9 files changed

+189
-192
lines changed

9 files changed

+189
-192
lines changed

.github/.codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ coverage:
1313
threshold: 2%
1414
patch:
1515
default:
16-
target: 95%
16+
target: 40%
1717
threshold: 1%

src/date.ts

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
import type { Faker } from '.';
22
import type { DateEntryDefinition } from './definitions';
33

4+
/**
5+
* Converts date passed as a string or Date to a Date object. If nothing passed, takes current date.
6+
*
7+
* @param date Date
8+
*/
9+
function toDate(date?: string | Date): Date {
10+
if (date != null) {
11+
return new Date(date instanceof Date ? date : Date.parse(date));
12+
}
13+
14+
return new Date();
15+
}
16+
17+
/**
18+
* Converts date passed as a string or Date to milliseconds. If nothing passed, takes current date.
19+
*
20+
* @param date Date
21+
*/
22+
function toMilliseconds(date?: string | Date): number {
23+
if (date != null) {
24+
return date instanceof Date ? date.getTime() : Date.parse(date);
25+
}
26+
27+
return new Date().getTime();
28+
}
29+
430
/**
531
* Module to generate dates.
632
*/
@@ -28,12 +54,8 @@ export class _Date {
2854
* faker.date.past(10) // '2017-10-25T21:34:19.488Z'
2955
* faker.date.past(10, '2020-01-01T00:00:00.000Z') // '2017-08-18T02:59:12.350Z'
3056
*/
31-
past(years?: number, refDate?: string): Date {
32-
let date = new Date();
33-
if (typeof refDate !== 'undefined') {
34-
date = new Date(Date.parse(refDate));
35-
}
36-
57+
past(years?: number, refDate?: string | Date): Date {
58+
const date = toDate(refDate);
3759
const range = {
3860
min: 1000,
3961
max: (years || 1) * 365 * 24 * 3600 * 1000,
@@ -59,12 +81,8 @@ export class _Date {
5981
* faker.date.future(10) // '2030-11-23T09:38:28.710Z'
6082
* faker.date.future(10, '2020-01-01T00:00:00.000Z') // '2020-12-13T22:45:10.252Z'
6183
*/
62-
future(years?: number, refDate?: string): Date {
63-
let date = new Date();
64-
if (typeof refDate !== 'undefined') {
65-
date = new Date(Date.parse(refDate));
66-
}
67-
84+
future(years?: number, refDate?: string | Date): Date {
85+
const date = toDate(refDate);
6886
const range = {
6987
min: 1000,
7088
max: (years || 1) * 365 * 24 * 3600 * 1000,
@@ -86,13 +104,12 @@ export class _Date {
86104
* @example
87105
* faker.date.between('2020-01-01T00:00:00.000Z', '2030-01-01T00:00:00.000Z') // '2026-05-16T02:22:53.002Z'
88106
*/
89-
between(from: string, to: string): Date {
90-
const fromMilli = Date.parse(from);
91-
const dateOffset = this.faker.datatype.number(Date.parse(to) - fromMilli);
92-
93-
const newDate = new Date(fromMilli + dateOffset);
107+
between(from: string | Date, to: string | Date): Date {
108+
const fromMs = toMilliseconds(from);
109+
const toMs = toMilliseconds(to);
110+
const dateOffset = this.faker.datatype.number(toMs - fromMs);
94111

95-
return newDate;
112+
return new Date(fromMs + dateOffset);
96113
}
97114

98115
/**
@@ -112,22 +129,18 @@ export class _Date {
112129
* faker.date.betweens('2020-01-01T00:00:00.000Z', '2030-01-01T00:00:00.000Z', 2)
113130
* // [ 2023-05-02T16:00:00.000Z, 2026-09-01T08:00:00.000Z ]
114131
*/
115-
betweens(from: string, to: string, num?: number): Date[] {
132+
betweens(from: string | Date, to: string | Date, num?: number): Date[] {
116133
if (typeof num === 'undefined') {
117134
num = 3;
118135
}
119-
const newDates: Date[] = [];
120-
let fromMilli = Date.parse(from);
121-
const dateOffset = (Date.parse(to) - fromMilli) / (num + 1);
122-
let lastDate: string | Date = from;
136+
137+
const dates: Date[] = [];
138+
123139
for (let i = 0; i < num; i++) {
124-
// TODO @Shinigami92 2022-01-11: It may be a bug that `lastDate` is passed to parse if it's a `Date` not a `string`
125-
// @ts-expect-error
126-
fromMilli = Date.parse(lastDate);
127-
lastDate = new Date(fromMilli + dateOffset);
128-
newDates.push(lastDate);
140+
dates.push(this.between(from, to));
129141
}
130-
return newDates;
142+
143+
return dates.sort((a, b) => a.getTime() - b.getTime());
131144
}
132145

133146
/**
@@ -143,12 +156,8 @@ export class _Date {
143156
* faker.date.recent(10) // '2022-01-29T06:12:12.829Z'
144157
* faker.date.recent(10, '2020-01-01T00:00:00.000Z') // '2019-12-27T18:11:19.117Z'
145158
*/
146-
recent(days?: number, refDate?: string): Date {
147-
let date = new Date();
148-
if (typeof refDate !== 'undefined') {
149-
date = new Date(Date.parse(refDate));
150-
}
151-
159+
recent(days?: number, refDate?: string | Date): Date {
160+
const date = toDate(refDate);
152161
const range = {
153162
min: 1000,
154163
max: (days || 1) * 24 * 3600 * 1000,
@@ -174,12 +183,8 @@ export class _Date {
174183
* faker.date.soon(10) // '2022-02-11T05:14:39.138Z'
175184
* faker.date.soon(10, '2020-01-01T00:00:00.000Z') // '2020-01-01T02:40:44.990Z'
176185
*/
177-
soon(days?: number, refDate?: string): Date {
178-
let date = new Date();
179-
if (typeof refDate !== 'undefined') {
180-
date = new Date(Date.parse(refDate));
181-
}
182-
186+
soon(days?: number, refDate?: string | Date): Date {
187+
const date = toDate(refDate);
183188
const range = {
184189
min: 1000,
185190
max: (days || 1) * 24 * 3600 * 1000,

src/fake.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ export class Fake {
116116
// replace the found tag with the returned fake value
117117
res = str.replace('{{' + token + '}}', result);
118118

119+
if (res === '') {
120+
return '';
121+
}
122+
119123
// return the response recursively until we are done finding all tags
120124
return this.fake(res);
121125
}

src/finance.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export class Finance {
249249
/**
250250
* Generates a random credit card number.
251251
*
252-
* @param provider The (lowercase) name of the provider or the format used to generate one.
252+
* @param provider The name of the provider (case insensitive) or the format used to generate one.
253253
*
254254
* @example
255255
* faker.finance.creditCardNumber() // '4427163488668'
@@ -259,8 +259,9 @@ export class Finance {
259259
creditCardNumber(provider = ''): string {
260260
let format: string;
261261
const localeFormat = this.faker.definitions.finance.credit_card;
262-
if (provider in localeFormat) {
263-
format = this.faker.random.arrayElement(localeFormat[provider]);
262+
const normalizedProvider = provider.toLowerCase();
263+
if (normalizedProvider in localeFormat) {
264+
format = this.faker.random.arrayElement(localeFormat[normalizedProvider]);
264265
} else if (provider.match(/#/)) {
265266
// The user chose an optional scheme
266267
format = provider;

src/helpers.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,6 @@ export class Helpers {
594594
email: this.faker.internet.email(userName),
595595
dob: this.faker.date.past(
596596
50,
597-
// TODO @Shinigami92 2022-01-14: We may need to convert this to a string
598-
// @ts-expect-error
599597
new Date('Sat Sep 20 1992 21:35:02 GMT+0200 (CEST)')
600598
),
601599
phone: this.faker.phone.phoneNumber(),

src/vehicle.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import type { Faker } from '.';
2-
import type { Fake } from './fake';
3-
4-
let fake: Fake['fake'];
52

63
/**
74
* Module to generate vehicle related entries.
85
*/
96
export class Vehicle {
107
constructor(private readonly faker: Faker) {
11-
fake = faker.fake;
12-
138
// Bind `this` so namespaced is working correctly
149
for (const name of Object.getOwnPropertyNames(Vehicle.prototype)) {
1510
if (name === 'constructor' || typeof this[name] !== 'function') {
@@ -26,7 +21,7 @@ export class Vehicle {
2621
* faker.vehicle.vehicle() // 'BMW Explorer'
2722
*/
2823
vehicle(): string {
29-
return fake('{{vehicle.manufacturer}} {{vehicle.model}}');
24+
return `${this.manufacturer()} ${this.model()}`;
3025
}
3126

3227
/**
@@ -98,7 +93,7 @@ export class Vehicle {
9893
* faker.vehicle.color() // 'red'
9994
*/
10095
color(): string {
101-
return fake('{{commerce.color}}');
96+
return this.faker.commerce.color();
10297
}
10398

10499
/**

0 commit comments

Comments
 (0)