countrynormalizer 0.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/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # 🌎 Country Data Lookup And Normalizer 🌍
2
+
3
+ A simple library for consolidation and reverse lookup of common ISO country data fields and calling codes all in one place.
4
+
5
+ Developers routinely need to mix and match pieces of country data across an application.
6
+
7
+ Some common examples are:
8
+
9
+ * Retrieve a country's full name based on an ISO Alpha 2 or Alpha 3 code.
10
+ * Turn a user-entered country name back into an ISO Alpha 2 or 3 code for API standardization.
11
+ * Load a country's flag/flag emoji based on a user input or phone number input.
12
+ * Standardize the name of a country or input from many different references to a country.
13
+ * List all countries on a continent for location selection.
14
+ * Find countries that correspond to a particular phone number's calling code.
15
+
16
+ There are lots of good libraries out there that handle one of these functions at a time. Turning ISO 3166-1 alpha-2 into full names, or phone number into country flags, and so on and so forth.
17
+
18
+ It is not uncommon though for application or business use cases to require moving between many of these data points in one application. Yet it can be fairly hard to find all of this data consolidated into a single place.
19
+
20
+ This library aims to solve a lot of that by consolidating these common country lookups into one place.
21
+
22
+ ## Available Lookups:
23
+
24
+ ### Find Country By Unique:
25
+
26
+ The `findCountryByUnique(needle: string | number): AllCountryFields | null` can be used to lookup all data for a country based on a single guaranteed unique field. If no country matches the input the function will return `null`.
27
+
28
+ The guaranteed unique fields are:
29
+
30
+ * ISO 3166-1 number: The unique ISO 3166-1 number values where there are no duplicate values across all countries.
31
+ * ISO 3166-1 alpha-2: Two letter guaranteed unique country code values.
32
+ * ISO 3166-1 alpha-3: Three letter guaranteed unique country code values.
33
+ * `english_clean`: English country names from the ISO 3166 standard as listed on Wikipedia.
34
+ * `formal_order`: The naturally spoken order of a country's formal name from the ISO 3166 standard. For instance, the `english_clean` for the Netherlands is `Netherlands, Kingdom of the`, while the `formal_order` is `Kingdom of the Netherlands` β€” the way the country would be referenced in formal international or political conversation.
35
+ * `common_reference`: Maps country names to how they would be referred to in normal casual conversation. So `Kingdom of the Netherlands` is just `Netherlands`. The `Holy See` is `Vatican City`. You can probably use common sense to arrive at most of these.
36
+ * `flag_emoji`: The Unicode standard dictates that all ISO 3166 countries shall have an emoji flag. As such you can search the emoji of a flag as input and retrieve all data for that country.
37
+
38
+ The returned country structure will match the `AllCountryFields` type and look like this...
39
+
40
+ ```
41
+ {
42
+ common_reference: "United States of America",
43
+ english_clean: "United States of America",
44
+ formal_order: "United States of America",
45
+ alpha_2: "US",
46
+ alpha_3: "USA",
47
+ num_code: 840,
48
+ demonym_male: "American",
49
+ demonym_female: "American",
50
+ gendered_demonym: true,
51
+ tld: ".us",
52
+ flag_emoji: "πŸ‡ΊπŸ‡Έ",
53
+ calling_code: ["1"],
54
+ continent: "North America",
55
+ }
56
+ ```
57
+
58
+ ### Find All Matching Countries:
59
+
60
+ You can use `findAllMatchedCountries(needle: string | number): AllCountryFields[]` to get all countries that match a particular data point.
61
+
62
+ Certain pieces of country data are not unique but still routinely need to be searched for.
63
+
64
+ A common example of this is calling codes. Most of North America uses the `+1` calling code block. Using a `needle` value of `1` or `+1` will return an array of country data like...
65
+
66
+ ```
67
+ {
68
+ "common_reference": "United States of America",
69
+ "english_clean": "United States of America",
70
+ "formal_order": "United States of America",
71
+ "alpha_2": "US",
72
+ "alpha_3": "USA",
73
+ "num_code": 840,
74
+ "demonym_male": "American",
75
+ "demonym_female": "American",
76
+ "gendered_demonym": true,
77
+ "tld": ".us",
78
+ "flag_emoji": "πŸ‡ΊπŸ‡Έ",
79
+ "calling_code": [
80
+ "1"
81
+ ],
82
+ "continent": "North America"
83
+ },
84
+ {
85
+ "common_reference": "United States Minor Outlying Islands",
86
+ "english_clean": "United States Minor Outlying Islands",
87
+ "formal_order": "United States Minor Outlying Islands",
88
+ "alpha_2": "UM",
89
+ "alpha_3": "UMI",
90
+ "num_code": 581,
91
+ "demonym_male": "United States Minor Outlying Islander",
92
+ "demonym_female": "United States Minor Outlying Islander",
93
+ "gendered_demonym": true,
94
+ "tld": ".um",
95
+ "flag_emoji": "πŸ‡ΊπŸ‡²",
96
+ "calling_code": [
97
+ "1"
98
+ ],
99
+ "continent": "Oceania"
100
+ },
101
+ {
102
+ "common_reference": "Canada",
103
+ "english_clean": "Canada",
104
+ "formal_order": "Canada",
105
+ "alpha_2": "CA",
106
+ "alpha_3": "CAN",
107
+ "num_code": 124,
108
+ "demonym_male": "Canadian",
109
+ "demonym_female": "Canadian",
110
+ "gendered_demonym": true,
111
+ "tld": ".ca",
112
+ "flag_emoji": "πŸ‡¨πŸ‡¦",
113
+ "calling_code": [
114
+ "1"
115
+ ],
116
+ "continent": "North America"
117
+ },
118
+
119
+ ```
120
+
121
+ Another use case for this could be if your app has a region-based country selection and you needed all countries in Asia. You could perform a `findAllMatchedCountries('asia')` and get all countries in Asia.
122
+
123
+ This function still supports singular lookup. Entering something like `findAllMatchedCountries('GB')` will return a single item array like...
124
+
125
+ ```
126
+ [{
127
+ "common_reference": "United Kingdom",
128
+ "english_clean": "United Kingdom of Great Britain and Northern Ireland",
129
+ "formal_order": "United Kingdom of Great Britain and Northern Ireland",
130
+ "alpha_2": "GB",
131
+ "alpha_3": "GBR",
132
+ "num_code": 826,
133
+ "demonym_male": "British",
134
+ "demonym_female": "British",
135
+ "gendered_demonym": true,
136
+ "tld": ".gb",
137
+ "flag_emoji": "πŸ‡¬πŸ‡§",
138
+ "calling_code": [
139
+ "44"
140
+ ],
141
+ "continent": "Europe"
142
+ },]
143
+ ```
144
+
145
+ Entering a text value into this function that yields no results will return an empty (`[]`) array.
146
+
147
+ ## To-Do List:
148
+ This package was started as part of language classification and news analytics projects I'm working on in my personal time.
149
+
150
+ I've enjoyed building it out but wanted to publish something for starters so I don't let this languish on my personal computer for too long.
151
+
152
+ Over the next few weeks (hopefully) I plan to build out the following...
153
+
154
+ * tld search support should be added to unique lookups since these are unique values.
155
+ * Optimize the search order of data when performing lookups.
156
+ * Clean up some of the `common_reference` values. A lot of these are executive decisions I quickly made and could be better researched or refined.
157
+ * Provide better documentation on the data sources for each of these fields to help assure people of data validity and no collisions of unique data points.
158
+ * Flesh out more complete unit testing. I have a few running right now checking ISO number values but all data values should be validated.
159
+ * Add size optimized lookups for common operations. Currently the `complete.json` object used for country data is rather large and bloats this library. I want to make smaller, tree-shakable functions to handle only certain data queries on the fly.
160
+ * Set up a small website to visualize all this data in an accessible table for using alongside this library. Right now a lot of it is just compiled in a haphazard Google Doc where I did my initial organizing before translating a CSV into a JSON file.
@@ -0,0 +1,4 @@
1
+ import { findCountryByUnique, findAllMatchedCountries } from "./src/findCountryByUnqiue";
2
+ import type { AllCountryFields } from "./types/core";
3
+ export { findCountryByUnique, findAllMatchedCountries };
4
+ export type { AllCountryFields };