diff --git a/cypress/e2e/api.cy.ts b/cypress/e2e/api.cy.ts index ec21d3d3dc7..a79c19579ea 100644 --- a/cypress/e2e/api.cy.ts +++ b/cypress/e2e/api.cy.ts @@ -32,11 +32,11 @@ describe('API Test', () => { const text = $el.find('a').text(); const link = $el.find('a').attr('href'); - cy.visit(`/api/${link}`); - - cy.get('h2').should('include.text', text); - cy.get('h1').should('not.include.text', 'PAGE NOT FOUND'); - cy.go('back'); + cy.request(`/api/${link}`).should((response) => { + expect(response.status).to.eq(200); + expect(response.body).to.include(text); + expect(response.body).to.not.include('PAGE NOT FOUND'); + }); }); }); }); diff --git a/src/modules/location/index.ts b/src/modules/location/index.ts index ab472658b3d..5fe542cbbc4 100644 --- a/src/modules/location/index.ts +++ b/src/modules/location/index.ts @@ -20,7 +20,8 @@ export class LocationModule { * Generates random zip code from specified format. If format is not specified, * the locale's zip format is used. * - * @param format The optional format used to generate the the zip code. + * @param options The format used to generate the the zip code or an options object. Defaults to `{}`. + * @param options.format The optional format used to generate the the zip code. * By default, a random format is used from the locale zip formats. * * @see faker.helpers.replaceSymbols() @@ -31,28 +32,42 @@ export class LocationModule { * * @since 8.0.0 */ - zipCode(format?: string): string { - // if zip format is not specified, use the zip format defined for the locale - if (format == null) { - const localeFormat = this.faker.definitions.location.postcode; - if (typeof localeFormat === 'string') { - format = localeFormat; - } else { - format = this.faker.helpers.arrayElement(localeFormat); - } + zipCode( + options: + | string + | { + /** + * The optional format used to generate the the zip code. + * + * @default faker.definitions.location.postcode + */ + format?: string; + } = {} + ): string { + if (typeof options === 'string') { + options = { format: options }; + } + + let { format = this.faker.definitions.location.postcode } = options; + if (typeof format === 'string') { + format = [format]; } + format = this.faker.helpers.arrayElement(format); + return this.faker.helpers.replaceSymbols(format); } /** - * Generates random zip code from state abbreviation. If state abbreviation is - * not specified, a random zip code is generated according to the locale's zip format. + * Generates random zip code from state abbreviation. + * * Only works for locales with postcode_by_state definition. If a locale does not * have a postcode_by_state definition, a random zip code is generated according * to the locale's zip format. * - * @param state The abbreviation of the state to generate the zip code for. + * @param options A state abbreviation or an options object. Defaults to `{}`. + * @param options.state The abbreviation of the state to generate the zip code for. + * If not specified, a random zip code is generated according to the locale's zip format. * * @example * fakerUS.location.zipCodeByState("AK") // '99595' @@ -60,7 +75,23 @@ export class LocationModule { * * @since 8.0.0 */ - zipCodeByState(state: string): string { + zipCodeByState( + options: + | string + | { + /** + * The abbreviation of the state to generate the zip code for. + * If not specified, a random zip code is generated according to the locale's zip format. + */ + state?: string; + } = {} + ): string { + if (typeof options === 'string') { + options = { state: options }; + } + + const { state } = options; + const zipRange = this.faker.definitions.location.postcode_by_state?.[state]; if (zipRange) { return String(this.faker.number.int(zipRange)); @@ -142,17 +173,35 @@ export class LocationModule { /** * Generates a random localized street address. * - * @param useFullAddress When true this will generate a full address. + * @param options Whether to use a full address or an options object. Defaults to `{}`. + * @param options.useFullAddress When true this will generate a full address. * Otherwise it will just generate a street address. * * @example * faker.location.streetAddress() // '0917 O'Conner Estates' * faker.location.streetAddress(false) // '34830 Erdman Hollow' * faker.location.streetAddress(true) // '3393 Ronny Way Apt. 742' + * faker.location.streetAddress({ useFullAddress: true }) // '7917 Miller Park Apt. 410' * * @since 8.0.0 */ - streetAddress(useFullAddress: boolean = false): string { + streetAddress( + options: + | boolean + | { + /** + * When true this will generate a full address. + * Otherwise it will just generate a street address. + */ + useFullAddress?: boolean; + } = {} + ): string { + if (typeof options === 'boolean') { + options = { useFullAddress: options }; + } + + const { useFullAddress } = options; + const formats = this.faker.definitions.location.street_address; const format = formats[useFullAddress ? 'full' : 'normal']; @@ -207,7 +256,8 @@ export class LocationModule { /** * Returns a random country code. * - * @param alphaCode The code to return. Can be either `'alpha-2'` (2 letter code) + * @param options The code to return or an options object. Defaults to `{}`. + * @param options.variant The variant to return. Can be either `'alpha-2'` (2 letter code) * or `'alpha-3'` (three letter code). Defaults to `'alpha-2'`. * * @example @@ -217,9 +267,27 @@ export class LocationModule { * * @since 8.0.0 */ - countryCode(alphaCode: 'alpha-2' | 'alpha-3' = 'alpha-2'): string { - const key = - alphaCode === 'alpha-3' ? 'country_code_alpha_3' : 'country_code'; + countryCode( + options: + | 'alpha-2' + | 'alpha-3' + | { + /** + * The code to return. + * Can be either `'alpha-2'` (2 letter code) + * or `'alpha-3'` (three letter code). + * + * @default 'alpha-2' + */ + variant?: 'alpha-2' | 'alpha-3'; + } = {} + ): string { + if (typeof options === 'string') { + options = { variant: options }; + } + + const { variant = 'alpha-2' } = options; + const key = variant === 'alpha-3' ? 'country_code_alpha_3' : 'country_code'; return this.faker.helpers.arrayElement( this.faker.definitions.location[key] @@ -254,6 +322,42 @@ export class LocationModule { ); } + /** + * Generates a random latitude. + * + * @param options An options object. Defaults to `{}`. + * @param options.max The upper bound for the latitude to generate. Defaults to `90`. + * @param options.min The lower bound for the latitude to generate. Defaults to `-90`. + * @param options.precision The number of decimal points of precision for the latitude. Defaults to `4`. + * + * @example + * faker.location.latitude() // -30.9501 + * faker.location.latitude({ max: 10 }) // 5.7225 + * faker.location.latitude({ max: 10, min: -10 }) // -9.6273 + * faker.location.latitude({ max: 10, min: -10, precision: 5 }) // 2.68452 + * + * @since 8.0.0 + */ + latitude(options?: { + /** + * The upper bound for the latitude to generate. + * + * @default 90 + */ + max?: number; + /** + * The lower bound for the latitude to generate. + * + * @default -90 + */ + min?: number; + /** + * The number of decimal points of precision for the latitude. + * + * @default 4 + */ + precision?: number; + }): number; /** * Generates a random latitude. * @@ -263,51 +367,303 @@ export class LocationModule { * * @example * faker.location.latitude() // -30.9501 + * faker.location.latitude(10) // 5.7225 + * faker.location.latitude(10, -10) // -9.6273 + * faker.location.latitude(10, -10, 5) // 2.68452 + * + * @since 8.0.0 + */ + latitude(max?: number, min?: number, precision?: number): number; + /** + * Generates a random latitude. + * + * @param options The upper bound for the latitude or an options object. Defaults to `{}`. + * @param options.max The upper bound for the latitude to generate. Defaults to `90`. + * @param options.min The lower bound for the latitude to generate. Defaults to `-90`. + * @param options.precision The number of decimal points of precision for the latitude. Defaults to `4`. + * @param legacyMin The lower bound for the latitude to generate. Defaults to `-90`. + * @param legacyPrecision The number of decimal points of precision for the latitude. Defaults to `4`. + * + * @example + * faker.location.latitude() // -30.9501 + * faker.location.latitude({ max: 10 }) // 5.7225 + * faker.location.latitude({ max: 10, min: -10 }) // -9.6273 + * faker.location.latitude({ max: 10, min: -10, precision: 5 }) // 2.68452 + * faker.location.latitude(10) // 5.7225 + * faker.location.latitude(10, -10) // -9.6273 * faker.location.latitude(10, -10, 5) // 2.68452 * * @since 8.0.0 */ - // TODO @xDivisionByZerox 2022-06-12 this signature should probably be an object for easier maintainability - latitude(max: number = 90, min: number = -90, precision: number = 4): number { + latitude( + options: + | number + | { + /** + * The upper bound for the latitude to generate. + * + * @default 90 + */ + max?: number; + /** + * The lower bound for the latitude to generate. + * + * @default -90 + */ + min?: number; + /** + * The number of decimal points of precision for the latitude. + * + * @default 4 + */ + precision?: number; + }, + legacyMin?: number, + legacyPrecision?: number + ): number; + /** + * Generates a random latitude. + * + * @param options The upper bound for the latitude or an options object. Defaults to `{}`. + * @param options.max The upper bound for the latitude to generate. Defaults to `90`. + * @param options.min The lower bound for the latitude to generate. Defaults to `-90`. + * @param options.precision The number of decimal points of precision for the latitude. Defaults to `4`. + * @param legacyMin The lower bound for the latitude to generate. Defaults to `-90`. + * @param legacyPrecision The number of decimal points of precision for the latitude. Defaults to `4`. + * + * @example + * faker.location.latitude() // -30.9501 + * faker.location.latitude({ max: 10 }) // 5.7225 + * faker.location.latitude({ max: 10, min: -10 }) // -9.6273 + * faker.location.latitude({ max: 10, min: -10, precision: 5 }) // 2.68452 + * faker.location.latitude(10) // 5.7225 + * faker.location.latitude(10, -10) // -9.6273 + * faker.location.latitude(10, -10, 5) // 2.68452 + * + * @since 8.0.0 + */ + latitude( + options: + | number + | { + /** + * The upper bound for the latitude to generate. + * + * @default 90 + */ + max?: number; + /** + * The lower bound for the latitude to generate. + * + * @default -90 + */ + min?: number; + /** + * The number of decimal points of precision for the latitude. + * + * @default 4 + */ + precision?: number; + } = {}, + legacyMin = -90, + legacyPrecision = 4 + ): number { + if (typeof options === 'number') { + options = { max: options }; + } + + const { max = 90, min = legacyMin, precision = legacyPrecision } = options; + return this.faker.number.float({ min, max, precision: 10 ** -precision }); } /** * Generates a random longitude. * - * @param max The upper bound for the longitude to generate. Defaults to `180`. - * @param min The lower bound for the longitude to generate. Defaults to `-180`. - * @param precision The number of decimal points of precision for the longitude. Defaults to `4`. + * @param options An options object. Defaults to `{}`. + * @param options.max The upper bound for the longitude to generate. Defaults to `180`. + * @param options.min The lower bound for the longitude to generate. Defaults to `-180`. + * @param options.precision The number of decimal points of precision for the longitude. Defaults to `4`. + * + * @example + * faker.location.longitude() // -30.9501 + * faker.location.longitude({ max: 10 }) // 5.7225 + * faker.location.longitude({ max: 10, min: -10 }) // -9.6273 + * faker.location.longitude({ max: 10, min: -10, precision: 5 }) // 2.68452 + * + * @since 8.0.0 + */ + longitude(options?: { + /** + * The upper bound for the latitude to generate. + * + * @default 90 + */ + max?: number; + /** + * The lower bound for the latitude to generate. + * + * @default -90 + */ + min?: number; + /** + * The number of decimal points of precision for the latitude. + * + * @default 4 + */ + precision?: number; + }): number; + /** + * Generates a random longitude. + * + * @param options An options object. Defaults to `{}`. + * @param options.max The upper bound for the longitude to generate. Defaults to `180`. + * @param options.min The lower bound for the longitude to generate. Defaults to `-180`. + * @param options.precision The number of decimal points of precision for the longitude. Defaults to `4`. + * + * @example + * faker.location.longitude() // -30.9501 + * faker.location.longitude({ max: 10 }) // 5.7225 + * faker.location.longitude({ max: 10, min: -10 }) // -9.6273 + * faker.location.longitude({ max: 10, min: -10, precision: 5 }) // 2.68452 + * + * @since 8.0.0 + */ + longitude(max?: number, min?: number, precision?: number): number; + /** + * Generates a random longitude. + * + * @param options The upper bound for the longitude or an options object. Defaults to `{}`. + * @param options.max The upper bound for the longitude to generate. Defaults to `180`. + * @param options.min The lower bound for the longitude to generate. Defaults to `-180`. + * @param options.precision The number of decimal points of precision for the longitude. Defaults to `4`. + * @param legacyMin The lower bound for the longitude to generate. Defaults to `-180`. + * @param legacyPrecision The number of decimal points of precision for the longitude. Defaults to `4`. + * + * @example + * faker.location.longitude() // -30.9501 + * faker.location.longitude({ max: 10 }) // 5.7225 + * faker.location.longitude({ max: 10, min: -10 }) // -9.6273 + * faker.location.longitude({ max: 10, min: -10, precision: 5 }) // 2.68452 + * + * @since 8.0.0 + */ + longitude( + options?: + | number + | { + /** + * The upper bound for the longitude to generate. + * + * @default 180 + */ + max?: number; + /** + * The lower bound for the longitude to generate. + * + * @default -180 + */ + min?: number; + /** + * The number of decimal points of precision for the longitude. + * + * @default 4 + */ + precision?: number; + }, + legacyMin?: number, + legacyPrecision?: number + ): number; + /** + * Generates a random longitude. + * + * @param options An options object. Defaults to `{}`. + * @param options.max The upper bound for the longitude to generate. Defaults to `180`. + * @param options.min The lower bound for the longitude to generate. Defaults to `-180`. + * @param options.precision The number of decimal points of precision for the longitude. Defaults to `4`. + * @param legacyMin The lower bound for the longitude to generate. Defaults to `-180`. + * @param legacyPrecision The number of decimal points of precision for the longitude. Defaults to `4`. * * @example * faker.location.longitude() // -154.0226 + * faker.location.longitude({ max: 10 }) // 2.4387 + * faker.location.longitude({ max: 10, min: -10 }) // 6.9126 + * faker.location.longitude({ max: 10, min: -10, precision: 5 }) // -4.03620 + * faker.location.longitude(10) // 2.4387 + * faker.location.longitude(10, -10) // 6.9126 * faker.location.longitude(10, -10, 5) // -4.03620 * * @since 8.0.0 */ - // TODO @xDivisionByZerox 2022-06-12 this signature should probably be an object for easier maintainability longitude( - max: number = 180, - min: number = -180, - precision: number = 4 + options: + | number + | { + /** + * The upper bound for the longitude to generate. + * + * @default 180 + */ + max?: number; + /** + * The lower bound for the longitude to generate. + * + * @default -180 + */ + min?: number; + /** + * The number of decimal points of precision for the longitude. + * + * @default 4 + */ + precision?: number; + } = {}, + legacyMin = -180, + legacyPrecision = 4 ): number { + if (typeof options === 'number') { + options = { max: options }; + } + + const { max = 180, min = legacyMin, precision = legacyPrecision } = options; + return this.faker.number.float({ max, min, precision: 10 ** -precision }); } /** * Returns a random direction (cardinal and ordinal; northwest, east, etc). * - * @param useAbbr If true this will return abbreviated directions (NW, E, etc). + * @param options Whether to use abbreviated or an options object. + * @param options.useAbbr If true this will return abbreviated directions (NW, E, etc). * Otherwise this will return the long name. Defaults to `false`. * * @example * faker.location.direction() // 'Northeast' * faker.location.direction(false) // 'South' * faker.location.direction(true) // 'NE' + * faker.location.direction({ useAbbr: true }) // 'SW' * * @since 8.0.0 */ - direction(useAbbr: boolean = false): string { + direction( + options: + | boolean + | { + /** + * If true this will return abbreviated directions (NW, E, etc). + * Otherwise this will return the long name. + * + * @default false + */ + useAbbr?: boolean; + } = {} + ): string { + if (typeof options === 'boolean') { + options = { useAbbr: options }; + } + + const { useAbbr = false } = options; + if (!useAbbr) { return this.faker.helpers.arrayElement( this.faker.definitions.location.direction @@ -322,17 +678,36 @@ export class LocationModule { /** * Returns a random cardinal direction (north, east, south, west). * - * @param useAbbr If true this will return abbreviated directions (N, E, etc). + * @param options Whether to use abbreviated or an options object. + * @param options.useAbbr If true this will return abbreviated directions (N, E, etc). * Otherwise this will return the long name. Defaults to `false`. * * @example * faker.location.cardinalDirection() // 'North' * faker.location.cardinalDirection(false) // 'South' * faker.location.cardinalDirection(true) // 'N' + * faker.location.cardinalDirection({ useAbbr: true }) // 'W' * * @since 8.0.0 */ - cardinalDirection(useAbbr: boolean = false): string { + cardinalDirection( + options: + | boolean + | { + /** + * If true this will return abbreviated directions (N, E, etc). + * Otherwise this will return the long name. + * + * @default false + */ + useAbbr?: boolean; + } = {} + ): string { + if (typeof options === 'boolean') { + options = { useAbbr: options }; + } + + const { useAbbr = false } = options; if (!useAbbr) { return this.faker.helpers.arrayElement( this.faker.definitions.location.direction.slice(0, 4) @@ -347,17 +722,36 @@ export class LocationModule { /** * Returns a random ordinal direction (northwest, southeast, etc). * - * @param useAbbr If true this will return abbreviated directions (NW, SE, etc). + * @param options Whether to use abbreviated or an options object. + * @param options.useAbbr If true this will return abbreviated directions (NW, SE, etc). * Otherwise this will return the long name. Defaults to `false`. * * @example * faker.location.ordinalDirection() // 'Northeast' * faker.location.ordinalDirection(false) // 'Northwest' * faker.location.ordinalDirection(true) // 'NE' + * faker.location.ordinalDirection({ useAbbr: true }) // 'SW' * * @since 8.0.0 */ - ordinalDirection(useAbbr: boolean = false): string { + ordinalDirection( + options: + | boolean + | { + /** + * If true this will return abbreviated directions (NW, SE, etc). + * Otherwise this will return the long name. + * + * @default false + */ + useAbbr?: boolean; + } = {} + ): string { + if (typeof options === 'boolean') { + options = { useAbbr: options }; + } + + const { useAbbr = false } = options; if (!useAbbr) { return this.faker.helpers.arrayElement( this.faker.definitions.location.direction.slice(4, 8) diff --git a/test/__snapshots__/location.spec.ts.snap b/test/__snapshots__/location.spec.ts.snap index 5f67809a36c..8b67230402f 100644 --- a/test/__snapshots__/location.spec.ts.snap +++ b/test/__snapshots__/location.spec.ts.snap @@ -4,9 +4,9 @@ exports[`location > 42 > buildingNumber 1`] = `"7917"`; exports[`location > 42 > cardinalDirection > noArgs 1`] = `"East"`; -exports[`location > 42 > cardinalDirection > with abbr = false 1`] = `"East"`; +exports[`location > 42 > cardinalDirection > with boolean 1`] = `"East"`; -exports[`location > 42 > cardinalDirection > with abbr = true 1`] = `"E"`; +exports[`location > 42 > cardinalDirection > with useAbbr option 1`] = `"E"`; exports[`location > 42 > city 1`] = `"Port Valentine"`; @@ -16,22 +16,54 @@ exports[`location > 42 > country 1`] = `"Guinea"`; exports[`location > 42 > countryCode > noArgs 1`] = `"GY"`; -exports[`location > 42 > countryCode > with code = alpha-2 1`] = `"GY"`; +exports[`location > 42 > countryCode > with alphaCode option 1`] = `"SAU"`; -exports[`location > 42 > countryCode > with code = alpha-3 1`] = `"SAU"`; +exports[`location > 42 > countryCode > with string 1`] = `"GY"`; exports[`location > 42 > county 1`] = `"Berkshire"`; exports[`location > 42 > direction > noArgs 1`] = `"South"`; -exports[`location > 42 > direction > with abbr = false 1`] = `"South"`; +exports[`location > 42 > direction > with boolean 1`] = `"South"`; -exports[`location > 42 > direction > with abbr = true 1`] = `"S"`; +exports[`location > 42 > direction > with useAbbr option 1`] = `"S"`; exports[`location > 42 > latitude > noArgs 1`] = `-22.5828`; +exports[`location > 42 > latitude > with max 1`] = `-52.546`; + +exports[`location > 42 > latitude > with max and min option 1`] = `-2.5092`; + +exports[`location > 42 > latitude > with max option 1`] = `-52.546`; + +exports[`location > 42 > latitude > with max, min and precision option 1`] = `-2.5091977138`; + +exports[`location > 42 > latitude > with min 1`] = `27.454`; + +exports[`location > 42 > latitude > with min option 1`] = `27.454`; + +exports[`location > 42 > latitude > with precision 1`] = `-22.5827794243`; + +exports[`location > 42 > latitude > with precision option 1`] = `-22.5827794243`; + exports[`location > 42 > longitude > noArgs 1`] = `-45.1656`; +exports[`location > 42 > longitude > with max 1`] = `-108.8374`; + +exports[`location > 42 > longitude > with max and min option 1`] = `-2.5092`; + +exports[`location > 42 > longitude > with max option 1`] = `-108.8374`; + +exports[`location > 42 > longitude > with max, min and precision option 1`] = `-2.5091977138`; + +exports[`location > 42 > longitude > with min 1`] = `61.1626`; + +exports[`location > 42 > longitude > with min option 1`] = `61.1626`; + +exports[`location > 42 > longitude > with precision 1`] = `-45.1655588485`; + +exports[`location > 42 > longitude > with precision option 1`] = `-45.1655588485`; + exports[`location > 42 > nearbyGPSCoordinate > near origin 1`] = ` [ 0.08140632875358443, @@ -90,9 +122,9 @@ exports[`location > 42 > nearbyGPSCoordinate > with radius and isMetric 1`] = ` exports[`location > 42 > ordinalDirection > noArgs 1`] = `"Northwest"`; -exports[`location > 42 > ordinalDirection > with abbr = false 1`] = `"Northwest"`; +exports[`location > 42 > ordinalDirection > with boolean 1`] = `"Northwest"`; -exports[`location > 42 > ordinalDirection > with abbr = true 1`] = `"NW"`; +exports[`location > 42 > ordinalDirection > with useAbbr option 1`] = `"NW"`; exports[`location > 42 > secondaryAddress 1`] = `"Apt. 791"`; @@ -104,9 +136,9 @@ exports[`location > 42 > street 1`] = `"Peyton Villages"`; exports[`location > 42 > streetAddress > noArgs 1`] = `"7917 Miller Park"`; -exports[`location > 42 > streetAddress > with useFullAddress = false 1`] = `"7917 Miller Park"`; +exports[`location > 42 > streetAddress > with boolean 1`] = `"7917 Miller Park"`; -exports[`location > 42 > streetAddress > with useFullAddress = true 1`] = `"7917 Miller Park Apt. 410"`; +exports[`location > 42 > streetAddress > with useFullAddress options 1`] = `"7917 Miller Park Apt. 410"`; exports[`location > 42 > streetName 1`] = `"b"`; @@ -114,19 +146,25 @@ exports[`location > 42 > timeZone 1`] = `"America/North_Dakota/New_Salem"`; exports[`location > 42 > zipCode > noArgs 1`] = `"79177"`; -exports[`location > 42 > zipCode > with format 1`] = `"379-177"`; +exports[`location > 42 > zipCode > with format option 1`] = `"379-177"`; + +exports[`location > 42 > zipCode > with string 1`] = `"379"`; + +exports[`location > 42 > zipCodeByState > noArgs 1`] = `"79177"`; + +exports[`location > 42 > zipCodeByState > with state options 1`] = `"79177"`; -exports[`location > 42 > zipCodeByState > state 1`] = `"79177"`; +exports[`location > 42 > zipCodeByState > with string 1 1`] = `"79177"`; -exports[`location > 42 > zipCodeByState > state2 1`] = `"79177"`; +exports[`location > 42 > zipCodeByState > with string 2 1`] = `"79177"`; exports[`location > 1211 > buildingNumber 1`] = `"487"`; exports[`location > 1211 > cardinalDirection > noArgs 1`] = `"West"`; -exports[`location > 1211 > cardinalDirection > with abbr = false 1`] = `"West"`; +exports[`location > 1211 > cardinalDirection > with boolean 1`] = `"West"`; -exports[`location > 1211 > cardinalDirection > with abbr = true 1`] = `"W"`; +exports[`location > 1211 > cardinalDirection > with useAbbr option 1`] = `"W"`; exports[`location > 1211 > city 1`] = `"La Crosse"`; @@ -136,22 +174,54 @@ exports[`location > 1211 > country 1`] = `"Uganda"`; exports[`location > 1211 > countryCode > noArgs 1`] = `"UM"`; -exports[`location > 1211 > countryCode > with code = alpha-2 1`] = `"UM"`; +exports[`location > 1211 > countryCode > with alphaCode option 1`] = `"IRN"`; -exports[`location > 1211 > countryCode > with code = alpha-3 1`] = `"IRN"`; +exports[`location > 1211 > countryCode > with string 1`] = `"UM"`; exports[`location > 1211 > county 1`] = `"Cambridgeshire"`; exports[`location > 1211 > direction > noArgs 1`] = `"Southwest"`; -exports[`location > 1211 > direction > with abbr = false 1`] = `"Southwest"`; +exports[`location > 1211 > direction > with boolean 1`] = `"Southwest"`; -exports[`location > 1211 > direction > with abbr = true 1`] = `"SW"`; +exports[`location > 1211 > direction > with useAbbr option 1`] = `"SW"`; exports[`location > 1211 > latitude > noArgs 1`] = `77.1337`; +exports[`location > 1211 > latitude > with max 1`] = `2.8521`; + +exports[`location > 1211 > latitude > with max and min option 1`] = `8.5704`; + +exports[`location > 1211 > latitude > with max option 1`] = `2.8521`; + +exports[`location > 1211 > latitude > with max, min and precision option 1`] = `8.5704030749`; + +exports[`location > 1211 > latitude > with min 1`] = `82.8521`; + +exports[`location > 1211 > latitude > with min option 1`] = `82.8521`; + +exports[`location > 1211 > latitude > with precision 1`] = `77.1336276737`; + +exports[`location > 1211 > latitude > with precision option 1`] = `77.1336276737`; + exports[`location > 1211 > longitude > noArgs 1`] = `154.2673`; +exports[`location > 1211 > longitude > with max 1`] = `-3.5811`; + +exports[`location > 1211 > longitude > with max and min option 1`] = `8.5704`; + +exports[`location > 1211 > longitude > with max option 1`] = `-3.5811`; + +exports[`location > 1211 > longitude > with max, min and precision option 1`] = `8.5704030749`; + +exports[`location > 1211 > longitude > with min 1`] = `166.4189`; + +exports[`location > 1211 > longitude > with min option 1`] = `166.4189`; + +exports[`location > 1211 > longitude > with precision 1`] = `154.2672553473`; + +exports[`location > 1211 > longitude > with precision option 1`] = `154.2672553473`; + exports[`location > 1211 > nearbyGPSCoordinate > near origin 1`] = ` [ -0.02872111236834616, @@ -210,9 +280,9 @@ exports[`location > 1211 > nearbyGPSCoordinate > with radius and isMetric 1`] = exports[`location > 1211 > ordinalDirection > noArgs 1`] = `"Southwest"`; -exports[`location > 1211 > ordinalDirection > with abbr = false 1`] = `"Southwest"`; +exports[`location > 1211 > ordinalDirection > with boolean 1`] = `"Southwest"`; -exports[`location > 1211 > ordinalDirection > with abbr = true 1`] = `"SW"`; +exports[`location > 1211 > ordinalDirection > with useAbbr option 1`] = `"SW"`; exports[`location > 1211 > secondaryAddress 1`] = `"Suite 487"`; @@ -224,9 +294,9 @@ exports[`location > 1211 > street 1`] = `"Koelpin Turnpike"`; exports[`location > 1211 > streetAddress > noArgs 1`] = `"487 Breana Wells"`; -exports[`location > 1211 > streetAddress > with useFullAddress = false 1`] = `"487 Breana Wells"`; +exports[`location > 1211 > streetAddress > with boolean 1`] = `"487 Breana Wells"`; -exports[`location > 1211 > streetAddress > with useFullAddress = true 1`] = `"487 Breana Wells Apt. 616"`; +exports[`location > 1211 > streetAddress > with useFullAddress options 1`] = `"487 Breana Wells Apt. 616"`; exports[`location > 1211 > streetName 1`] = `"c"`; @@ -234,19 +304,25 @@ exports[`location > 1211 > timeZone 1`] = `"Pacific/Fiji"`; exports[`location > 1211 > zipCode > noArgs 1`] = `"48721-9061"`; -exports[`location > 1211 > zipCode > with format 1`] = `"948-721"`; +exports[`location > 1211 > zipCode > with format option 1`] = `"948-721"`; + +exports[`location > 1211 > zipCode > with string 1`] = `"948"`; -exports[`location > 1211 > zipCodeByState > state 1`] = `"48721-9061"`; +exports[`location > 1211 > zipCodeByState > noArgs 1`] = `"48721-9061"`; -exports[`location > 1211 > zipCodeByState > state2 1`] = `"48721-9061"`; +exports[`location > 1211 > zipCodeByState > with state options 1`] = `"48721-9061"`; + +exports[`location > 1211 > zipCodeByState > with string 1 1`] = `"48721-9061"`; + +exports[`location > 1211 > zipCodeByState > with string 2 1`] = `"48721-9061"`; exports[`location > 1337 > buildingNumber 1`] = `"51225"`; exports[`location > 1337 > cardinalDirection > noArgs 1`] = `"East"`; -exports[`location > 1337 > cardinalDirection > with abbr = false 1`] = `"East"`; +exports[`location > 1337 > cardinalDirection > with boolean 1`] = `"East"`; -exports[`location > 1337 > cardinalDirection > with abbr = true 1`] = `"E"`; +exports[`location > 1337 > cardinalDirection > with useAbbr option 1`] = `"E"`; exports[`location > 1337 > city 1`] = `"New Carmella"`; @@ -256,22 +332,54 @@ exports[`location > 1337 > country 1`] = `"Egypt"`; exports[`location > 1337 > countryCode > noArgs 1`] = `"EH"`; -exports[`location > 1337 > countryCode > with code = alpha-2 1`] = `"EH"`; +exports[`location > 1337 > countryCode > with alphaCode option 1`] = `"PRI"`; -exports[`location > 1337 > countryCode > with code = alpha-3 1`] = `"PRI"`; +exports[`location > 1337 > countryCode > with string 1`] = `"EH"`; exports[`location > 1337 > county 1`] = `"Bedfordshire"`; exports[`location > 1337 > direction > noArgs 1`] = `"South"`; -exports[`location > 1337 > direction > with abbr = false 1`] = `"South"`; +exports[`location > 1337 > direction > with boolean 1`] = `"South"`; -exports[`location > 1337 > direction > with abbr = true 1`] = `"S"`; +exports[`location > 1337 > direction > with useAbbr option 1`] = `"S"`; exports[`location > 1337 > latitude > noArgs 1`] = `-42.8356`; +exports[`location > 1337 > latitude > with max 1`] = `-63.7976`; + +exports[`location > 1337 > latitude > with max and min option 1`] = `-4.7595`; + +exports[`location > 1337 > latitude > with max option 1`] = `-63.7976`; + +exports[`location > 1337 > latitude > with max, min and precision option 1`] = `-4.7595064761`; + +exports[`location > 1337 > latitude > with min 1`] = `16.2024`; + +exports[`location > 1337 > latitude > with min option 1`] = `16.2024`; + +exports[`location > 1337 > latitude > with precision 1`] = `-42.835558285`; + +exports[`location > 1337 > latitude > with precision option 1`] = `-42.835558285`; + exports[`location > 1337 > longitude > noArgs 1`] = `-85.6711`; +exports[`location > 1337 > longitude > with max 1`] = `-130.2153`; + +exports[`location > 1337 > longitude > with max and min option 1`] = `-4.7595`; + +exports[`location > 1337 > longitude > with max option 1`] = `-130.2153`; + +exports[`location > 1337 > longitude > with max, min and precision option 1`] = `-4.7595064761`; + +exports[`location > 1337 > longitude > with min 1`] = `39.7847`; + +exports[`location > 1337 > longitude > with min option 1`] = `39.7847`; + +exports[`location > 1337 > longitude > with precision 1`] = `-85.67111657`; + +exports[`location > 1337 > longitude > with precision option 1`] = `-85.67111657`; + exports[`location > 1337 > nearbyGPSCoordinate > near origin 1`] = ` [ 0.08055259537977688, @@ -330,9 +438,9 @@ exports[`location > 1337 > nearbyGPSCoordinate > with radius and isMetric 1`] = exports[`location > 1337 > ordinalDirection > noArgs 1`] = `"Northwest"`; -exports[`location > 1337 > ordinalDirection > with abbr = false 1`] = `"Northwest"`; +exports[`location > 1337 > ordinalDirection > with boolean 1`] = `"Northwest"`; -exports[`location > 1337 > ordinalDirection > with abbr = true 1`] = `"NW"`; +exports[`location > 1337 > ordinalDirection > with useAbbr option 1`] = `"NW"`; exports[`location > 1337 > secondaryAddress 1`] = `"Apt. 512"`; @@ -344,9 +452,9 @@ exports[`location > 1337 > street 1`] = `"Kellen Crest"`; exports[`location > 1337 > streetAddress > noArgs 1`] = `"51225 Alexys Gateway"`; -exports[`location > 1337 > streetAddress > with useFullAddress = false 1`] = `"51225 Alexys Gateway"`; +exports[`location > 1337 > streetAddress > with boolean 1`] = `"51225 Alexys Gateway"`; -exports[`location > 1337 > streetAddress > with useFullAddress = true 1`] = `"51225 Alexys Gateway Apt. 552"`; +exports[`location > 1337 > streetAddress > with useFullAddress options 1`] = `"51225 Alexys Gateway Apt. 552"`; exports[`location > 1337 > streetName 1`] = `"a"`; @@ -354,8 +462,14 @@ exports[`location > 1337 > timeZone 1`] = `"America/Guatemala"`; exports[`location > 1337 > zipCode > noArgs 1`] = `"51225"`; -exports[`location > 1337 > zipCode > with format 1`] = `"251-225"`; +exports[`location > 1337 > zipCode > with format option 1`] = `"251-225"`; + +exports[`location > 1337 > zipCode > with string 1`] = `"251"`; + +exports[`location > 1337 > zipCodeByState > noArgs 1`] = `"51225"`; + +exports[`location > 1337 > zipCodeByState > with state options 1`] = `"51225"`; -exports[`location > 1337 > zipCodeByState > state 1`] = `"51225"`; +exports[`location > 1337 > zipCodeByState > with string 1 1`] = `"51225"`; -exports[`location > 1337 > zipCodeByState > state2 1`] = `"51225"`; +exports[`location > 1337 > zipCodeByState > with string 2 1`] = `"51225"`; diff --git a/test/location.spec.ts b/test/location.spec.ts index 3a7d958d0ea..3328f8d0727 100644 --- a/test/location.spec.ts +++ b/test/location.spec.ts @@ -48,27 +48,42 @@ describe('location', () => { t.it('buildingNumber'); t.it('secondaryAddress'); + t.describe('streetAddress', (t) => { t.it('noArgs') - .it('with useFullAddress = true', true) - .it('with useFullAddress = false', false); + .it('with boolean', false) + .it('with useFullAddress options', { useFullAddress: true }); }); t.itEach('city', 'cityName'); t.it('county'); - t.it('country').describe('countryCode', (t) => { + t.it('country'); + + t.describe('countryCode', (t) => { t.it('noArgs') - .it('with code = alpha-2', 'alpha-2') - .it('with code = alpha-3', 'alpha-3'); + .it('with string', 'alpha-2') + .it('with alphaCode option', { variant: 'alpha-3' }); }); - t.describe('latitude', (t) => { - t.it('noArgs'); - }); - t.describe('longitude', (t) => { - t.it('noArgs'); + t.describeEach( + 'latitude', + 'longitude' + )((t) => { + t.it('noArgs') + .it('with max', 10) + .it('with min', undefined, -10) + .it('with precision', undefined, undefined, 10) + .it('with max option', { max: 10 }) + .it('with min option', { min: -10 }) + .it('with precision option', { precision: 10 }) + .it('with max and min option', { max: 10, min: -10 }) + .it('with max, min and precision option', { + max: 10, + min: -10, + precision: 10, + }); }); t.describe('nearbyGPSCoordinate', (t) => { @@ -95,17 +110,21 @@ describe('location', () => { 'ordinalDirection' )((t) => { t.it('noArgs') - .it('with abbr = true', true) - .it('with abbr = false', false); + .it('with boolean', false) + .it('with useAbbr option', { useAbbr: true }); }); t.describe('zipCode', (t) => { - t.it('noArgs').it('with format', '###-###'); + t.it('noArgs') + .it('with string', '###') + .it('with format option', { format: '###-###' }); }); t.describe('zipCodeByState', (t) => { - t.it('state', 'CA'); - t.it('state2', 'WA'); + t.it('noArgs') + .it('with string 1', 'CA') + .it('with string 2', 'WA') + .it('with state options', { state: 'WA' }); }); }); @@ -125,12 +144,12 @@ describe('location', () => { describe('zipCode()', () => { it('returns random zipCode - user specified format', () => { - let zipCode = faker.location.zipCode('?#? #?#'); + let zipCode = faker.location.zipCode({ format: '?#? #?#' }); expect(zipCode).toMatch(/^[A-Za-z]\d[A-Za-z]\s\d[A-Za-z]\d$/); // try another format - zipCode = faker.location.zipCode('###-###'); + zipCode = faker.location.zipCode({ format: '###-###' }); expect(zipCode).toMatch(/^\d{3}-\d{3}$/); }); @@ -178,7 +197,7 @@ describe('location', () => { }); it('returns latitude with min and max and default precision', () => { - const latitude = faker.location.latitude(5, -5); + const latitude = faker.location.latitude({ max: 5, min: -5 }); expect( latitude.toString().split('.')[1].length, @@ -190,7 +209,7 @@ describe('location', () => { }); it('returns random latitude with custom precision', () => { - const latitude = faker.location.latitude(undefined, undefined, 7); + const latitude = faker.location.latitude({ precision: 7 }); expect( latitude.toString().split('.')[1].length, @@ -217,7 +236,7 @@ describe('location', () => { }); it('returns random longitude with min and max and default precision', () => { - const longitude = faker.location.longitude(100, -30); + const longitude = faker.location.longitude({ max: 100, min: -30 }); expect( longitude.toString().split('.')[1].length, @@ -229,7 +248,7 @@ describe('location', () => { }); it('returns random longitude with custom precision', () => { - const longitude = faker.location.longitude(undefined, undefined, 7); + const longitude = faker.location.longitude({ precision: 7 }); expect( longitude.toString().split('.')[1].length, @@ -243,7 +262,7 @@ describe('location', () => { describe('direction()', () => { it('returns abbreviation when useAbbr is true', () => { - const direction = faker.location.direction(true); + const direction = faker.location.direction({ useAbbr: true }); const lengthDirection = direction.length; const prefixErrorMessage = 'The abbreviation of direction when useAbbr is true should'; @@ -258,7 +277,9 @@ describe('location', () => { describe('ordinalDirection()', () => { it('returns abbreviation when useAbbr is true', () => { - const ordinalDirection = faker.location.ordinalDirection(true); + const ordinalDirection = faker.location.ordinalDirection({ + useAbbr: true, + }); const expectedType = 'string'; const ordinalDirectionLength = ordinalDirection.length; const prefixErrorMessage = @@ -274,7 +295,9 @@ describe('location', () => { describe('cardinalDirection()', () => { it('returns abbreviation when useAbbr is true', () => { - const cardinalDirection = faker.location.cardinalDirection(true); + const cardinalDirection = faker.location.cardinalDirection({ + useAbbr: true, + }); const expectedType = 'string'; const cardinalDirectionLength = cardinalDirection.length; const prefixErrorMessage =