country-codes-list 1.6.12 → 2.0.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.
@@ -0,0 +1,17 @@
1
+ import groupBy from "./utils/groupBy";
2
+ import { CountryData, CountryProperty } from "./countriesData";
3
+ export type { CountryData, CountryProperty };
4
+ export declare const utils: {
5
+ groupBy: typeof groupBy;
6
+ };
7
+ export declare function all(): CountryData[];
8
+ export declare function filter(countryProperty: CountryProperty, value: string): CountryData[];
9
+ export declare function findOne(countryProperty: CountryProperty, value: string): CountryData | undefined;
10
+ export declare function customArray(fields?: Record<string, string>, { sortBy, sortDataBy, filter: filterFunc, }?: {
11
+ sortBy?: CountryProperty;
12
+ sortDataBy?: CountryProperty;
13
+ filter?: (cd: CountryData) => boolean;
14
+ }): Record<string, string>[];
15
+ export declare function customList(key?: keyof CountryData, label?: string, { filter: filterFunc }?: {
16
+ filter?: (cd: CountryData) => boolean;
17
+ }): Record<string, string>;
package/dist/index.js ADDED
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.customList = exports.customArray = exports.findOne = exports.filter = exports.all = exports.utils = void 0;
7
+ const groupBy_1 = __importDefault(require("./utils/groupBy"));
8
+ const supplant_1 = __importDefault(require("./utils/supplant"));
9
+ const countriesData_1 = __importDefault(require("./countriesData"));
10
+ exports.utils = {
11
+ groupBy: groupBy_1.default,
12
+ };
13
+ function all() {
14
+ return countriesData_1.default;
15
+ }
16
+ exports.all = all;
17
+ function filter(countryProperty, value) {
18
+ return countriesData_1.default.filter((countryData) => countryData[countryProperty] === value);
19
+ }
20
+ exports.filter = filter;
21
+ function findOne(countryProperty, value) {
22
+ return countriesData_1.default.find((countryData) => countryData[countryProperty] === value);
23
+ }
24
+ exports.findOne = findOne;
25
+ function customArray(fields = {
26
+ name: "{countryNameEn} ({countryCode})",
27
+ value: "{countryCode}",
28
+ }, { sortBy, sortDataBy, filter: filterFunc, } = {}) {
29
+ const finalCollection = [];
30
+ let data = countriesData_1.default;
31
+ if (typeof filterFunc === "function") {
32
+ data = data.filter(filterFunc);
33
+ }
34
+ if (sortDataBy) {
35
+ const collator = new Intl.Collator([], { sensitivity: "accent" });
36
+ data.sort((a, b) => collator.compare(a[sortDataBy], b[sortDataBy]));
37
+ }
38
+ data.forEach((countryData) => {
39
+ const collectionObject = {};
40
+ for (const field in fields) {
41
+ collectionObject[field] = (0, supplant_1.default)(fields[field], countryData);
42
+ }
43
+ finalCollection.push(collectionObject);
44
+ });
45
+ if (sortBy && fields[sortBy]) {
46
+ const collator = new Intl.Collator([], { sensitivity: "accent" });
47
+ finalCollection.sort((a, b) => collator.compare(a[sortBy], b[sortBy]));
48
+ }
49
+ return finalCollection;
50
+ }
51
+ exports.customArray = customArray;
52
+ function customList(key = "countryCode", label = "{countryNameEn} ({countryCode})", { filter: filterFunc } = {}) {
53
+ const finalObject = {};
54
+ let data = countriesData_1.default;
55
+ if (typeof filterFunc === "function") {
56
+ data = data.filter(filterFunc);
57
+ }
58
+ data.forEach((countryData) => {
59
+ const value = (0, supplant_1.default)(label, countryData);
60
+ finalObject[String(countryData[key])] = value;
61
+ });
62
+ return finalObject;
63
+ }
64
+ exports.customList = customList;
@@ -0,0 +1,7 @@
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>(array: T[], key: keyof T): Record<string, T[]>;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Groups an array by a specified key.
5
+ * @param array - The array to group.
6
+ * @param key - The key to group the array by.
7
+ * @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.
8
+ */
9
+ function groupBy(array, key) {
10
+ return array.reduce((result, item) => {
11
+ const groupKey = String(item[key]);
12
+ if (!result[groupKey]) {
13
+ result[groupKey] = [];
14
+ }
15
+ result[groupKey].push(item);
16
+ return result;
17
+ }, {});
18
+ }
19
+ exports.default = groupBy;
@@ -0,0 +1,7 @@
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;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Replaces placeholders in a string with values from an object.
5
+ * @param template - The string containing placeholders.
6
+ * @param data - The object containing key-value pairs for replacement.
7
+ * @returns The string with placeholders replaced by values.
8
+ */
9
+ function supplant(template, data) {
10
+ return template.replace(/{([^{}]*)}/g, (match, key) => {
11
+ const value = data[key];
12
+ return typeof value === "string" || typeof value === "number"
13
+ ? value.toString()
14
+ : match;
15
+ });
16
+ }
17
+ exports.default = supplant;
package/package.json CHANGED
@@ -1,37 +1,51 @@
1
1
  {
2
2
  "name": "country-codes-list",
3
- "version": "1.6.12",
4
- "description": "List of codes per country (languages, calling codes, currency codes, etc)",
5
- "main": "index.js",
6
- "types": "./index.d.ts",
3
+ "version": "2.0.0",
4
+ "description": "List of codes per country (languages, calling codes, currency codes, etc) with full TypeScript support.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
7
  "scripts": {
8
- "test": "echo \"Error: no test specified\""
8
+ "build": "tsc",
9
+ "test": "jest",
10
+ "prepublishOnly": "npm run build"
9
11
  },
10
12
  "repository": {
11
13
  "type": "git",
12
- "url": "git+https://github.com/LucianoGanga/country-codes.git"
14
+ "url": "git+https://github.com/Synergy-Shock/country-codes-list.git"
13
15
  },
14
16
  "keywords": [
15
17
  "country",
16
18
  "code",
17
19
  "currency",
18
- "code",
19
- "country",
20
20
  "list",
21
21
  "language",
22
- "per",
23
- "country",
24
- "languages",
25
- "list",
26
22
  "calling",
27
23
  "codes",
24
+ "countries",
25
+ "iso",
28
26
  "phone",
29
- "codes"
27
+ "international",
28
+ "i18n",
29
+ "typescript",
30
+ "tin"
30
31
  ],
31
32
  "author": "Luciano Andrés Ganga Carabante",
32
33
  "license": "MIT",
33
34
  "bugs": {
34
- "url": "https://github.com/LucianoGanga/country-codes/issues"
35
+ "url": "https://github.com/Synergy-Shock/country-codes-list/issues"
36
+ },
37
+ "homepage": "https://github.com/Synergy-Shock/country-codes-list#readme",
38
+ "devDependencies": {
39
+ "@types/jest": "^29.0.0",
40
+ "jest": "^29.0.0",
41
+ "ts-jest": "^29.0.0",
42
+ "typescript": "^4.9.0"
35
43
  },
36
- "homepage": "https://github.com/LucianoGanga/country-codes#readme"
44
+ "jest": {
45
+ "preset": "ts-jest",
46
+ "testEnvironment": "node",
47
+ "testMatch": [
48
+ "**/tests/**/*.test.ts"
49
+ ]
50
+ }
37
51
  }