Skip to content

Commit

Permalink
chore: fix some lint warnings (#613)
Browse files Browse the repository at this point in the history
Co-authored-by: Shinigami <chrissi92@hotmail.de>
  • Loading branch information
ST-DDT and Shinigami92 committed Mar 15, 2022
1 parent 09487b6 commit 5cb74b1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 40 deletions.
59 changes: 31 additions & 28 deletions src/date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '.';
import type { DateEntryDefinition } from './definitions';

/**
* Module to generate dates.
Expand Down Expand Up @@ -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]);
}

/**
Expand All @@ -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]);
}
}
4 changes: 4 additions & 0 deletions src/definitions/utils.ts
Original file line number Diff line number Diff line change
@@ -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>[]) {
Expand Down
4 changes: 2 additions & 2 deletions src/fake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions src/finance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 9 additions & 7 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/fake.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('fake', () => {

it('does not allow undefined parameters', () => {
expect(() =>
// @ts-expect-error
// @ts-expect-error: The parameter is required
faker.fake()
).toThrowError(Error('string parameter is required!'));
});
Expand Down

0 comments on commit 5cb74b1

Please sign in to comment.