country-code-kit 1.0.0 → 1.0.1

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/dist/esm/index.js CHANGED
@@ -2,6 +2,14 @@ import { countries } from "./data.js";
2
2
  export { countries };
3
3
  const byCode = new Map(countries.map((c) => [c.code, c]));
4
4
  const byName = new Map(countries.map((c) => [c.name.toLowerCase(), c]));
5
+ const byDialCode = new Map();
6
+ for (const c of countries) {
7
+ if (c.dialCode) {
8
+ const list = byDialCode.get(c.dialCode) ?? [];
9
+ list.push(c);
10
+ byDialCode.set(c.dialCode, list);
11
+ }
12
+ }
5
13
  /** Get a country by its ISO 3166-1 alpha-2 code (case-insensitive). */
6
14
  export function getCountryByCode(code) {
7
15
  return byCode.get(code.trim().toUpperCase());
@@ -23,6 +31,17 @@ export function getFlag(code) {
23
31
  export function getName(code) {
24
32
  return getCountryByCode(code)?.name;
25
33
  }
34
+ /** Get the dial code (e.g. "+880") for an alpha-2 code. Returns null for territories with no dial code, undefined for unknown codes. */
35
+ export function getDialCode(code) {
36
+ return getCountryByCode(code)?.dialCode;
37
+ }
38
+ /**
39
+ * Get all countries that share a given dial code (e.g. "+1" → US, CA, and many Caribbean territories).
40
+ * The dialCode argument must include the leading "+".
41
+ */
42
+ export function getCountriesByDialCode(dialCode) {
43
+ return byDialCode.get(dialCode.trim()) ?? [];
44
+ }
26
45
  /** Check whether a string is a valid ISO 3166-1 alpha-2 code. */
27
46
  export function isValidCode(code) {
28
47
  return byCode.has(code.trim().toUpperCase());
@@ -6,4 +6,6 @@ export interface Country {
6
6
  name: string;
7
7
  /** Flag emoji, e.g. "🇮🇳" */
8
8
  flag: string;
9
+ /** ITU calling code including +, e.g. "+880". Null for territories with no assigned dial code. */
10
+ dialCode: string | null;
9
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "country-code-kit",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Zero-dependency list of all 249 ISO 3166-1 countries with code, name, and flag emoji. Lookup and search helpers included. Tree-shakeable, works in Node and browsers.",
5
5
  "keywords": [
6
6
  "country",