Skip to content

Commit 5cb74b1

Browse files
ST-DDTShinigami92
andauthored
chore: fix some lint warnings (#613)
Co-authored-by: Shinigami <[email protected]>
1 parent 09487b6 commit 5cb74b1

File tree

7 files changed

+54
-40
lines changed

7 files changed

+54
-40
lines changed

src/date.ts

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '.';
2+
import type { DateEntryDefinition } from './definitions';
23

34
/**
45
* Module to generate dates.
@@ -205,23 +206,24 @@ export class _Date {
205206
* faker.date.month({ abbr: true, context: true }) // 'Sep'
206207
*/
207208
month(options?: { abbr?: boolean; context?: boolean }): string {
208-
options = options || {};
209+
const abbr = options?.abbr ?? false;
210+
const context = options?.context ?? false;
209211

210-
let type = 'wide';
211-
if (options.abbr) {
212-
type = 'abbr';
213-
}
214-
if (
215-
options.context &&
216-
typeof this.faker.definitions.date.month[type + '_context'] !==
217-
'undefined'
218-
) {
219-
type += '_context';
212+
const source = this.faker.definitions.date.month;
213+
let type: keyof DateEntryDefinition;
214+
if (abbr) {
215+
if (context && typeof source['abbr_context'] !== 'undefined') {
216+
type = 'abbr_context';
217+
} else {
218+
type = 'abbr';
219+
}
220+
} else if (context && typeof source['wide_context'] !== 'undefined') {
221+
type = 'wide_context';
222+
} else {
223+
type = 'wide';
220224
}
221225

222-
const source = this.faker.definitions.date.month[type];
223-
224-
return this.faker.random.arrayElement(source);
226+
return this.faker.random.arrayElement(source[type]);
225227
}
226228

227229
/**
@@ -238,22 +240,23 @@ export class _Date {
238240
* faker.date.weekday({ abbr: true, context: true }) // 'Fri'
239241
*/
240242
weekday(options?: { abbr?: boolean; context?: boolean }): string {
241-
options = options || {};
243+
const abbr = options?.abbr ?? false;
244+
const context = options?.context ?? false;
242245

243-
let type = 'wide';
244-
if (options.abbr) {
245-
type = 'abbr';
246-
}
247-
if (
248-
options.context &&
249-
typeof this.faker.definitions.date.weekday[type + '_context'] !==
250-
'undefined'
251-
) {
252-
type += '_context';
246+
const source = this.faker.definitions.date.weekday;
247+
let type: keyof DateEntryDefinition;
248+
if (abbr) {
249+
if (context && typeof source['abbr_context'] !== 'undefined') {
250+
type = 'abbr_context';
251+
} else {
252+
type = 'abbr';
253+
}
254+
} else if (context && typeof source['wide_context'] !== 'undefined') {
255+
type = 'wide_context';
256+
} else {
257+
type = 'wide';
253258
}
254259

255-
const source = this.faker.definitions.date.weekday[type];
256-
257-
return this.faker.random.arrayElement(source);
260+
return this.faker.random.arrayElement(source[type]);
258261
}
259262
}

src/definitions/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// https://stackoverflow.com/a/53395649/4573065
22
export type AllOf<T> = ['Needs to be all of', T];
33

4+
/**
5+
* Creates a function that requires all keys of the generic type to be used as parameters.
6+
* The function itself will return the given parameters.
7+
*/
48
export function allOf<T>(): <U extends T[]>(
59
...array: U & ([T] extends [U[number]] ? unknown : AllOf<T>[])
610
) => U & ([T] extends [U[number]] ? unknown : AllOf<T>[]) {

src/fake.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ export class Fake {
9090
}
9191

9292
// assign the function from the module.function namespace
93-
let fn: (args?: any) => string = this.faker[parts[0]][parts[1]];
93+
let fn: (args?: unknown) => string = this.faker[parts[0]][parts[1]];
9494
fn = fn.bind(this);
9595

9696
// If parameters are populated here, they are always going to be of string type
9797
// since we might actually be dealing with an object or array,
9898
// we always attempt to the parse the incoming parameters into JSON
99-
let params: any;
99+
let params: unknown;
100100
// Note: we experience a small performance hit here due to JSON.parse try / catch
101101
// If anyone actually needs to optimize this specific code path, please open a support issue on github
102102
try {

src/finance.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,7 @@ export class Finance {
347347

348348
let s = '';
349349
let count = 0;
350-
for (let b = 0; b < ibanFormat.bban.length; b++) {
351-
const bban = ibanFormat.bban[b];
350+
for (const bban of ibanFormat.bban) {
352351
let c = bban.count;
353352
count += bban.count;
354353
while (c > 0) {

src/helpers.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,10 @@ export class Helpers {
342342
const RANGE_REP_REG = /(.)\{(\d+)\,(\d+)\}/;
343343
const REP_REG = /(.)\{(\d+)\}/;
344344
const RANGE_REG = /\[(\d+)\-(\d+)\]/;
345-
let min, max, tmp, repetitions;
345+
let min: number;
346+
let max: number;
347+
let tmp: number;
348+
let repetitions: number;
346349
let token = string.match(RANGE_REP_REG);
347350
while (token !== null) {
348351
min = parseInt(token[2]);
@@ -434,9 +437,9 @@ export class Helpers {
434437
* faker.helpers.uniqueArray(faker.definitions.name.first_name, 6)
435438
* faker.helpers.uniqueArray(["Hello", "World", "Goodbye"], 2)
436439
*/
437-
uniqueArray<T>(source: T[] | (() => T), length: number): T[] {
440+
uniqueArray<T>(source: readonly T[] | (() => T), length: number): T[] {
438441
if (Array.isArray(source)) {
439-
const set = new Set(source);
442+
const set = new Set<T>(source);
440443
const array = Array.from(set);
441444
return this.faker.helpers.shuffle(array).splice(0, length);
442445
}
@@ -447,11 +450,10 @@ export class Helpers {
447450
set.add(source());
448451
}
449452
}
450-
} finally {
451-
// TODO @Shinigami92 2022-01-21: Check what to do here
452-
// eslint-disable-next-line no-unsafe-finally
453-
return Array.from(set);
453+
} catch {
454+
// Ignore
454455
}
456+
return Array.from(set);
455457
}
456458

457459
/**

src/system.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ const commonMimeTypes = [
1414
'text/html',
1515
];
1616

17+
/**
18+
* Converts the given set to an array.
19+
*
20+
* @param set The set to convert.
21+
*/
22+
// TODO ST-DDT 2022-03-11: Replace with Array.from(Set)
1723
function setToArray<T>(set: Set<T>): T[] {
1824
// shortcut if Array.from is available
1925
if (Array.from) {

test/fake.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('fake', () => {
3030

3131
it('does not allow undefined parameters', () => {
3232
expect(() =>
33-
// @ts-expect-error
33+
// @ts-expect-error: The parameter is required
3434
faker.fake()
3535
).toThrowError(Error('string parameter is required!'));
3636
});

0 commit comments

Comments
 (0)