diff options
| author | Shinigami <[email protected]> | 2022-03-25 16:51:36 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-03-25 15:51:36 +0000 |
| commit | a299c22c375538112c52ee0ad69902c87496f78c (patch) | |
| tree | e080dfb6f468a2bb4c6bb28b2442c68865bd2371 /src | |
| parent | ba3c62fca76fe2dc9fd1c8c9292c5333bf2c10a7 (diff) | |
| download | faker-a299c22c375538112c52ee0ad69902c87496f78c.tar.xz faker-a299c22c375538112c52ee0ad69902c87496f78c.zip | |
refactor: use smart eqeqeq null checks (#650)
Diffstat (limited to 'src')
| -rw-r--r-- | src/address.ts | 2 | ||||
| -rw-r--r-- | src/datatype.ts | 6 | ||||
| -rw-r--r-- | src/date.ts | 14 | ||||
| -rw-r--r-- | src/fake.ts | 4 | ||||
| -rw-r--r-- | src/faker.ts | 6 | ||||
| -rw-r--r-- | src/finance.ts | 3 | ||||
| -rw-r--r-- | src/helpers.ts | 10 | ||||
| -rw-r--r-- | src/image.ts | 4 | ||||
| -rw-r--r-- | src/image_providers/lorempixel.ts | 2 | ||||
| -rw-r--r-- | src/image_providers/unsplash.ts | 4 | ||||
| -rw-r--r-- | src/internet.ts | 2 | ||||
| -rw-r--r-- | src/lorem.ts | 15 | ||||
| -rw-r--r-- | src/random.ts | 12 | ||||
| -rw-r--r-- | src/vendor/unique.ts | 4 |
14 files changed, 40 insertions, 48 deletions
diff --git a/src/address.ts b/src/address.ts index ec137ad2..f60377ee 100644 --- a/src/address.ts +++ b/src/address.ts @@ -39,7 +39,7 @@ export class Address { */ zipCode(format?: string): string { // if zip format is not specified, use the zip format defined for the locale - if (typeof format === 'undefined') { + if (format == null) { const localeFormat = this.faker.definitions.address.postcode; if (typeof localeFormat === 'string') { format = localeFormat; diff --git a/src/datatype.ts b/src/datatype.ts index aa7139dd..ad37c3da 100644 --- a/src/datatype.ts +++ b/src/datatype.ts @@ -98,7 +98,7 @@ export class Datatype { for (const p in options) { opts[p] = options[p]; } - if (typeof opts.precision === 'undefined') { + if (opts.precision == null) { opts.precision = 0.01; } return this.faker.datatype.number(opts); @@ -122,11 +122,11 @@ export class Datatype { let min = typeof options === 'number' ? undefined : options?.min; let max = typeof options === 'number' ? options : options?.max; - if (typeof min === 'undefined' || min < minMax * -1) { + if (min == null || min < minMax * -1) { min = Date.UTC(1990, 0); } - if (typeof max === 'undefined' || max > minMax) { + if (max == null || max > minMax) { max = Date.UTC(2100, 0); } diff --git a/src/date.ts b/src/date.ts index 4a8e7827..c10a80f8 100644 --- a/src/date.ts +++ b/src/date.ts @@ -121,12 +121,8 @@ export class _Date { betweens( from: string | Date | number, to: string | Date | number, - num?: number + num: number = 3 ): Date[] { - if (typeof num === 'undefined') { - num = 3; - } - const dates: Date[] = []; while (dates.length < num) { @@ -210,12 +206,12 @@ export class _Date { const source = this.faker.definitions.date.month; let type: keyof DateEntryDefinition; if (abbr) { - if (context && typeof source['abbr_context'] !== 'undefined') { + if (context && source['abbr_context'] != null) { type = 'abbr_context'; } else { type = 'abbr'; } - } else if (context && typeof source['wide_context'] !== 'undefined') { + } else if (context && source['wide_context'] != null) { type = 'wide_context'; } else { type = 'wide'; @@ -244,12 +240,12 @@ export class _Date { const source = this.faker.definitions.date.weekday; let type: keyof DateEntryDefinition; if (abbr) { - if (context && typeof source['abbr_context'] !== 'undefined') { + if (context && source['abbr_context'] != null) { type = 'abbr_context'; } else { type = 'abbr'; } - } else if (context && typeof source['wide_context'] !== 'undefined') { + } else if (context && source['wide_context'] != null) { type = 'wide_context'; } else { type = 'wide'; diff --git a/src/fake.ts b/src/fake.ts index 4261ebeb..40f16aa7 100644 --- a/src/fake.ts +++ b/src/fake.ts @@ -81,11 +81,11 @@ export class Fake { // split the method into module and function const parts = method.split('.'); - if (typeof this.faker[parts[0]] === 'undefined') { + if (this.faker[parts[0]] == null) { throw new Error('Invalid module: ' + parts[0]); } - if (typeof this.faker[parts[0]][parts[1]] === 'undefined') { + if (this.faker[parts[0]][parts[1]] == null) { throw new Error('Invalid method: ' + parts[0] + '.' + parts[1]); } diff --git a/src/faker.ts b/src/faker.ts index e2530f16..d7ba3f80 100644 --- a/src/faker.ts +++ b/src/faker.ts @@ -96,7 +96,7 @@ export class Faker { // TODO @Shinigami92 2022-01-11: Find a way to load this even more dynamically // In a way so that we don't accidentally miss a definition Object.entries(DEFINITIONS).forEach(([t, v]) => { - if (typeof this.definitions[t] === 'undefined') { + if (this.definitions[t] == null) { this.definitions[t] = {}; } @@ -109,8 +109,8 @@ export class Faker { Object.defineProperty(this.definitions[t], p, { get: () => { if ( - typeof this.locales[this.locale][t] === 'undefined' || - typeof this.locales[this.locale][t][p] === 'undefined' + this.locales[this.locale][t] == null || + this.locales[this.locale][t][p] == null ) { // certain localization sets contain less data then others. // in the case of a missing definition, use the default localeFallback diff --git a/src/finance.ts b/src/finance.ts index 37f491fe..10e0b853 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -90,8 +90,7 @@ export class Finance { */ mask(length?: number, parens?: boolean, ellipsis?: boolean): string { // set defaults - length = - length === 0 || !length || typeof length === 'undefined' ? 4 : length; + length = length || 4; parens = parens == null ? true : parens; ellipsis = ellipsis == null ? true : ellipsis; diff --git a/src/helpers.ts b/src/helpers.ts index 2ee9faf5..286ecc41 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -351,7 +351,7 @@ export class Helpers { let tmp: number; let repetitions: number; let token = string.match(RANGE_REP_REG); - while (token !== null) { + while (token != null) { min = parseInt(token[2]); max = parseInt(token[3]); // switch min and max @@ -369,7 +369,7 @@ export class Helpers { } // Deal with repeat `{num}` token = string.match(REP_REG); - while (token !== null) { + while (token != null) { repetitions = parseInt(token[2]); string = string.slice(0, token.index) + @@ -381,7 +381,7 @@ export class Helpers { //TODO: implement for letters e.g. [0-9a-zA-Z] etc. token = string.match(RANGE_REG); - while (token !== null) { + while (token != null) { min = parseInt(token[1]); // This time we are not capturing the char before `[]` max = parseInt(token[2]); // switch min and max @@ -412,7 +412,7 @@ export class Helpers { * faker.helpers.shuffle(['a', 'b', 'c']) // [ 'b', 'c', 'a' ] */ shuffle<T>(o?: T[]): T[] { - if (typeof o === 'undefined' || o.length === 0) { + if (o == null || o.length === 0) { return o || []; } @@ -481,7 +481,7 @@ export class Helpers { string | ((substring: string, ...args: any[]) => string) > ): string { - if (typeof str === 'undefined') { + if (str == null) { return ''; } for (const p in data) { diff --git a/src/image.ts b/src/image.ts index 31f8d651..e9ae6eb7 100644 --- a/src/image.ts +++ b/src/image.ts @@ -99,11 +99,11 @@ export class Image { width = width || 640; height = height || 480; let protocol = 'http://'; - if (typeof https !== 'undefined' && https === true) { + if (https === true) { protocol = 'https://'; } let url = `${protocol}placeimg.com/${width}/${height}`; - if (typeof category !== 'undefined') { + if (category != null) { url += '/' + category; } diff --git a/src/image_providers/lorempixel.ts b/src/image_providers/lorempixel.ts index e58aa30b..310ba45a 100644 --- a/src/image_providers/lorempixel.ts +++ b/src/image_providers/lorempixel.ts @@ -66,7 +66,7 @@ export class Lorempixel { height = height || 480; let url = `https://lorempixel.com/${width}/${height}`; - if (typeof category !== 'undefined') { + if (category != null) { url += '/' + category; } diff --git a/src/image_providers/unsplash.ts b/src/image_providers/unsplash.ts index 47157eb2..dd8abd37 100644 --- a/src/image_providers/unsplash.ts +++ b/src/image_providers/unsplash.ts @@ -58,13 +58,13 @@ export class Unsplash { let url = 'https://source.unsplash.com'; - if (typeof category !== 'undefined') { + if (category != null) { url += '/category/' + category; } url += `/${width}x${height}`; - if (typeof keyword !== 'undefined') { + if (keyword != null) { const keywordFormat = /^([A-Za-z0-9].+,[A-Za-z0-9]+)$|^([A-Za-z0-9]+)$/; if (keywordFormat.test(keyword)) { url += '?' + keyword; diff --git a/src/internet.ts b/src/internet.ts index f247f06b..1ed6a837 100644 --- a/src/internet.ts +++ b/src/internet.ts @@ -375,7 +375,7 @@ export class Internet { prefix?: string ): string { len = len || 15; - if (typeof memorable === 'undefined') { + if (memorable == null) { memorable = false; } /* diff --git a/src/lorem.ts b/src/lorem.ts index 4612fe2d..dee259f3 100644 --- a/src/lorem.ts +++ b/src/lorem.ts @@ -31,7 +31,7 @@ export class Lorem { word(length?: number): string { const hasRightLength = (word: string) => word.length === length; let properLengthWords: readonly string[]; - if (typeof length === 'undefined') { + if (length == null) { properLengthWords = this.faker.definitions.lorem.words; } else { properLengthWords = @@ -49,10 +49,7 @@ export class Lorem { * faker.lorem.words() // 'qui praesentium pariatur' * faker.lorem.words(10) // 'debitis consectetur voluptatem non doloremque ipsum autem totam eum ratione' */ - words(num?: number): string { - if (typeof num === 'undefined') { - num = 3; - } + words(num: number = 3): string { const words: string[] = []; for (let i = 0; i < num; i++) { words.push(this.faker.lorem.word()); @@ -70,7 +67,7 @@ export class Lorem { * faker.lorem.sentence(5) // 'Laborum voluptatem officiis est et.' */ sentence(wordCount?: number): string { - if (typeof wordCount === 'undefined') { + if (wordCount == null) { wordCount = this.faker.datatype.number({ min: 3, max: 10 }); } @@ -105,10 +102,10 @@ export class Lorem { * // Et perspiciatis ipsam omnis.' */ sentences(sentenceCount?: number, separator?: string): string { - if (typeof sentenceCount === 'undefined') { + if (sentenceCount == null) { sentenceCount = this.faker.datatype.number({ min: 2, max: 6 }); } - if (typeof separator === 'undefined') { + if (separator == null) { separator = ' '; } const sentences: string[] = []; @@ -208,7 +205,7 @@ export class Lorem { * // Voluptate aut aut.' */ lines(lineCount?: number): string { - if (typeof lineCount === 'undefined') { + if (lineCount == null) { lineCount = this.faker.datatype.number({ min: 1, max: 5 }); } return this.faker.lorem.sentences(lineCount, '\n'); diff --git a/src/random.ts b/src/random.ts index 0658cdd5..7e410b10 100644 --- a/src/random.ts +++ b/src/random.ts @@ -322,7 +322,7 @@ export class Random { words(count?: number): string { const words: string[] = []; - if (typeof count === 'undefined') { + if (count == null) { count = this.faker.datatype.number({ min: 1, max: 3 }); } @@ -380,7 +380,7 @@ export class Random { | number | { count?: number; upcase?: boolean; bannedChars?: string[] } ): string { - if (typeof options === 'undefined') { + if (options == null) { options = { count: 1, }; @@ -388,14 +388,14 @@ export class Random { options = { count: options, }; - } else if (typeof options.count === 'undefined') { + } else if (options.count == null) { options.count = 1; } - if (typeof options.upcase === 'undefined') { + if (options.upcase == null) { options.upcase = false; } - if (typeof options.bannedChars === 'undefined') { + if (options.bannedChars == null) { options.bannedChars = []; } @@ -455,7 +455,7 @@ export class Random { count: number = 1, options: { bannedChars?: string[] } = {} ): string { - if (typeof options.bannedChars === 'undefined') { + if (options.bannedChars == null) { options.bannedChars = []; } diff --git a/src/vendor/unique.ts b/src/vendor/unique.ts index 0d6e6317..78d0fdfc 100644 --- a/src/vendor/unique.ts +++ b/src/vendor/unique.ts @@ -19,7 +19,7 @@ function defaultCompare( obj: Record<RecordKey, RecordKey>, key: RecordKey ): 0 | -1 { - if (typeof obj[key] === 'undefined') { + if (obj[key] === undefined) { return -1; } return 0; @@ -71,7 +71,7 @@ export function exec<Method extends (...parameters) => RecordKey>( opts.currentIterations = 0; } - if (typeof opts.startTime === 'undefined') { + if (opts.startTime == null) { opts.startTime = new Date().getTime(); } |
