country-codes-list 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/dist/countriesData.d.ts +1 -0
- package/dist/countriesData.js +253 -18
- package/package.json +5 -2
- package/.github/workflows/publish.yml +0 -27
- package/src/countriesData.ts +0 -4100
- package/src/index.ts +0 -95
- package/src/utils/groupBy.ts +0 -19
- package/src/utils/supplant.ts +0 -17
- package/tests/index.test.ts +0 -62
- package/tsconfig.json +0 -12
package/src/index.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import groupBy from "./utils/groupBy";
|
|
2
|
-
import supplant from "./utils/supplant";
|
|
3
|
-
import countriesData, { CountryData, CountryProperty } from "./countriesData";
|
|
4
|
-
|
|
5
|
-
export type { CountryData, CountryProperty };
|
|
6
|
-
|
|
7
|
-
export const utils = {
|
|
8
|
-
groupBy,
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export function all(): CountryData[] {
|
|
12
|
-
return countriesData;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function filter(
|
|
16
|
-
countryProperty: CountryProperty,
|
|
17
|
-
value: string
|
|
18
|
-
): CountryData[] {
|
|
19
|
-
return countriesData.filter(
|
|
20
|
-
(countryData: CountryData) => countryData[countryProperty] === value
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function findOne(
|
|
25
|
-
countryProperty: CountryProperty,
|
|
26
|
-
value: string
|
|
27
|
-
): CountryData | undefined {
|
|
28
|
-
return countriesData.find(
|
|
29
|
-
(countryData: CountryData) => countryData[countryProperty] === value
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function customArray(
|
|
34
|
-
fields: Record<string, string> = {
|
|
35
|
-
name: "{countryNameEn} ({countryCode})",
|
|
36
|
-
value: "{countryCode}",
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
sortBy,
|
|
40
|
-
sortDataBy,
|
|
41
|
-
filter: filterFunc,
|
|
42
|
-
}: {
|
|
43
|
-
sortBy?: CountryProperty;
|
|
44
|
-
sortDataBy?: CountryProperty;
|
|
45
|
-
filter?: (cd: CountryData) => boolean;
|
|
46
|
-
} = {}
|
|
47
|
-
) {
|
|
48
|
-
const finalCollection: Record<string, string>[] = [];
|
|
49
|
-
let data: CountryData[] = countriesData;
|
|
50
|
-
if (typeof filterFunc === "function") {
|
|
51
|
-
data = data.filter(filterFunc);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (sortDataBy) {
|
|
55
|
-
const collator = new Intl.Collator([], { sensitivity: "accent" });
|
|
56
|
-
data.sort((a: CountryData, b: CountryData) =>
|
|
57
|
-
collator.compare(a[sortDataBy] as string, b[sortDataBy] as string)
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
data.forEach((countryData: CountryData) => {
|
|
62
|
-
const collectionObject: Record<string, string> = {};
|
|
63
|
-
for (const field in fields) {
|
|
64
|
-
collectionObject[field] = supplant(fields[field], countryData);
|
|
65
|
-
}
|
|
66
|
-
finalCollection.push(collectionObject);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
if (sortBy && fields[sortBy as string]) {
|
|
70
|
-
const collator = new Intl.Collator([], { sensitivity: "accent" });
|
|
71
|
-
finalCollection.sort((a, b) =>
|
|
72
|
-
collator.compare(a[sortBy as string], b[sortBy as string])
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return finalCollection;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function customList(
|
|
80
|
-
key: keyof CountryData = "countryCode",
|
|
81
|
-
label: string = "{countryNameEn} ({countryCode})",
|
|
82
|
-
{ filter: filterFunc }: { filter?: (cd: CountryData) => boolean } = {}
|
|
83
|
-
) {
|
|
84
|
-
const finalObject: Record<string, string> = {};
|
|
85
|
-
let data: CountryData[] = countriesData;
|
|
86
|
-
if (typeof filterFunc === "function") {
|
|
87
|
-
data = data.filter(filterFunc);
|
|
88
|
-
}
|
|
89
|
-
data.forEach((countryData: CountryData) => {
|
|
90
|
-
const value = supplant(label, countryData);
|
|
91
|
-
finalObject[String(countryData[key])] = value;
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
return finalObject;
|
|
95
|
-
}
|
package/src/utils/groupBy.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Groups an array by a specified key.
|
|
3
|
-
* @param array - The array to group.
|
|
4
|
-
* @param key - The key to group the array by.
|
|
5
|
-
* @returns An object where each key is a unique value from the array, and each value is an array of items from the original array that match the key.
|
|
6
|
-
*/
|
|
7
|
-
export default function groupBy<T>(
|
|
8
|
-
array: T[],
|
|
9
|
-
key: keyof T
|
|
10
|
-
): Record<string, T[]> {
|
|
11
|
-
return array.reduce((result, item) => {
|
|
12
|
-
const groupKey = String(item[key]);
|
|
13
|
-
if (!result[groupKey]) {
|
|
14
|
-
result[groupKey] = [];
|
|
15
|
-
}
|
|
16
|
-
result[groupKey].push(item);
|
|
17
|
-
return result;
|
|
18
|
-
}, {} as Record<string, T[]>);
|
|
19
|
-
}
|
package/src/utils/supplant.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Replaces placeholders in a string with values from an object.
|
|
3
|
-
* @param template - The string containing placeholders.
|
|
4
|
-
* @param data - The object containing key-value pairs for replacement.
|
|
5
|
-
* @returns The string with placeholders replaced by values.
|
|
6
|
-
*/
|
|
7
|
-
export default function supplant(template: string, data: any): string {
|
|
8
|
-
return template.replace(
|
|
9
|
-
/{([^{}]*)}/g,
|
|
10
|
-
(match: string, key: string): string => {
|
|
11
|
-
const value = data[key];
|
|
12
|
-
return typeof value === "string" || typeof value === "number"
|
|
13
|
-
? value.toString()
|
|
14
|
-
: match;
|
|
15
|
-
}
|
|
16
|
-
);
|
|
17
|
-
}
|
package/tests/index.test.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import * as countryCodes from "../src/index";
|
|
2
|
-
|
|
3
|
-
describe("country-codes-list", () => {
|
|
4
|
-
test("all returns a non-empty array", () => {
|
|
5
|
-
const data = countryCodes.all();
|
|
6
|
-
expect(Array.isArray(data)).toBe(true);
|
|
7
|
-
expect(data.length).toBeGreaterThan(0);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
test("filter returns correct countries", () => {
|
|
11
|
-
const result = countryCodes.filter("countryCode", "AD");
|
|
12
|
-
expect(result.length).toBeGreaterThan(0);
|
|
13
|
-
expect(result[0].countryNameEn).toBe("Andorra");
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test("findOne returns a country", () => {
|
|
17
|
-
const result = countryCodes.findOne("countryCode", "AF");
|
|
18
|
-
expect(result).toBeDefined();
|
|
19
|
-
if (result) {
|
|
20
|
-
expect(result.countryNameEn).toBe("Afghanistan");
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test("customArray returns mapped array with correct placeholders", () => {
|
|
25
|
-
const fields = {
|
|
26
|
-
customName: "{countryNameEn}",
|
|
27
|
-
customCode: "{countryCode}",
|
|
28
|
-
};
|
|
29
|
-
const result = countryCodes.customArray(fields);
|
|
30
|
-
expect(result.length).toBeGreaterThan(0);
|
|
31
|
-
expect(result[0]).toHaveProperty("customName");
|
|
32
|
-
expect(result[0]).toHaveProperty("customCode");
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test("customList returns object with countryCode as key", () => {
|
|
36
|
-
const list = countryCodes.customList("countryCode", "{countryNameEn}");
|
|
37
|
-
expect(Object.keys(list).length).toBeGreaterThan(0);
|
|
38
|
-
expect(list["AD"]).toBeDefined();
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("all countriesData are valid", () => {
|
|
42
|
-
const data = countryCodes.all();
|
|
43
|
-
const shape = {
|
|
44
|
-
countryNameEn: expect.any(String),
|
|
45
|
-
countryNameLocal: expect.any(String),
|
|
46
|
-
countryCode: expect.any(String),
|
|
47
|
-
currencyCode: expect.any(String),
|
|
48
|
-
currencyNameEn: expect.any(String),
|
|
49
|
-
tinType: expect.any(String),
|
|
50
|
-
tinName: expect.any(String),
|
|
51
|
-
officialLanguageCode: expect.any(String),
|
|
52
|
-
officialLanguageNameEn: expect.any(String),
|
|
53
|
-
officialLanguageNameLocal: expect.any(String),
|
|
54
|
-
countryCallingCode: expect.any(String),
|
|
55
|
-
region: expect.any(String),
|
|
56
|
-
flag: expect.any(String),
|
|
57
|
-
};
|
|
58
|
-
data.forEach((country) => {
|
|
59
|
-
expect(country).toMatchObject(shape);
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
});
|