Skip to content

Commit

Permalink
chore: use explicit-module-boundary-types (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Jan 30, 2022
1 parent 730ca6a commit d4125e2
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 24 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = defineConfig({

'@typescript-eslint/ban-ts-comment': 'warn',
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/explicit-module-boundary-types': 'error',
'@typescript-eslint/no-inferrable-types': [
'error',
{ ignoreParameters: true },
Expand Down
8 changes: 4 additions & 4 deletions src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Address {
* @method faker.address.zipCode
* @param format
*/
zipCode(format?: string) {
zipCode(format?: string): string {
// if zip format is not specified, use the zip format defined for the locale
if (typeof format === 'undefined') {
const localeFormat = this.faker.definitions.address.postcode;
Expand All @@ -70,7 +70,7 @@ export class Address {
* @method faker.address.zipCodeByState
* @param state
*/
zipCodeByState(state: string) {
zipCodeByState(state: string): string | number {
const zipRange = this.faker.definitions.address.postcode_by_state[state];
if (zipRange) {
return this.faker.datatype.number(zipRange);
Expand All @@ -94,7 +94,7 @@ export class Address {
* @method faker.address.city
* @param format
*/
city(format?: string | number) {
city(format?: string | number): string {
const formats = [
'{{address.cityPrefix}} {{name.firstName}}{{address.citySuffix}}',
'{{address.cityPrefix}} {{name.firstName}}',
Expand Down Expand Up @@ -353,7 +353,7 @@ export class Address {
* @method faker.address.direction
* @param useAbbr return direction abbreviation. defaults to false
*/
direction(useAbbr: boolean = false) {
direction(useAbbr: boolean = false): string {
if (!useAbbr) {
return this.faker.random.arrayElement(
this.faker.definitions.address.direction
Expand Down
108 changes: 103 additions & 5 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,103 @@
import type { Faker } from '.';

export interface Card {
name: string;
username: string;
email: string;
address: {
streetA: string;
streetB: string;
streetC: string;
streetD: string;
city: string;
state: string;
country: string;
zipcode: string;
geo: {
lat: string;
lng: string;
};
};
phone: string;
website: string;
company: {
name: string;
catchPhrase: string;
bs: string;
};
posts: Array<{
words: string;
sentence: string;
sentences: string;
paragraph: string;
}>;
accountHistory: Array<{
amount: string;
date: Date;
business: string;
name: string;
type: string;
account: string;
}>;
}

export interface ContextualCard {
name: string;
username: string;
avatar: string;
email: string;
dob: Date;
phone: string;
address: {
street: string;
suite: string;
city: string;
zipcode: string;
geo: {
lat: string;
lng: string;
};
};
website: string;
company: {
name: string;
catchPhrase: string;
bs: string;
};
}

export interface UserCard {
name: string;
username: string;
email: string;
address: {
street: string;
suite: string;
city: string;
zipcode: string;
geo: {
lat: string;
lng: string;
};
};
phone: string;
website: string;
company: {
name: string;
catchPhrase: string;
bs: string;
};
}

export interface Transaction {
amount: string;
date: Date;
business: string;
name: string;
type: string;
account: string;
}

export class Helpers {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
Expand Down Expand Up @@ -160,7 +258,7 @@ export class Helpers {
* @param string
* @param num
*/
repeatString(string, num = 0): string {
repeatString(string: string, num = 0): string {
let text = '';
for (let i = 0; i < num; i++) {
text += string.toString();
Expand Down Expand Up @@ -323,7 +421,7 @@ export class Helpers {
*
* @method faker.helpers.createCard
*/
createCard() {
createCard(): Card {
return {
name: this.faker.name.findName(),
username: this.faker.internet.userName(),
Expand Down Expand Up @@ -382,7 +480,7 @@ export class Helpers {
*
* @method faker.helpers.contextualCard
*/
contextualCard() {
contextualCard(): ContextualCard {
const name = this.faker.name.firstName();
const userName = this.faker.internet.userName(name);
return {
Expand Down Expand Up @@ -421,7 +519,7 @@ export class Helpers {
*
* @method faker.helpers.userCard
*/
userCard() {
userCard(): UserCard {
return {
name: this.faker.name.findName(),
username: this.faker.internet.userName(),
Expand Down Expand Up @@ -451,7 +549,7 @@ export class Helpers {
*
* @method faker.helpers.createTransaction
*/
createTransaction() {
createTransaction(): Transaction {
return {
amount: this.faker.finance.amount(),
date: new Date(2012, 1, 2), // TODO: add a ranged date method
Expand Down
8 changes: 6 additions & 2 deletions src/iban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ export = {
// @ts-expect-error
match.toUpperCase().charCodeAt(0) - 55
),
mod97: (digitStr): number => {
mod97: (digitStr: string): number => {
let m = 0;
for (let i = 0; i < digitStr.length; i++) {
m = (m * 10 + (digitStr[i] | 0)) % 97;
m =
(m * 10 +
// @ts-expect-error: We need to convert this properly
(digitStr[i] | 0)) %
97;
}
return m;
},
Expand Down
2 changes: 1 addition & 1 deletion src/image_providers/unsplash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Unsplash {
*
* @method faker.image.unsplash.avatar
*/
avatar() {
avatar(): string {
return this.faker.internet.avatar();
}

Expand Down
2 changes: 1 addition & 1 deletion src/internet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class Internet {
* @param firstName
* @param lastName
*/
exampleEmail(firstName?: string, lastName?: string) {
exampleEmail(firstName?: string, lastName?: string): string {
const provider = this.faker.random.arrayElement(
this.faker.definitions.internet.example_email
);
Expand Down
8 changes: 3 additions & 5 deletions src/lorem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class Lorem {
* @param sentenceCount defaults to a random number between 2 and 6
* @param separator defaults to `' '`
*/
sentences(sentenceCount?: number, separator?: string) {
sentences(sentenceCount?: number, separator?: string): string {
if (typeof sentenceCount === 'undefined') {
sentenceCount = this.faker.datatype.number({ min: 2, max: 6 });
}
Expand Down Expand Up @@ -137,10 +137,8 @@ export class Lorem {
* @method faker.lorem.text
* @param times
*/
// TODO @Shinigami92 2022-01-11: Is this a function-name alias?
// Or can we just remove the `loremText`?
// TODO @Shinigami92 2022-01-11: `times` is not in use
text = function loremText(times?: number) {
text(times?: number): string {
const loremMethods = [
'lorem.word',
'lorem.words',
Expand All @@ -152,7 +150,7 @@ export class Lorem {
];
const randomLoremMethod = this.faker.random.arrayElement(loremMethods);
return this.faker.fake('{{' + randomLoremMethod + '}}');
};
}

/**
* Returns lines of lorem separated by `'\n'`
Expand Down
6 changes: 3 additions & 3 deletions src/mersenne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Mersenne {
}
}

rand(max?: number, min?: number) {
rand(max?: number, min?: number): number {
// TODO @Shinigami92 2022-01-11: This is buggy, cause if min is not passed but only max,
// then min will be undefined and this result in NaN for the whole function
if (max === undefined) {
Expand All @@ -26,15 +26,15 @@ export class Mersenne {
return Math.floor(this.gen.genrand_real2() * (max - min) + min);
}

seed(S: number) {
seed(S: number): void {
if (typeof S != 'number') {
throw new Error('seed(S) must take numeric argument; is ' + typeof S);
}

this.gen.init_genrand(S);
}

seed_array(A) {
seed_array(A: number[]): void {
if (typeof A != 'object') {
throw new Error(
'seed_array(A) must take array of numbers; is ' + typeof A
Expand Down
4 changes: 2 additions & 2 deletions src/phone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Phone {
* @param format
* @memberOf faker.phone
*/
phoneNumber(format?: string) {
phoneNumber(format?: string): string {
format ||= this.faker.phone.phoneFormats();
return this.faker.helpers.replaceSymbolWithNumber(format);
}
Expand All @@ -31,7 +31,7 @@ export class Phone {
* @param phoneFormatsArrayIndex
* @memberOf faker.phone
*/
phoneNumberFormat(phoneFormatsArrayIndex: number = 0) {
phoneNumberFormat(phoneFormatsArrayIndex: number = 0): string {
return this.faker.helpers.replaceSymbolWithNumber(
this.faker.definitions.phone_number.formats[phoneFormatsArrayIndex]
);
Expand Down
1 change: 1 addition & 0 deletions src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class Random {
* @param object
* @param field
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
objectElement(object: any = { foo: 'bar', too: 'car' }, field?: string) {
const array = Object.keys(object);
const key = this.faker.random.arrayElement(array);
Expand Down
2 changes: 1 addition & 1 deletion src/vendor/user-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type { Faker } from '..';

export type Arch = 'lin' | 'mac' | 'win';

export function generate(faker: Faker) {
export function generate(faker: Faker): string {
function rnd(
a?: string[] | number | Record<string, number>,
b?: number
Expand Down

0 comments on commit d4125e2

Please sign in to comment.