From 82b779da5e87fddd7b5a5564b7228ac54b44d349 Mon Sep 17 00:00:00 2001 From: Robin van der Vliet Date: Fri, 15 Sep 2023 21:48:17 +0200 Subject: feat(location): Support ISO 3166-1 numeric country codes (#2325) --- src/modules/location/index.ts | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'src/modules') diff --git a/src/modules/location/index.ts b/src/modules/location/index.ts index 175c5433..19816c8f 100644 --- a/src/modules/location/index.ts +++ b/src/modules/location/index.ts @@ -320,13 +320,19 @@ export class LocationModule { * Returns a random [ISO_3166-1](https://en.wikipedia.org/wiki/ISO_3166-1) country 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'` (two-letter code) - * or `'alpha-3'` (three-letter code). Defaults to `'alpha-2'`. + * @param options.variant The variant to return. Can be one of: + * + * - `'alpha-2'` (two-letter code) + * - `'alpha-3'` (three-letter code) + * - `'numeric'` (numeric code) + * + * Defaults to `'alpha-2'`. * * @example * faker.location.countryCode() // 'SJ' * faker.location.countryCode('alpha-2') // 'GA' * faker.location.countryCode('alpha-3') // 'TJK' + * faker.location.countryCode('numeric') // '528' * * @since 8.0.0 */ @@ -334,15 +340,17 @@ export class LocationModule { options: | 'alpha-2' | 'alpha-3' + | 'numeric' | { /** * The code to return. - * Can be either `'alpha-2'` (two-letter code) - * or `'alpha-3'` (three-letter code). + * Can be either `'alpha-2'` (two-letter code), + * `'alpha-3'` (three-letter code) + * or `'numeric'` (numeric code). * * @default 'alpha-2' */ - variant?: 'alpha-2' | 'alpha-3'; + variant?: 'alpha-2' | 'alpha-3' | 'numeric'; } = {} ): string { if (typeof options === 'string') { @@ -350,7 +358,17 @@ export class LocationModule { } const { variant = 'alpha-2' } = options; - const key = variant === 'alpha-3' ? 'alpha3' : 'alpha2'; + const key = (() => { + switch (variant) { + case 'numeric': + return 'numeric'; + case 'alpha-3': + return 'alpha3'; + case 'alpha-2': + default: + return 'alpha2'; + } + })(); return this.faker.helpers.arrayElement( this.faker.definitions.location.country_code -- cgit v1.2.3