Skip to content

Commit

Permalink
add option to allow randCountryCode to generate alpha3 syntax codes (#…
Browse files Browse the repository at this point in the history
…376)

* feat: 🔥 add country code alpha3 option

Country codes can be 2 or 3 chars long. This commit add alpha3 optionnal
param to switch from default alpha2 to alpha3 syntax.
  • Loading branch information
MathRobin authored Feb 21, 2024
1 parent 318fdf5 commit 6ffa858
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 8 deletions.
251 changes: 250 additions & 1 deletion packages/falso/src/lib/country-code.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"data": [
"alpha2": [
"MS",
"TW",
"LR",
Expand Down Expand Up @@ -85,5 +85,254 @@
"SL",
"CL",
"ER"
],
"alpha3": [
"AFG",
"ALB",
"DZA",
"ASM",
"AND",
"AGO",
"AIA",
"ATA",
"ATG",
"ARG",
"ARM",
"ABW",
"AUS",
"AUT",
"AZE",
"BHS",
"BHR",
"BGD",
"BRB",
"BLR",
"BEL",
"BLZ",
"BEN",
"BMU",
"BTN",
"BOL",
"BES",
"BIH",
"BWA",
"BVT",
"BRA",
"IOT",
"BRN",
"BGR",
"BFA",
"BDI",
"CPV",
"KHM",
"CMR",
"CAN",
"CYM",
"CAF",
"TCD",
"CHL",
"CHN",
"CXR",
"CCK",
"COL",
"COM",
"COG",
"COD",
"COK",
"CRI",
"HRV",
"CUB",
"CUW",
"CYP",
"CZE",
"DNK",
"DJI",
"DMA",
"DOM",
"ECU",
"EGY",
"SLV",
"GNQ",
"ERI",
"EST",
"SWZ",
"ETH",
"FLK",
"FRO",
"FJI",
"FIN",
"FRA",
"GUF",
"PYF",
"ATF",
"GAB",
"GMB",
"GEO",
"DEU",
"GHA",
"GIB",
"GRC",
"GRL",
"GRD",
"GLP",
"GUM",
"GTM",
"GGY",
"GIN",
"GNB",
"GUY",
"HTI",
"HMD",
"VAT",
"HND",
"HKG",
"HUN",
"ISL",
"IND",
"IDN",
"IRN",
"IRQ",
"IRL",
"IMN",
"ISR",
"ITA",
"JAM",
"JPN",
"JEY",
"JOR",
"KAZ",
"KEN",
"KIR",
"PRK",
"KOR",
"KWT",
"KGZ",
"LAO",
"LVA",
"LBN",
"LSO",
"LBR",
"LBY",
"LIE",
"LTU",
"LUX",
"MAC",
"MDG",
"MWI",
"MYS",
"MDV",
"MLI",
"MLT",
"MHL",
"MTQ",
"MRT",
"MUS",
"MYT",
"MEX",
"FSM",
"MDA",
"MCO",
"MNG",
"MNE",
"MSR",
"MAR",
"MOZ",
"MMR",
"NAM",
"NRU",
"NPL",
"NLD",
"NCL",
"NZL",
"NIC",
"NER",
"NGA",
"NIU",
"NFK",
"MKD",
"MNP",
"NOR",
"OMN",
"PAK",
"PLW",
"PSE",
"PAN",
"PNG",
"PRY",
"PER",
"PHL",
"PCN",
"POL",
"PRT",
"PRI",
"QAT",
"REU",
"ROU",
"RUS",
"RWA",
"BLM",
"SHN",
"KNA",
"LCA",
"MAF",
"SPM",
"VCT",
"WSM",
"SMR",
"STP",
"SAU",
"SEN",
"SRB",
"SYC",
"SLE",
"SGP",
"SXM",
"SVK",
"SVN",
"SLB",
"SOM",
"ZAF",
"SGS",
"SSD",
"ESP",
"LKA",
"SDN",
"SUR",
"SJM",
"SWE",
"CHE",
"SYR",
"TWN",
"TJK",
"TZA",
"THA",
"TLS",
"TGO",
"TKL",
"TON",
"TTO",
"TUN",
"TUR",
"TKM",
"TCA",
"TUV",
"UGA",
"UKR",
"ARE",
"GBR",
"USA",
"UMI",
"URY",
"UZB",
"VUT",
"VEN",
"VNM",
"VGB",
"VIR",
"WLF",
"ESH",
"YEM",
"ZMB",
"ZWE"
]
}
27 changes: 20 additions & 7 deletions packages/falso/src/lib/country-code.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { fake, FakeOptions } from './core/core';
import { fake, FakeOptions, RandomInRangeOptions } from './core/core';
import { AZ } from './core/types';
import { data } from './country-code.json';
import { alpha2, alpha3 } from './country-code.json';

export type CountryCode = `${AZ}${AZ}`;
export type CountryCode3 = `${AZ}${AZ}${AZ}`;

export interface RandomCountryCodeOptions
extends RandomInRangeOptions,
FakeOptions {
alpha3?: boolean;
}

/**
* Generate a random country code.
Expand All @@ -11,15 +18,21 @@ export type CountryCode = `${AZ}${AZ}`;
*
* @example
*
* randCountryCode()
* randCountryCode() // US
*
* @example
*
* randCountryCode({ alpha3 : true }) // KRW
*
* @example
*
* randCountryCode({ length: 10 })
*
*/
export function randCountryCode<Options extends FakeOptions = never>(
options?: Options
) {
return fake(data as CountryCode[], options);
export function randCountryCode<
Options extends RandomCountryCodeOptions = never
>(options?: Options) {
const dataset = options?.alpha3 ? alpha3 : alpha2;

return fake(dataset as CountryCode[] | CountryCode3[], options);
}
35 changes: 35 additions & 0 deletions packages/falso/src/tests/country-code.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { randCountryCode } from '../lib/country-code';

describe('countryCode', () => {
it('should create default country code', () => {
const result = randCountryCode();

expect(result.length).toBe(2);
expect(typeof result).toBe('string');
});

it('should create default country code using alpha-3 syntax', () => {
const result = randCountryCode({ alpha3: true });

expect(result.length).toBe(3);
expect(typeof result).toBe('string');
});

it('should create array of default country codes', () => {
const result = randCountryCode({ length: 10 });

expect(result.length).toBe(10);
expect(Array.isArray(result)).toBe(true);
expect(result[0].length).toBe(2);
expect(result[0]).not.toEqual(result[1]);
});

it('should create array of alpha3 country codes', () => {
const result = randCountryCode({ length: 10, alpha3: true });

expect(result.length).toBe(10);
expect(Array.isArray(result)).toBe(true);
expect(result[0].length).toBe(3);
expect(result[0]).not.toEqual(result[1]);
});
});

0 comments on commit 6ffa858

Please sign in to comment.