@samatawy/rules-world 0.1.0 → 0.1.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 +131 -11
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -30,8 +30,34 @@ import { CommonGeographyFunctionsProvider } from '@samatawy/rules-world';
|
|
|
30
30
|
FunctionFactory.registerProvider(CommonGeographyFunctionsProvider);
|
|
31
31
|
|
|
32
32
|
const workspace = new Workspace();
|
|
33
|
-
|
|
34
|
-
workspace.addRule('
|
|
33
|
+
|
|
34
|
+
workspace.addRule('SET applicant.country_code = country_code(applicant.country)');
|
|
35
|
+
|
|
36
|
+
workspace.addRule('IF applicant.country_code != "Unknown" THEN country_known = true');
|
|
37
|
+
|
|
38
|
+
workspace.addRule('IF country_known THEN email_is_ok = country_has_tld(applicant.country_code, applicant.email_tld)');
|
|
39
|
+
|
|
40
|
+
workspace.addRule('IF country_known THEN currency_is_ok = country_uses_currency(applicant.country_code, applicant.currency)');
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
This is especially useful for input validation and fraud screening cases where upstream services have already extracted values such as an email TLD, phone calling code, claimed country, or transaction currency.
|
|
44
|
+
|
|
45
|
+
For example, after extracting `.ca` from an email domain and `CAD` from a payment payload, your rules can validate whether those values are plausible for the claimed country.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
const context = workspace.loadContext({
|
|
49
|
+
applicant: {
|
|
50
|
+
country: 'Canada',
|
|
51
|
+
email_tld: '.ca',
|
|
52
|
+
currency: 'CAD',
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
workspace.process(context);
|
|
57
|
+
|
|
58
|
+
console.log(context.getOutput('applicant.country_code')); // CA
|
|
59
|
+
console.log(context.getOutput('email_is_ok')); // true
|
|
60
|
+
console.log(context.getOutput('currency_is_ok')); // true
|
|
35
61
|
```
|
|
36
62
|
|
|
37
63
|
You can also register the provider through the package helper:
|
|
@@ -57,20 +83,114 @@ The same package entry works in both Node.js and browser builds, with ESM and Co
|
|
|
57
83
|
- `Continents`
|
|
58
84
|
- `Timezones`
|
|
59
85
|
|
|
60
|
-
##
|
|
86
|
+
## Included Geography Functions
|
|
87
|
+
|
|
88
|
+
The provider includes lookup, metadata, reverse-lookup, and validation helpers built on the packaged world dataset.
|
|
89
|
+
|
|
90
|
+
### Core Lookup and Metadata
|
|
91
|
+
|
|
92
|
+
Use these to normalize a country code first, then read related metadata.
|
|
93
|
+
|
|
94
|
+
- `country_code(value)` resolves a 2-letter country code from a country name, alias, or ISO code when the input is uniquely identifiable.
|
|
95
|
+
|
|
96
|
+
This allows you to use the other functions that expect a valid 2-letter code.
|
|
97
|
+
|
|
98
|
+
- `country_name(code)` and `official_country_name(code)` return human-readable country names.
|
|
99
|
+
- `capital_of(code)`, `continent_of(code)`, and `country_subregion(code)` return broad location metadata.
|
|
100
|
+
- `three_letter_code(code)` return ISO-style code forms from a two-letter code.
|
|
101
|
+
|
|
102
|
+
Example:
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
set country_code = country_code(applicant.country)
|
|
106
|
+
|
|
107
|
+
set official = official_country_name(country_code)
|
|
108
|
+
|
|
109
|
+
set continent = continent_of(country_code)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Currency, TLD, Calling Code, and Language Checks
|
|
113
|
+
|
|
114
|
+
These are the most useful validation-oriented helpers when checking whether user-supplied values fit the claimed country.
|
|
115
|
+
|
|
116
|
+
- `country_uses_currency(code, currency)` checks a currency code, currency symbol, or currency name against a country.
|
|
117
|
+
- `country_has_tld(code, tld)` checks whether a country includes the given top-level domain.
|
|
118
|
+
- `country_has_calling_code(code, callingCode)` checks whether a country includes a phone calling code.
|
|
119
|
+
- `country_speaks_language(code, language)` checks whether a language is listed for a country.
|
|
120
|
+
|
|
121
|
+
Example:
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
IF applicant.country_code != "Unknown" THEN currency_is_ok = country_uses_currency(applicant.country_code, applicant.currency)
|
|
125
|
+
|
|
126
|
+
IF applicant.country_code != "Unknown" THEN email_is_ok = country_has_tld(applicant.country_code, applicant.email_tld)
|
|
127
|
+
|
|
128
|
+
IF applicant.country_code != "Unknown" THEN calling_code_is_ok = country_has_calling_code(applicant.country_code, applicant.phone_calling_code)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Country Property Lists
|
|
132
|
+
|
|
133
|
+
These return arrays of values associated with a country.
|
|
61
134
|
|
|
62
|
-
- `country_codes()`
|
|
63
|
-
- `country_code(value)`
|
|
64
|
-
- `country_name(code)`
|
|
65
|
-
- `official_country_name(code)`
|
|
66
135
|
- `country_calling_codes(code)`
|
|
67
|
-
- `country_top_level_domains(code)`
|
|
136
|
+
- `country_tlds(code)` and `country_top_level_domains(code)`
|
|
68
137
|
- `country_languages(code)`
|
|
69
138
|
- `country_timezones(code)`
|
|
139
|
+
- `country_member_of(code)`
|
|
140
|
+
- `country_part_of(code)`
|
|
141
|
+
|
|
142
|
+
Example:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
set allowed_tlds = country_top_level_domains(applicant.country_code)
|
|
146
|
+
|
|
147
|
+
set languages = country_languages(applicant.country_code)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Global Lists and Reverse Group Lookups
|
|
151
|
+
|
|
152
|
+
These are helpful for building dropdowns, admin tools, filters, or reporting views.
|
|
153
|
+
|
|
154
|
+
- `country_codes()`, `currency_codes()`, `languages()`, `continents()`, `timezones()`
|
|
70
155
|
- `countries_in_continent(continent)`
|
|
71
156
|
- `countries_in_subregion(subregion)`
|
|
157
|
+
- `countries_in_timezone(timezone)`
|
|
158
|
+
- `countries_speaking_language(language)`
|
|
72
159
|
- `countries_using_currency(currency)`
|
|
73
|
-
- `
|
|
74
|
-
- `
|
|
75
|
-
|
|
160
|
+
- `member_countries(group)`
|
|
161
|
+
- `countries_part_of(group)`
|
|
162
|
+
|
|
163
|
+
Example:
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
set euro_countries = countries_using_currency("EUR")
|
|
167
|
+
|
|
168
|
+
set eu_timezones = countries_in_timezone("UTC+01:00")
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Membership and Boolean Country Facts
|
|
172
|
+
|
|
173
|
+
These answer yes/no questions directly.
|
|
174
|
+
|
|
175
|
+
- `country_is_member_of(code, group)`
|
|
176
|
+
- `country_is_part_of(code, group)`
|
|
177
|
+
- `country_in_continent(code, continent)`
|
|
76
178
|
- `country_in_timezone(code, timezone)`
|
|
179
|
+
- `country_is_independent(code)`
|
|
180
|
+
- `country_is_un_member(code)`
|
|
181
|
+
|
|
182
|
+
Example:
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
IF country_is_un_member(applicant.country_code) THEN applicant.recognized_state = true
|
|
186
|
+
|
|
187
|
+
IF country_in_continent(applicant.country_code, "Europe") THEN applicant.region = "EMEA"
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Additional Metadata Helpers
|
|
191
|
+
|
|
192
|
+
- `currency_of(code)`, `currency_name_of(code)`, `currency_symbol_of(code)`
|
|
193
|
+
- `driving_side(code)`
|
|
194
|
+
- `system_of_government(code)`
|
|
195
|
+
|
|
196
|
+
These are useful when rules need world metadata for routing, enrichment, or display, not only validation.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samatawy/rules-world",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "World and geography-oriented function providers for @samatawy/rules, including country metadata and lookup helpers.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"prepublishOnly": "npm run lint && npm run test && npm run build"
|
|
36
36
|
},
|
|
37
37
|
"keywords": [
|
|
38
|
+
"@samatawy/rules",
|
|
38
39
|
"rules-engine",
|
|
39
40
|
"plugin",
|
|
40
41
|
"world",
|
|
@@ -57,7 +58,7 @@
|
|
|
57
58
|
},
|
|
58
59
|
"homepage": "https://samatawy.github.io/rules-world/",
|
|
59
60
|
"peerDependencies": {
|
|
60
|
-
"@samatawy/rules": "^0.
|
|
61
|
+
"@samatawy/rules": "^0.4.0"
|
|
61
62
|
},
|
|
62
63
|
"devDependencies": {
|
|
63
64
|
"@samatawy/rules": "file:../rules",
|