@osimatic/helpers-js 1.5.28 → 1.5.29

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/bank.js CHANGED
@@ -15,7 +15,7 @@ class BankCard {
15
15
  return cardNumber;
16
16
  }
17
17
 
18
- static formatExpirationDate(expirationDate, locale="fr-FR") {
18
+ static formatExpirationDate(expirationDate, locale='fr-FR') {
19
19
  return SqlDateTime.getMonthName(expirationDate, locale)+' '+SqlDateTime.getYear(expirationDate);
20
20
  }
21
21
  }
package/date_time.js CHANGED
@@ -100,27 +100,27 @@ class DateTime {
100
100
  return Math.trunc(jsDate.getTime()/1000);
101
101
  }
102
102
 
103
- static getDateDigitalDisplay(jsDate, locale="fr-FR", timeZone="Europe/Paris") {
103
+ static getDateDigitalDisplay(jsDate, locale='fr-FR', timeZone="Europe/Paris") {
104
104
  return DateTimeFormatter.getDateDigitalFormatter(locale, timeZone).format(jsDate);
105
105
  //return jsDate.toLocaleDateString(locale, {year: 'numeric', month: 'numeric', day: 'numeric', timeZone: timeZone});
106
106
  }
107
107
 
108
- static getDateTextDisplay(jsDate, locale="fr-FR", timeZone="Europe/Paris") {
108
+ static getDateTextDisplay(jsDate, locale='fr-FR', timeZone="Europe/Paris") {
109
109
  return DateTimeFormatter.getDateTextFormatter(locale, timeZone).format(jsDate);
110
110
  //return jsDate.toLocaleDateString(locale, {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', timeZone: timeZone});
111
111
  }
112
112
 
113
- static getTimeDisplay(jsDate, locale="fr-FR", timeZone="Europe/Paris") {
113
+ static getTimeDisplay(jsDate, locale='fr-FR', timeZone="Europe/Paris") {
114
114
  return DateTimeFormatter.getTimeFormatter(locale, timeZone).format(jsDate);
115
115
  //return jsDate.toLocaleTimeString(locale, {hour: 'numeric', minute: 'numeric', timeZone: timeZone});
116
116
  }
117
117
 
118
- static getTimeDigitalDisplay(jsDate, locale="fr-FR", timeZone="Europe/Paris") {
118
+ static getTimeDigitalDisplay(jsDate, locale='fr-FR', timeZone="Europe/Paris") {
119
119
  return DateTimeFormatter.getTimeDigitalFormatter(locale, timeZone).format(jsDate);
120
120
  //return jsDate.toLocaleTimeString(locale, {hour: '2-digit', minute: '2-digit', second: '2-digit', timeZone: timeZone});
121
121
  }
122
122
 
123
- static getTimeDisplayWithNbDays(jsDate, jsPreviousDate, locale="fr-FR", timeZone="Europe/Paris") {
123
+ static getTimeDisplayWithNbDays(jsDate, jsPreviousDate, locale='fr-FR', timeZone="Europe/Paris") {
124
124
  let str = this.getTimeDisplay(jsDate, locale, timeZone);
125
125
  if (jsPreviousDate !== 0 && jsPreviousDate != null) {
126
126
  let nbDaysDiff = DatePeriod.getNbDayBetweenTwo(jsPreviousDate, jsDate, false);
@@ -131,7 +131,7 @@ class DateTime {
131
131
  return str;
132
132
  }
133
133
 
134
- static getDateTimeDigitalDisplay(jsDate, locale="fr-FR", timeZone="Europe/Paris") {
134
+ static getDateTimeDigitalDisplay(jsDate, locale='fr-FR', timeZone="Europe/Paris") {
135
135
  return DateTimeFormatter.getDateTimeFormatter(locale, timeZone).format(jsDate);
136
136
  //return jsDate.toLocaleDateString(locale, {year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', timeZone: timeZone});
137
137
  }
@@ -142,7 +142,7 @@ class DateTime {
142
142
  static getMonth(jsDate) {
143
143
  return jsDate.getUTCMonth()+1;
144
144
  }
145
- static getMonthName(jsDate, locale="fr-FR", isShort=false) {
145
+ static getMonthName(jsDate, locale='fr-FR', isShort=false) {
146
146
  return jsDate.toLocaleDateString(locale, {month: (isShort?'short':'long')});
147
147
  }
148
148
  static getDay(jsDate) {
@@ -157,7 +157,7 @@ class DateTime {
157
157
  return jsDate.getUTCDay();
158
158
  }
159
159
 
160
- static getDayName(jsDate, locale="fr-FR", isShort=false) {
160
+ static getDayName(jsDate, locale='fr-FR', isShort=false) {
161
161
  return jsDate.toLocaleDateString(locale, {weekday: (isShort?'short':'long')});
162
162
  }
163
163
 
@@ -165,13 +165,13 @@ class DateTime {
165
165
  return new Date(Date.UTC(year, month, 0)).getDate();
166
166
  }
167
167
 
168
- static getMonthNameByMonth(month, locale="fr-FR", isShort=false) {
168
+ static getMonthNameByMonth(month, locale='fr-FR', isShort=false) {
169
169
  let d = new Date();
170
170
  d.setDate(1);
171
171
  d.setMonth(month-1);
172
172
  return this.getMonthName(d, locale, isShort);
173
173
  }
174
- static getDayNameByDayOfWeek(dayOfWeek, locale="fr-FR", isShort=false) {
174
+ static getDayNameByDayOfWeek(dayOfWeek, locale='fr-FR', isShort=false) {
175
175
  let d = new Date();
176
176
  // d.setDate(d.getDate() + (1 + 7 - d.getDay()) % 7);
177
177
  d.setDate(d.getDate() + (dayOfWeek - d.getDay()) % 7);
@@ -375,20 +375,20 @@ class TimestampUnix {
375
375
  return parseInt(today.getTime() / 1000);
376
376
  }
377
377
 
378
- static getDateDigitalDisplay(timestamp, locale="fr-FR", timeZone="Europe/Paris") {
378
+ static getDateDigitalDisplay(timestamp, locale='fr-FR', timeZone="Europe/Paris") {
379
379
  return DateTime.getDateDigitalDisplay(this.parse(timestamp), locale, timeZone);
380
380
  }
381
- static getDateTextDisplay(timestamp, locale="fr-FR", timeZone="Europe/Paris") {
381
+ static getDateTextDisplay(timestamp, locale='fr-FR', timeZone="Europe/Paris") {
382
382
  return DateTime.getDateTextDisplay(this.parse(timestamp), locale, timeZone);
383
383
  }
384
384
 
385
- static getTimeDisplay(timestamp, locale="fr-FR", timeZone="Europe/Paris") {
385
+ static getTimeDisplay(timestamp, locale='fr-FR', timeZone="Europe/Paris") {
386
386
  return DateTime.getTimeDisplay(this.parse(timestamp), locale, timeZone);
387
387
  }
388
- static getTimeDisplayWithNbDays(timestamp, previousTimestamp, locale="fr-FR", timeZone="Europe/Paris") {
388
+ static getTimeDisplayWithNbDays(timestamp, previousTimestamp, locale='fr-FR', timeZone="Europe/Paris") {
389
389
  return DateTime.getTimeDisplayWithNbDays(this.parse(timestamp), this.parse(previousTimestamp), locale, timeZone);
390
390
  }
391
- static getTimeDigitalDisplay(timestamp, locale="fr-FR", timeZone="Europe/Paris") {
391
+ static getTimeDigitalDisplay(timestamp, locale='fr-FR', timeZone="Europe/Paris") {
392
392
  return DateTime.getTimeDigitalDisplay(this.parse(timestamp), locale, timeZone);
393
393
  }
394
394
 
@@ -477,10 +477,10 @@ class SqlDate {
477
477
  return DateTime.getSqlDate(new Date());
478
478
  }
479
479
 
480
- static getDateDigitalDisplay(sqlDate, locale="fr-FR", timeZone="Europe/Paris") {
480
+ static getDateDigitalDisplay(sqlDate, locale='fr-FR', timeZone="Europe/Paris") {
481
481
  return SqlDateTime.getDateDigitalDisplay(sqlDate+" 00:00:00", locale, timeZone);
482
482
  }
483
- static getDateTextDisplay(sqlDate, locale="fr-FR", timeZone="Europe/Paris") {
483
+ static getDateTextDisplay(sqlDate, locale='fr-FR', timeZone="Europe/Paris") {
484
484
  return SqlDateTime.getDateTextDisplay(sqlDate+" 00:00:00", locale, timeZone);
485
485
  }
486
486
 
@@ -498,7 +498,7 @@ class SqlDate {
498
498
  static getMonth(sqlDate) {
499
499
  return SqlDateTime.getMonth(sqlDate+" 00:00:00");
500
500
  }
501
- static getMonthName(sqlDate, locale="fr-FR", isShort=false) {
501
+ static getMonthName(sqlDate, locale='fr-FR', isShort=false) {
502
502
  return SqlDateTime.getMonthName(sqlDate+" 00:00:00", locale, isShort);
503
503
  }
504
504
  static getDay(sqlDate) {
@@ -539,13 +539,13 @@ class SqlTime {
539
539
  return DateTime.getSqlTime(new Date());
540
540
  }
541
541
 
542
- static getTimeDisplay(sqlTime, locale="fr-FR", timeZone="Europe/Paris") {
542
+ static getTimeDisplay(sqlTime, locale='fr-FR', timeZone="Europe/Paris") {
543
543
  return SqlDateTime.getTimeDisplay('1970-01-01 '+sqlTime, locale, timeZone);
544
544
  }
545
- static getTimeDigitalDisplay(sqlTime, locale="fr-FR", timeZone="Europe/Paris") {
545
+ static getTimeDigitalDisplay(sqlTime, locale='fr-FR', timeZone="Europe/Paris") {
546
546
  return SqlDateTime.getTimeDigitalDisplay('1970-01-01 '+sqlTime, locale, timeZone);
547
547
  }
548
- static getTimeDisplayWithNbDays(sqlTime, previousSqlTime, locale="fr-FR", timeZone="Europe/Paris") {
548
+ static getTimeDisplayWithNbDays(sqlTime, previousSqlTime, locale='fr-FR', timeZone="Europe/Paris") {
549
549
  return SqlDateTime.getTimeDisplayWithNbDays('1970-01-01 '+sqlTime, '1970-01-01 '+previousSqlTime, locale, timeZone);
550
550
  }
551
551
 
@@ -588,24 +588,24 @@ class SqlDateTime {
588
588
  return new Date(Date.UTC(sqlDateTime.substring(0, 4), sqlDateTime.substring(5, 7)-1, sqlDateTime.substring(8, 10), sqlDateTime.substring(11, 13), sqlDateTime.substring(14, 16), sqlDateTime.substring(17, 19)));
589
589
  }
590
590
 
591
- static getDateDigitalDisplay(sqlDateTime, locale="fr-FR", timeZone="Europe/Paris") {
591
+ static getDateDigitalDisplay(sqlDateTime, locale='fr-FR', timeZone="Europe/Paris") {
592
592
  return DateTime.getDateDigitalDisplay(this.parse(sqlDateTime), locale, timeZone);
593
593
  }
594
- static getDateTextDisplay(sqlDateTime, locale="fr-FR", timeZone="Europe/Paris") {
594
+ static getDateTextDisplay(sqlDateTime, locale='fr-FR', timeZone="Europe/Paris") {
595
595
  return DateTime.getDateTextDisplay(this.parse(sqlDateTime), locale, timeZone);
596
596
  }
597
597
 
598
- static getTimeDisplay(sqlDateTime, locale="fr-FR", timeZone="Europe/Paris") {
598
+ static getTimeDisplay(sqlDateTime, locale='fr-FR', timeZone="Europe/Paris") {
599
599
  return DateTime.getTimeDisplay(this.parse(sqlDateTime), locale, timeZone);
600
600
  }
601
- static getTimeDisplayWithNbDays(sqlDateTime, previousSqlDateTime, locale="fr-FR", timeZone="Europe/Paris") {
601
+ static getTimeDisplayWithNbDays(sqlDateTime, previousSqlDateTime, locale='fr-FR', timeZone="Europe/Paris") {
602
602
  return DateTime.getTimeDisplayWithNbDays(this.parse(sqlDateTime), this.parse(previousSqlDateTime), locale, timeZone);
603
603
  }
604
- static getTimeDigitalDisplay(sqlDateTime, locale="fr-FR", timeZone="Europe/Paris") {
604
+ static getTimeDigitalDisplay(sqlDateTime, locale='fr-FR', timeZone="Europe/Paris") {
605
605
  return DateTime.getTimeDigitalDisplay(this.parse(sqlDateTime), locale, timeZone);
606
606
  }
607
607
 
608
- static getDateTimeDigitalDisplay(sqlDateTime, locale="fr-FR", timeZone="Europe/Paris") {
608
+ static getDateTimeDigitalDisplay(sqlDateTime, locale='fr-FR', timeZone="Europe/Paris") {
609
609
  return DateTime.getDateTimeDigitalDisplay(this.parse(sqlDateTime), locale, timeZone);
610
610
  }
611
611
 
@@ -622,7 +622,7 @@ class SqlDateTime {
622
622
  static getMonth(sqlDateTime) {
623
623
  return DateTime.getMonth(this.parse(sqlDateTime));
624
624
  }
625
- static getMonthName(sqlDateTime, locale="fr-FR", isShort=false) {
625
+ static getMonthName(sqlDateTime, locale='fr-FR', isShort=false) {
626
626
  return DateTime.getMonthName(this.parse(sqlDateTime), locale);
627
627
  }
628
628
  static getDay(sqlDateTime) {
package/location.js CHANGED
@@ -20,7 +20,7 @@ class Country {
20
20
  return [...countryCode.toUpperCase()].map(c => String.fromCodePoint(0x1F1E6 - 65 + c.charCodeAt(0))).join('');
21
21
  }
22
22
 
23
- static fillCountrySelect(select, defaultValue=null, countriesList=null, addNoneValue=false, noneLabel='- Aucun -', showFlags=false) {
23
+ static fillSelect(select, defaultValue=null, showFlags=false, countriesList=null, addNoneValue=false, noneLabel='- Aucun -', locale='fr-FR') {
24
24
  select = toEl(select);
25
25
  if (!select) {
26
26
  return;
@@ -29,20 +29,44 @@ class Country {
29
29
  if (addNoneValue) {
30
30
  select.insertAdjacentHTML('beforeend', '<option value="">'+noneLabel+'</option>');
31
31
  }
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>');
32
+ const allCountries = Country.getCountries(locale);
33
+ const entries = countriesList != null
34
+ ? countriesList.map(code => [code, allCountries[code] || code])
35
+ : Object.entries(allCountries);
36
+ entries.forEach(([countryCode, countryName]) => {
37
+ let attrs = '';
38
+ if (showFlags) {
39
+ if (typeof Country.flagsPath !== 'undefined') {
40
+ attrs = ' data-thumbnail="' + Country.getFlagPath(countryCode) + '"';
41
+ } else {
42
+ attrs = ' data-content="<span class=&quot;fi fi-' + countryCode.toLowerCase() + '&quot;></span> ' + countryName + '"';
43
+ }
44
+ }
45
+ select.insertAdjacentHTML('beforeend', '<option value="' + countryCode + '"' + attrs + '>' + countryName + '</option>');
35
46
  });
36
47
  }
37
48
  if (null != defaultValue) {
38
49
  select.value = defaultValue;
39
50
  }
40
51
  }
41
- static getCountryName(countryCode, locale = 'en') {
52
+
53
+ static fillSelectWithFlags(select, defaultValue=null, countriesList=null, addNoneValue=false, noneLabel='- Aucun -', locale='fr-FR') {
54
+ return Country.fillSelect(select, defaultValue, true, countriesList, addNoneValue, noneLabel, locale);
55
+ }
56
+
57
+ static getCountryName(countryCode, locale='fr-FR') {
42
58
  const name = isoCountries.getName(countryCode, locale);
43
59
  return name || countryCode;
44
60
  }
45
61
  static getCountries(locale = 'en') {
62
+ const names = isoCountries.getNames(locale);
63
+ if (!names || Object.keys(names).length === 0) {
64
+ try {
65
+ isoCountries.registerLocale(require('i18n-iso-countries/langs/' + locale + '.json'));
66
+ } catch (e) {
67
+ // locale not available
68
+ }
69
+ }
46
70
  return isoCountries.getNames(locale);
47
71
  }
48
72
 
@@ -58,10 +82,28 @@ class Country {
58
82
  };
59
83
  }
60
84
 
61
- /** @deprecated **/
85
+ /** @deprecated Use getCountries instead **/
62
86
  static getCountryList() {
63
87
  return Country.getCountries();
64
88
  }
89
+ /** @deprecated Use fillSelect instead **/
90
+ static fillCountrySelect(select, defaultValue=null, countriesList=null, addNoneValue=false, noneLabel='- Aucun -') {
91
+ select = toEl(select);
92
+ if (!select) {
93
+ return;
94
+ }
95
+ if (select.children.length === 0) {
96
+ if (addNoneValue) {
97
+ select.insertAdjacentHTML('beforeend', '<option value="">'+noneLabel+'</option>');
98
+ }
99
+ Object.entries(null != countriesList ? countriesList : Country.getCountries()).forEach(([countryCode, countryName]) => {
100
+ select.insertAdjacentHTML('beforeend', '<option value="' + countryCode + '">' + countryName + '</option>');
101
+ });
102
+ }
103
+ if (null != defaultValue) {
104
+ select.value = defaultValue;
105
+ }
106
+ }
65
107
  }
66
108
 
67
109
  class PostalAddress {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.5.28",
3
+ "version": "1.5.29",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -21,6 +21,7 @@
21
21
  },
22
22
  "peerDependencies": {
23
23
  "datatables.net": ">=2.0",
24
+ "flag-icons": ">=7.0",
24
25
  "intl-tel-input": ">=19.0",
25
26
  "leaflet": ">=1.9",
26
27
  "leaflet-draw": ">=1.0",
@@ -109,13 +109,23 @@ describe('Country', () => {
109
109
  });
110
110
 
111
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'));
112
+ test('should auto-register and return names for an unregistered locale', () => {
115
113
  const countriesFr = Country.getCountries('fr');
114
+ expect(typeof countriesFr).toBe('object');
116
115
  expect(countriesFr.FR).toBe('France');
117
116
  expect(typeof countriesFr.US).toBe('string');
118
117
  });
118
+
119
+ test('should return names for de locale', () => {
120
+ const countriesDe = Country.getCountries('de');
121
+ expect(typeof countriesDe).toBe('object');
122
+ expect(countriesDe.DE).toBeTruthy();
123
+ });
124
+
125
+ test('should return empty object for unknown locale', () => {
126
+ const result = Country.getCountries('xx');
127
+ expect(typeof result).toBe('object');
128
+ });
119
129
  });
120
130
  });
121
131