diff options
| author | ST-DDT <[email protected]> | 2022-03-15 19:16:56 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-03-15 19:16:56 +0100 |
| commit | 5cb74b1bf31f44311b4ee54ea320b81f68879f07 (patch) | |
| tree | bfe4e31ed4384be8b7b47826cf1ceb98227e9100 /src | |
| parent | 09487b6b3a6e6cc3de0303851b9913ecdf1390dc (diff) | |
| download | faker-5cb74b1bf31f44311b4ee54ea320b81f68879f07.tar.xz faker-5cb74b1bf31f44311b4ee54ea320b81f68879f07.zip | |
chore: fix some lint warnings (#613)
Co-authored-by: Shinigami <[email protected]>
Diffstat (limited to 'src')
| -rw-r--r-- | src/date.ts | 59 | ||||
| -rw-r--r-- | src/definitions/utils.ts | 4 | ||||
| -rw-r--r-- | src/fake.ts | 4 | ||||
| -rw-r--r-- | src/finance.ts | 3 | ||||
| -rw-r--r-- | src/helpers.ts | 16 | ||||
| -rw-r--r-- | src/system.ts | 6 |
6 files changed, 53 insertions, 39 deletions
diff --git a/src/date.ts b/src/date.ts index 818a946a..dcc23600 100644 --- a/src/date.ts +++ b/src/date.ts @@ -1,4 +1,5 @@ import type { Faker } from '.'; +import type { DateEntryDefinition } from './definitions'; /** * Module to generate dates. @@ -205,23 +206,24 @@ export class _Date { * faker.date.month({ abbr: true, context: true }) // 'Sep' */ month(options?: { abbr?: boolean; context?: boolean }): string { - options = options || {}; + const abbr = options?.abbr ?? false; + const context = options?.context ?? false; - let type = 'wide'; - if (options.abbr) { - type = 'abbr'; - } - if ( - options.context && - typeof this.faker.definitions.date.month[type + '_context'] !== - 'undefined' - ) { - type += '_context'; + const source = this.faker.definitions.date.month; + let type: keyof DateEntryDefinition; + if (abbr) { + if (context && typeof source['abbr_context'] !== 'undefined') { + type = 'abbr_context'; + } else { + type = 'abbr'; + } + } else if (context && typeof source['wide_context'] !== 'undefined') { + type = 'wide_context'; + } else { + type = 'wide'; } - const source = this.faker.definitions.date.month[type]; - - return this.faker.random.arrayElement(source); + return this.faker.random.arrayElement(source[type]); } /** @@ -238,22 +240,23 @@ export class _Date { * faker.date.weekday({ abbr: true, context: true }) // 'Fri' */ weekday(options?: { abbr?: boolean; context?: boolean }): string { - options = options || {}; + const abbr = options?.abbr ?? false; + const context = options?.context ?? false; - let type = 'wide'; - if (options.abbr) { - type = 'abbr'; - } - if ( - options.context && - typeof this.faker.definitions.date.weekday[type + '_context'] !== - 'undefined' - ) { - type += '_context'; + const source = this.faker.definitions.date.weekday; + let type: keyof DateEntryDefinition; + if (abbr) { + if (context && typeof source['abbr_context'] !== 'undefined') { + type = 'abbr_context'; + } else { + type = 'abbr'; + } + } else if (context && typeof source['wide_context'] !== 'undefined') { + type = 'wide_context'; + } else { + type = 'wide'; } - const source = this.faker.definitions.date.weekday[type]; - - return this.faker.random.arrayElement(source); + return this.faker.random.arrayElement(source[type]); } } diff --git a/src/definitions/utils.ts b/src/definitions/utils.ts index b7b826a1..03764c1d 100644 --- a/src/definitions/utils.ts +++ b/src/definitions/utils.ts @@ -1,6 +1,10 @@ // https://stackoverflow.com/a/53395649/4573065 export type AllOf<T> = ['Needs to be all of', T]; +/** + * Creates a function that requires all keys of the generic type to be used as parameters. + * The function itself will return the given parameters. + */ export function allOf<T>(): <U extends T[]>( ...array: U & ([T] extends [U[number]] ? unknown : AllOf<T>[]) ) => U & ([T] extends [U[number]] ? unknown : AllOf<T>[]) { diff --git a/src/fake.ts b/src/fake.ts index d56b4794..726bf896 100644 --- a/src/fake.ts +++ b/src/fake.ts @@ -90,13 +90,13 @@ export class Fake { } // assign the function from the module.function namespace - let fn: (args?: any) => string = this.faker[parts[0]][parts[1]]; + let fn: (args?: unknown) => string = this.faker[parts[0]][parts[1]]; fn = fn.bind(this); // If parameters are populated here, they are always going to be of string type // since we might actually be dealing with an object or array, // we always attempt to the parse the incoming parameters into JSON - let params: any; + let params: unknown; // Note: we experience a small performance hit here due to JSON.parse try / catch // If anyone actually needs to optimize this specific code path, please open a support issue on github try { diff --git a/src/finance.ts b/src/finance.ts index a25f6443..ae70728e 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -347,8 +347,7 @@ export class Finance { let s = ''; let count = 0; - for (let b = 0; b < ibanFormat.bban.length; b++) { - const bban = ibanFormat.bban[b]; + for (const bban of ibanFormat.bban) { let c = bban.count; count += bban.count; while (c > 0) { diff --git a/src/helpers.ts b/src/helpers.ts index 0f970dba..91111936 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -342,7 +342,10 @@ export class Helpers { const RANGE_REP_REG = /(.)\{(\d+)\,(\d+)\}/; const REP_REG = /(.)\{(\d+)\}/; const RANGE_REG = /\[(\d+)\-(\d+)\]/; - let min, max, tmp, repetitions; + let min: number; + let max: number; + let tmp: number; + let repetitions: number; let token = string.match(RANGE_REP_REG); while (token !== null) { min = parseInt(token[2]); @@ -434,9 +437,9 @@ export class Helpers { * faker.helpers.uniqueArray(faker.definitions.name.first_name, 6) * faker.helpers.uniqueArray(["Hello", "World", "Goodbye"], 2) */ - uniqueArray<T>(source: T[] | (() => T), length: number): T[] { + uniqueArray<T>(source: readonly T[] | (() => T), length: number): T[] { if (Array.isArray(source)) { - const set = new Set(source); + const set = new Set<T>(source); const array = Array.from(set); return this.faker.helpers.shuffle(array).splice(0, length); } @@ -447,11 +450,10 @@ export class Helpers { set.add(source()); } } - } finally { - // TODO @Shinigami92 2022-01-21: Check what to do here - // eslint-disable-next-line no-unsafe-finally - return Array.from(set); + } catch { + // Ignore } + return Array.from(set); } /** diff --git a/src/system.ts b/src/system.ts index 33413a23..afaed741 100644 --- a/src/system.ts +++ b/src/system.ts @@ -14,6 +14,12 @@ const commonMimeTypes = [ 'text/html', ]; +/** + * Converts the given set to an array. + * + * @param set The set to convert. + */ +// TODO ST-DDT 2022-03-11: Replace with Array.from(Set) function setToArray<T>(set: Set<T>): T[] { // shortcut if Array.from is available if (Array.from) { |
