@osimatic/helpers-js 1.5.26 → 1.5.28

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/location.js CHANGED
@@ -2,6 +2,8 @@
2
2
  const Address = require('ilib/lib/Address');
3
3
  const AddressFmt = require('ilib/lib/AddressFmt');
4
4
  const { toEl } = require('./util');
5
+ const isoCountries = require('i18n-iso-countries');
6
+ isoCountries.registerLocale(require('i18n-iso-countries/langs/en.json'));
5
7
 
6
8
  class Country {
7
9
  static setFlagsPath(flagsPath) {
@@ -14,8 +16,11 @@ class Country {
14
16
  static getFlagImg(countryCode) {
15
17
  return '<span><img src="'+Country.getFlagPath(countryCode)+'" alt="" title="'+Country.getCountryName(countryCode)+'" class="flag" /></span>'
16
18
  }
19
+ static getFlagEmoji(countryCode) {
20
+ return [...countryCode.toUpperCase()].map(c => String.fromCodePoint(0x1F1E6 - 65 + c.charCodeAt(0))).join('');
21
+ }
17
22
 
18
- static fillCountrySelect(select, defaultValue=null, countriesList=null, addNoneValue=false, noneLabel='- Aucun -') {
23
+ static fillCountrySelect(select, defaultValue=null, countriesList=null, addNoneValue=false, noneLabel='- Aucun -', showFlags=false) {
19
24
  select = toEl(select);
20
25
  if (!select) {
21
26
  return;
@@ -24,265 +29,21 @@ class Country {
24
29
  if (addNoneValue) {
25
30
  select.insertAdjacentHTML('beforeend', '<option value="">'+noneLabel+'</option>');
26
31
  }
27
- Object.entries(null != countriesList ? countriesList : Country.getCountries()).forEach(([countryCode, countryName]) => select.insertAdjacentHTML('beforeend', '<option value="' + countryCode + '">' + countryName + '</option>'));
32
+ Object.entries(null != countriesList ? countriesList : Country.getCountries()).forEach(([countryCode, countryName]) => {
33
+ const flag = showFlags ? Country.getFlagEmoji(countryCode) + ' ' : '';
34
+ select.insertAdjacentHTML('beforeend', '<option value="' + countryCode + '">' + flag + countryName + '</option>');
35
+ });
28
36
  }
29
37
  if (null != defaultValue) {
30
38
  select.value = defaultValue;
31
39
  }
32
40
  }
33
- static getCountryName(countryCode) {
34
- if (Country.getCountries().hasOwnProperty(countryCode)) {
35
- return Country.getCountries()[countryCode];
36
- }
37
- return countryCode;
41
+ static getCountryName(countryCode, locale = 'en') {
42
+ const name = isoCountries.getName(countryCode, locale);
43
+ return name || countryCode;
38
44
  }
39
- static getCountries() {
40
- return {
41
- AF:'Afghanistan',
42
- AX:'Åland Islands',
43
- AL:'Albania',
44
- DZ:'Algeria',
45
- AS:'American Samoa',
46
- AD:'Andorra',
47
- AO:'Angola',
48
- AI:'Anguilla',
49
- AQ:'Antarctica',
50
- AG:'Antigua and Barbuda',
51
- AR:'Argentina',
52
- AM:'Armenia',
53
- AW:'Aruba',
54
- AU:'Australia',
55
- AT:'Austria',
56
- AZ:'Azerbaijan',
57
- BS:'Bahamas',
58
- BH:'Bahrain',
59
- BD:'Bangladesh',
60
- BB:'Barbados',
61
- BY:'Belarus',
62
- BE:'Belgium',
63
- BZ:'Belize',
64
- BJ:'Benin',
65
- BM:'Bermuda',
66
- BT:'Bhutan',
67
- BO:'Bolivia',
68
- BA:'Bosnia and Herzegovina',
69
- BW:'Botswana',
70
- BV:'Bouvet Island',
71
- BR:'Brazil',
72
- IO:'British Indian Ocean Territory',
73
- BN:'Brunei Darussalam',
74
- BG:'Bulgaria',
75
- BF:'Burkina Faso',
76
- BI:'Burundi',
77
- KH:'Cambodia',
78
- CM:'Cameroon',
79
- CA:'Canada',
80
- CV:'Cape Verde',
81
- KY:'Cayman Islands',
82
- CF:'Central African Republic',
83
- TD:'Chad',
84
- CL:'Chile',
85
- CN:'China',
86
- CX:'Christmas Island',
87
- CC:'Cocos (Keeling) Islands',
88
- CO:'Colombia',
89
- KM:'Comoros',
90
- CG:'Congo',
91
- CD:'Congo, The Democratic Republic of The',
92
- CK:'Cook Islands',
93
- CR:'Costa Rica',
94
- CI:'Cote d’Ivoire',
95
- HR:'Croatia',
96
- CU:'Cuba',
97
- CY:'Cyprus',
98
- CZ:'Czechia',
99
- DK:'Denmark',
100
- DJ:'Djibouti',
101
- DM:'Dominica',
102
- DO:'Dominican Republic',
103
- EC:'Ecuador',
104
- EG:'Egypt',
105
- SV:'El Salvador',
106
- GQ:'Equatorial Guinea',
107
- ER:'Eritrea',
108
- EE:'Estonia',
109
- ET:'Ethiopia',
110
- FK:'Falkland Islands (Malvinas)',
111
- FO:'Faroe Islands',
112
- FJ:'Fiji',
113
- FI:'Finland',
114
- FR:'France',
115
- GF:'French Guiana',
116
- PF:'French Polynesia',
117
- TF:'French Southern Territories',
118
- GA:'Gabon',
119
- GM:'Gambia',
120
- GE:'Georgia',
121
- DE:'Germany',
122
- GH:'Ghana',
123
- GI:'Gibraltar',
124
- GR:'Greece',
125
- GL:'Greenland',
126
- GD:'Grenada',
127
- GP:'Guadeloupe',
128
- GU:'Guam',
129
- GT:'Guatemala',
130
- GG:'Guernsey',
131
- GN:'Guinea',
132
- GW:'Guinea-bissau',
133
- GY:'Guyana',
134
- HT:'Haiti',
135
- HM:'Heard Island and Mcdonald Islands',
136
- VA:'Holy See (Vatican City State)',
137
- HN:'Honduras',
138
- HK:'Hong Kong',
139
- HU:'Hungary',
140
- IS:'Iceland',
141
- IN:'India',
142
- ID:'Indonesia',
143
- IR:'Iran, Islamic Republic of',
144
- IQ:'Iraq',
145
- IE:'Ireland',
146
- IM:'Isle of Man',
147
- IL:'Israel',
148
- IT:'Italy',
149
- JM:'Jamaica',
150
- JP:'Japan',
151
- JE:'Jersey',
152
- JO:'Jordan',
153
- KZ:'Kazakhstan',
154
- KE:'Kenya',
155
- KI:'Kiribati',
156
- KP:'Korea, Democratic People’s Republic of',
157
- KR:'Korea, Republic of',
158
- KW:'Kuwait',
159
- KG:'Kyrgyzstan',
160
- LA:'Lao People’s Democratic Republic',
161
- LV:'Latvia',
162
- LB:'Lebanon',
163
- LS:'Lesotho',
164
- LR:'Liberia',
165
- LY:'Libyan Arab Jamahiriya',
166
- LI:'Liechtenstein',
167
- LT:'Lithuania',
168
- LU:'Luxembourg',
169
- MO:'Macao',
170
- MK:'Macedonia, The Former Yugoslav Republic of',
171
- MG:'Madagascar',
172
- MW:'Malawi',
173
- MY:'Malaysia',
174
- MV:'Maldives',
175
- ML:'Mali',
176
- MT:'Malta',
177
- MH:'Marshall Islands',
178
- MQ:'Martinique',
179
- MR:'Mauritania',
180
- MU:'Mauritius',
181
- YT:'Mayotte',
182
- MX:'Mexico',
183
- FM:'Micronesia, Federated States of',
184
- MD:'Moldova, Republic of',
185
- MC:'Monaco',
186
- MN:'Mongolia',
187
- ME:'Montenegro',
188
- MS:'Montserrat',
189
- MA:'Morocco',
190
- MZ:'Mozambique',
191
- MM:'Myanmar',
192
- NA:'Namibia',
193
- NR:'Nauru',
194
- NP:'Nepal',
195
- NL:'Netherlands',
196
- AN:'Netherlands Antilles',
197
- NC:'New Caledonia',
198
- NZ:'New Zealand',
199
- NI:'Nicaragua',
200
- NE:'Niger',
201
- NG:'Nigeria',
202
- NU:'Niue',
203
- NF:'Norfolk Island',
204
- MP:'Northern Mariana Islands',
205
- NO:'Norway',
206
- OM:'Oman',
207
- PK:'Pakistan',
208
- PW:'Palau',
209
- PS:'Palestinian Territory, Occupied',
210
- PA:'Panama',
211
- PG:'Papua New Guinea',
212
- PY:'Paraguay',
213
- PE:'Peru',
214
- PH:'Philippines',
215
- PN:'Pitcairn',
216
- PL:'Poland',
217
- PT:'Portugal',
218
- PR:'Puerto Rico',
219
- QA:'Qatar',
220
- RE:'Reunion',
221
- RO:'Romania',
222
- RU:'Russian Federation',
223
- RW:'Rwanda',
224
- SH:'Saint Helena',
225
- KN:'Saint Kitts and Nevis',
226
- LC:'Saint Lucia',
227
- PM:'Saint Pierre and Miquelon',
228
- VC:'Saint Vincent and The Grenadines',
229
- WS:'Samoa',
230
- SM:'San Marino',
231
- ST:'Sao Tome and Principe',
232
- SA:'Saudi Arabia',
233
- SN:'Senegal',
234
- RS:'Serbia',
235
- SC:'Seychelles',
236
- SL:'Sierra Leone',
237
- SG:'Singapore',
238
- SK:'Slovakia',
239
- SI:'Slovenia',
240
- SB:'Solomon Islands',
241
- SO:'Somalia',
242
- ZA:'South Africa',
243
- GS:'South Georgia and The South Sandwich Islands',
244
- ES:'Spain',
245
- LK:'Sri Lanka',
246
- SD:'Sudan',
247
- SR:'Suriname',
248
- SJ:'Svalbard and Jan Mayen',
249
- SZ:'Swaziland',
250
- SE:'Sweden',
251
- CH:'Switzerland',
252
- SY:'Syrian Arab Republic',
253
- TW:'Taiwan, Province of China',
254
- TJ:'Tajikistan',
255
- TZ:'Tanzania, United Republic of',
256
- TH:'Thailand',
257
- TL:'Timor-leste',
258
- TG:'Togo',
259
- TK:'Tokelau',
260
- TO:'Tonga',
261
- TT:'Trinidad and Tobago',
262
- TN:'Tunisia',
263
- TR:'Turkey',
264
- TM:'Turkmenistan',
265
- TC:'Turks and Caicos Islands',
266
- TV:'Tuvalu',
267
- UG:'Uganda',
268
- UA:'Ukraine',
269
- AE:'United Arab Emirates',
270
- GB:'United Kingdom',
271
- US:'United States',
272
- UM:'United States Minor Outlying Islands',
273
- UY:'Uruguay',
274
- UZ:'Uzbekistan',
275
- VU:'Vanuatu',
276
- VE:'Venezuela',
277
- VN:'Viet Nam',
278
- VG:'Virgin Islands, British',
279
- VI:'Virgin Islands, U.S.',
280
- WF:'Wallis and Futuna',
281
- EH:'Western Sahara',
282
- YE:'Yemen',
283
- ZM:'Zambia',
284
- ZW:'Zimbabwe',
285
- };
45
+ static getCountries(locale = 'en') {
46
+ return isoCountries.getNames(locale);
286
47
  }
287
48
 
288
49
  static getContinents() {
@@ -329,7 +90,7 @@ class PostalAddress {
329
90
  return autocomplete;
330
91
  }
331
92
 
332
- static format(addressData, separator='<br/>') {
93
+ static format(addressData, separator='<br/>', locale=null) {
333
94
  function empty(value) {
334
95
  return typeof value == 'undefined' || value == null || value === '';
335
96
  }
@@ -365,7 +126,8 @@ class PostalAddress {
365
126
  addressDataForPluging['locality'] = addressData['stateDistrict'];
366
127
  }
367
128
 
368
- let af = new AddressFmt();
129
+ const afLocale = locale || (addressDataForPluging['countryCode'] ? `und-${addressDataForPluging['countryCode']}` : null);
130
+ let af = new AddressFmt(afLocale ? {locale: afLocale} : {});
369
131
  let formattedAddress = af.format(new Address(addressDataForPluging));
370
132
  return formattedAddress.replace(/\n+/g, separator);
371
133
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.5.26",
3
+ "version": "1.5.28",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -14,6 +14,7 @@
14
14
  "description": "",
15
15
  "dependencies": {
16
16
  "deepmerge": "^4.3.1",
17
+ "i18n-iso-countries": "^7.14.0",
17
18
  "ilib": "^14.21",
18
19
  "libphonenumber-js": "^1.12.39",
19
20
  "ua-parser-js": "^2.0.9"
@@ -13,7 +13,7 @@ describe('Country', () => {
13
13
  test('should have correct country names', () => {
14
14
  const countries = Country.getCountries();
15
15
  expect(countries.FR).toBe('France');
16
- expect(countries.US).toBe('United States');
16
+ expect(countries.US).toBe('United States of America');
17
17
  expect(countries.GB).toBe('United Kingdom');
18
18
  });
19
19
  });
@@ -21,7 +21,7 @@ describe('Country', () => {
21
21
  describe('getCountryName', () => {
22
22
  test('should return country name for valid code', () => {
23
23
  expect(Country.getCountryName('FR')).toBe('France');
24
- expect(Country.getCountryName('US')).toBe('United States');
24
+ expect(Country.getCountryName('US')).toBe('United States of America');
25
25
  expect(Country.getCountryName('CA')).toBe('Canada');
26
26
  });
27
27
 
@@ -79,7 +79,7 @@ describe('Country', () => {
79
79
 
80
80
  test('should include country name in title', () => {
81
81
  const html = Country.getFlagImg('US');
82
- expect(html).toContain('title="United States"');
82
+ expect(html).toContain('title="United States of America"');
83
83
  });
84
84
 
85
85
  test('should wrap img in span', () => {
@@ -96,6 +96,27 @@ describe('Country', () => {
96
96
  expect(list).toEqual(countries);
97
97
  });
98
98
  });
99
+
100
+ describe('getFlagEmoji', () => {
101
+ test('should return emoji flag for country code', () => {
102
+ expect(Country.getFlagEmoji('FR')).toBe('🇫🇷');
103
+ expect(Country.getFlagEmoji('US')).toBe('🇺🇸');
104
+ expect(Country.getFlagEmoji('GB')).toBe('🇬🇧');
105
+ });
106
+ test('should handle lowercase country codes', () => {
107
+ expect(Country.getFlagEmoji('fr')).toBe('🇫🇷');
108
+ });
109
+ });
110
+
111
+ describe('getCountries with locale', () => {
112
+ test('should support locale parameter', () => {
113
+ const isoCountries = require('i18n-iso-countries');
114
+ isoCountries.registerLocale(require('i18n-iso-countries/langs/fr.json'));
115
+ const countriesFr = Country.getCountries('fr');
116
+ expect(countriesFr.FR).toBe('France');
117
+ expect(typeof countriesFr.US).toBe('string');
118
+ });
119
+ });
99
120
  });
100
121
 
101
122
  describe('GeographicCoordinates', () => {
@@ -706,5 +727,45 @@ describe('PostalAddress', () => {
706
727
  expect(result).toContain('Mountain View');
707
728
  expect(result).toContain('94043');
708
729
  });
730
+
731
+ test('should place postalCode before locality for FR (countryCode)', () => {
732
+ const result = PostalAddress.format({
733
+ streetAddress: '10 Rue de la Paix',
734
+ postalCode: '75001',
735
+ locality: 'Paris',
736
+ countryCode: 'FR',
737
+ }, '\n');
738
+ expect(result.indexOf('75001')).toBeLessThan(result.indexOf('Paris'));
739
+ });
740
+
741
+ test('should place postalCode before locality for FR (locale parameter, no countryCode)', () => {
742
+ const result = PostalAddress.format({
743
+ streetAddress: '10 Rue de la Paix',
744
+ postalCode: '75001',
745
+ locality: 'Paris',
746
+ }, '\n', 'fr-FR');
747
+ expect(result.indexOf('75001')).toBeLessThan(result.indexOf('Paris'));
748
+ });
749
+
750
+ test('should place postalCode before locality for FR (locale parameter and countryCode)', () => {
751
+ const result = PostalAddress.format({
752
+ streetAddress: '10 Rue de la Paix',
753
+ postalCode: '75001',
754
+ locality: 'Paris',
755
+ countryCode: 'FR',
756
+ }, '\n', 'fr-FR');
757
+ expect(result.indexOf('75001')).toBeLessThan(result.indexOf('Paris'));
758
+ });
759
+
760
+ test('should place postalCode after locality for US', () => {
761
+ const result = PostalAddress.format({
762
+ streetAddress: '1600 Amphitheatre Pkwy',
763
+ postalCode: '94043',
764
+ locality: 'Mountain View',
765
+ state: 'CA',
766
+ countryCode: 'US',
767
+ }, '\n');
768
+ expect(result.indexOf('94043')).toBeGreaterThan(result.indexOf('Mountain View'));
769
+ });
709
770
  });
710
771
  });