@osimatic/helpers-js 1.4.23 → 1.4.25

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.
Files changed (129) hide show
  1. package/.claude/settings.local.json +13 -0
  2. package/CHANGELOG +2 -1
  3. package/array.js +2 -2
  4. package/bank.js +3 -3
  5. package/chartjs.js +1 -1
  6. package/contact_details.js +2 -2
  7. package/date_time.js +25 -16
  8. package/draw.js +3 -2
  9. package/duration.js +12 -15
  10. package/event_bus.js +2 -2
  11. package/file.js +1 -1
  12. package/form_helper.js +1 -1
  13. package/http_client.js +2 -0
  14. package/jwt.js +18 -6
  15. package/location.js +6 -2
  16. package/media.js +1 -1
  17. package/number.js +2 -3
  18. package/package.json +28 -2
  19. package/social_network.js +5 -0
  20. package/string.js +11 -2
  21. package/tests/array.test.js +458 -0
  22. package/tests/bank.test.js +158 -0
  23. package/tests/chartjs.test.js +273 -0
  24. package/tests/contact_details.test.js +391 -0
  25. package/tests/date_time/DatePeriod.test.js +179 -0
  26. package/tests/date_time/DateTime.test.js +492 -0
  27. package/tests/date_time/SqlDate.test.js +205 -0
  28. package/tests/date_time/SqlDateTime.test.js +326 -0
  29. package/tests/date_time/SqlTime.test.js +162 -0
  30. package/tests/date_time/TimestampUnix.test.js +262 -0
  31. package/tests/draw.test.js +271 -0
  32. package/tests/duration.test.js +365 -0
  33. package/tests/event_bus.test.js +268 -0
  34. package/tests/file.test.js +358 -0
  35. package/tests/form_date.test.js +417 -0
  36. package/tests/form_helper.test.js +415 -0
  37. package/tests/http_client.test.js +570 -0
  38. package/tests/jwt.test.js +804 -0
  39. package/tests/location.test.js +509 -0
  40. package/tests/media.test.js +458 -0
  41. package/tests/network.test.js +489 -0
  42. package/tests/number.test.js +448 -0
  43. package/tests/open_street_map.test.js +388 -0
  44. package/tests/shopping_cart.test.js +355 -0
  45. package/tests/social_network.test.js +333 -0
  46. package/tests/string.test.js +473 -0
  47. package/tests/user.test.js +204 -0
  48. package/tests/util.test.js +99 -0
  49. package/tests/visitor.test.js +508 -0
  50. package/tmpclaude-00a6-cwd +1 -0
  51. package/tmpclaude-0526-cwd +1 -0
  52. package/tmpclaude-0973-cwd +1 -0
  53. package/tmpclaude-0b61-cwd +1 -0
  54. package/tmpclaude-0fa4-cwd +1 -0
  55. package/tmpclaude-104f-cwd +1 -0
  56. package/tmpclaude-1468-cwd +1 -0
  57. package/tmpclaude-146f-cwd +1 -0
  58. package/tmpclaude-223d-cwd +1 -0
  59. package/tmpclaude-2330-cwd +1 -0
  60. package/tmpclaude-282a-cwd +1 -0
  61. package/tmpclaude-2846-cwd +1 -0
  62. package/tmpclaude-28a6-cwd +1 -0
  63. package/tmpclaude-2b5a-cwd +1 -0
  64. package/tmpclaude-2def-cwd +1 -0
  65. package/tmpclaude-324b-cwd +1 -0
  66. package/tmpclaude-35d3-cwd +1 -0
  67. package/tmpclaude-3906-cwd +1 -0
  68. package/tmpclaude-3b32-cwd +1 -0
  69. package/tmpclaude-3da9-cwd +1 -0
  70. package/tmpclaude-3dc3-cwd +1 -0
  71. package/tmpclaude-3e3b-cwd +1 -0
  72. package/tmpclaude-43b6-cwd +1 -0
  73. package/tmpclaude-4495-cwd +1 -0
  74. package/tmpclaude-462f-cwd +1 -0
  75. package/tmpclaude-4aa8-cwd +1 -0
  76. package/tmpclaude-4b29-cwd +1 -0
  77. package/tmpclaude-4db5-cwd +1 -0
  78. package/tmpclaude-4e01-cwd +1 -0
  79. package/tmpclaude-5101-cwd +1 -0
  80. package/tmpclaude-524f-cwd +1 -0
  81. package/tmpclaude-5636-cwd +1 -0
  82. package/tmpclaude-5cdd-cwd +1 -0
  83. package/tmpclaude-5f1f-cwd +1 -0
  84. package/tmpclaude-6078-cwd +1 -0
  85. package/tmpclaude-622e-cwd +1 -0
  86. package/tmpclaude-6802-cwd +1 -0
  87. package/tmpclaude-6e36-cwd +1 -0
  88. package/tmpclaude-7793-cwd +1 -0
  89. package/tmpclaude-7f96-cwd +1 -0
  90. package/tmpclaude-8566-cwd +1 -0
  91. package/tmpclaude-8874-cwd +1 -0
  92. package/tmpclaude-8915-cwd +1 -0
  93. package/tmpclaude-8c8b-cwd +1 -0
  94. package/tmpclaude-94df-cwd +1 -0
  95. package/tmpclaude-9859-cwd +1 -0
  96. package/tmpclaude-9ac5-cwd +1 -0
  97. package/tmpclaude-9f18-cwd +1 -0
  98. package/tmpclaude-a202-cwd +1 -0
  99. package/tmpclaude-a741-cwd +1 -0
  100. package/tmpclaude-ab5f-cwd +1 -0
  101. package/tmpclaude-b008-cwd +1 -0
  102. package/tmpclaude-b0a1-cwd +1 -0
  103. package/tmpclaude-b63d-cwd +1 -0
  104. package/tmpclaude-b681-cwd +1 -0
  105. package/tmpclaude-b72d-cwd +1 -0
  106. package/tmpclaude-b92f-cwd +1 -0
  107. package/tmpclaude-bc49-cwd +1 -0
  108. package/tmpclaude-bc50-cwd +1 -0
  109. package/tmpclaude-bccf-cwd +1 -0
  110. package/tmpclaude-be55-cwd +1 -0
  111. package/tmpclaude-c228-cwd +1 -0
  112. package/tmpclaude-c717-cwd +1 -0
  113. package/tmpclaude-c7ce-cwd +1 -0
  114. package/tmpclaude-cf3e-cwd +1 -0
  115. package/tmpclaude-d142-cwd +1 -0
  116. package/tmpclaude-d5bc-cwd +1 -0
  117. package/tmpclaude-d6ae-cwd +1 -0
  118. package/tmpclaude-d77a-cwd +1 -0
  119. package/tmpclaude-d8da-cwd +1 -0
  120. package/tmpclaude-dbdb-cwd +1 -0
  121. package/tmpclaude-de61-cwd +1 -0
  122. package/tmpclaude-de81-cwd +1 -0
  123. package/tmpclaude-df9d-cwd +1 -0
  124. package/tmpclaude-e786-cwd +1 -0
  125. package/tmpclaude-f01d-cwd +1 -0
  126. package/tmpclaude-f2a9-cwd +1 -0
  127. package/tmpclaude-fc36-cwd +1 -0
  128. package/tmpclaude-ffef-cwd +1 -0
  129. package/visitor.js +2 -2
@@ -0,0 +1,13 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(npm test)",
5
+ "Bash(npm test:*)",
6
+ "Bash(npm run test:coverage:*)",
7
+ "Bash(npm login)",
8
+ "Bash(npm publish:*)",
9
+ "Bash(npm logout:*)",
10
+ "Bash(node -e:*)",
11
+ ]
12
+ }
13
+ }
package/CHANGELOG CHANGED
@@ -79,4 +79,5 @@ PostalAddress.setAutocomplete(input, onChange(formatted_address)) -> PostalAddre
79
79
 
80
80
  1.4.0
81
81
  new OpenStreetMap(id) -> new OpenStreetMap(element)
82
- OpenStreetMap.centerOnFrance -> OpenStreetMap.centerMapOnCountry('FR')
82
+ OpenStreetMap.centerOnFrance -> OpenStreetMap.centerMapOnCountry('FR')
83
+ const flagsPath = 'xxx'; -> Country.setFlagsPath('xxx');
package/array.js CHANGED
@@ -31,7 +31,7 @@ Array.prototype.unique = function() {
31
31
  };
32
32
 
33
33
  Array.prototype.removeEmptyValues = function() {
34
- return this.filter(val => val != '');
34
+ return this.filter(val => val !== '');
35
35
  };
36
36
 
37
37
  Array.prototype.hasOwnIndex = function(prop) {
@@ -64,7 +64,7 @@ Array.generate = ({from = 0, to, step = 1, length = Math.ceil((to - from) / step
64
64
 
65
65
  Array.getValuesByKeyInArrayOfArrays = function(array, key) {
66
66
  let listeValues = [];
67
- for (let i in array) {
67
+ for (let i = 0; i < array.length; i++) {
68
68
  let subArray = array[i];
69
69
  if (typeof(subArray[key]) != 'undefined') {
70
70
  listeValues.push(subArray[key]);
package/bank.js CHANGED
@@ -1,14 +1,14 @@
1
1
  class IBAN {
2
2
  static format(iban) {
3
- return iban.replace(/[^\dA-Z]/g, '').replace(/(.{4})/g, '$1 ').trim();
3
+ return iban.toUpperCase().replace(/[^\dA-Z]/g, '').replace(/(.{4})/g, '$1 ').trim();
4
4
  }
5
5
  }
6
6
 
7
7
  class BankCard {
8
- static formatCardNumber(cardNumber) {
8
+ static formatCardNumber(cardNumber, hiddenChar='*') {
9
9
  if (cardNumber.length === 16) {
10
10
  cardNumber = cardNumber.substring(0, 4)+'-'+cardNumber.substring(4, 8)+'-'+cardNumber.substring(8, 12)+'-'+cardNumber.substring(12, 16);
11
- cardNumber = cardNumber.replace(/(\*)/gi, 'X');
11
+ cardNumber = cardNumber.replace(/[*X]/gi, hiddenChar);
12
12
  }
13
13
  return cardNumber;
14
14
  }
package/chartjs.js CHANGED
@@ -298,7 +298,7 @@ class Chartjs {
298
298
  }
299
299
 
300
300
  static getAutoGranularity(data) {
301
- const dates = Object.keys(data);
301
+ const dates = Object.keys(data).sort();
302
302
  const days = (new Date(dates[dates.length - 1]) - new Date(dates[0])) / (1000 * 60 * 60 * 24);
303
303
  if (days > 90) return 'month';
304
304
  if (days > 30) return 'week';
@@ -4,10 +4,10 @@ class PersonName {
4
4
  static format(firstName, lastName) {
5
5
  let str = '';
6
6
  if (firstName != null && firstName !== '') {
7
- str += ' '+firstName;
7
+ str += ' '+firstName.trim();
8
8
  }
9
9
  if (lastName != null && lastName !== '') {
10
- str += ' '+lastName;
10
+ str += ' '+lastName.trim();
11
11
  }
12
12
  return str.trim();
13
13
  }
package/date_time.js CHANGED
@@ -206,9 +206,7 @@ class DateTime {
206
206
  }
207
207
 
208
208
  static getLastDayOfYear(date) {
209
- date.setUTCDate(31);
210
- date.setUTCMonth(11);
211
- return new Date(date);
209
+ return new Date(Date.UTC(date.getUTCFullYear(), 11, 31));
212
210
  }
213
211
 
214
212
  static getFirstDayOfWeekAndYear(year, week) {
@@ -308,7 +306,7 @@ class DatePeriod {
308
306
  return partOfDay === DatePeriod.PART_OF_DAY_MORNING || partOfDay === DatePeriod.PART_OF_DAY_AFTERNOON;
309
307
  }
310
308
 
311
- static getNbDayBetweenTwo(jsDate1, jsDate2, asPeriod=false, timeZone="Europe/Paris") {
309
+ static getNbDayBetweenTwo(jsDate1, jsDate2, considerTime=false, timeZone="Europe/Paris", returnDecimal=false) {
312
310
  //jsDate1.set
313
311
  if (jsDate1 == null || jsDate2 == null) {
314
312
  return 0;
@@ -317,7 +315,7 @@ class DatePeriod {
317
315
  let timestamp1 = jsDate1.getTime() / 1000;
318
316
  let timestamp2 = jsDate2.getTime() / 1000;
319
317
 
320
- if (!asPeriod) {
318
+ if (!considerTime) {
321
319
  let jsMidnightDate1 = new Date(jsDate1.toLocaleDateString('en-US', {timeZone: timeZone})+' 00:00:00');
322
320
  let jsMidnightDate2 = new Date(jsDate2.toLocaleDateString('en-US', {timeZone: timeZone})+' 00:00:00');
323
321
  timestamp1 = jsMidnightDate1.getTime() / 1000;
@@ -327,7 +325,9 @@ class DatePeriod {
327
325
  //jsDate1.setUTCHours(0, 0, 0);
328
326
  //jsDate2.setUTCHours(0, 0, 0);
329
327
  }
330
- return parseInt(Math.round((timestamp2-timestamp1)/86400));
328
+
329
+ const daysDiff = (timestamp2-timestamp1)/86400;
330
+ return returnDecimal ? daysDiff : parseInt(Math.round(daysDiff));
331
331
  }
332
332
 
333
333
  static getPeriodLabels(data, period, locale = 'fr-FR', timeZone = 'Europe/Paris') {
@@ -392,23 +392,32 @@ class TimestampUnix {
392
392
  }
393
393
 
394
394
  static getYear(timestamp, timeZone="Europe/Paris") {
395
- return this.parse(timestamp).toLocaleDateString('fr-FR', {year: 'numeric', timeZone: timeZone});
395
+ //const sqlDate = this.getSqlDate(timestamp, timeZone);
396
+ //return parseInt(sqlDate.substring(0, 4));
397
+ return parseInt(this.parse(timestamp).toLocaleDateString('fr-FR', {year: 'numeric', timeZone: timeZone}));
396
398
  }
397
399
  static getMonth(timestamp, timeZone="Europe/Paris") {
398
- return this.parse(timestamp).toLocaleDateString('fr-FR', {month: 'numeric', timeZone: timeZone});
400
+ //const sqlDate = this.getSqlDate(timestamp, timeZone);
401
+ //return parseInt(sqlDate.substring(5, 7));
402
+ return parseInt(this.parse(timestamp).toLocaleDateString('fr-FR', {month: 'numeric', timeZone: timeZone}));
399
403
  }
400
404
  static getDayOfMonth(timestamp, timeZone="Europe/Paris") {
401
- return this.parse(timestamp).toLocaleDateString('fr-FR', {day: 'numeric', timeZone: timeZone});
405
+ //const sqlDate = this.getSqlDate(timestamp, timeZone);
406
+ //return parseInt(sqlDate.substring(8, 10));
407
+ return parseInt(this.parse(timestamp).toLocaleDateString('fr-FR', {day: 'numeric', timeZone: timeZone}));
402
408
  }
403
409
 
404
410
  static getHour(timestamp, timeZone="Europe/Paris") {
405
- return this.parse(timestamp).toLocaleTimeString('en-GB', {hour: 'numeric', timeZone: timeZone});
411
+ const sqlTime = this.getSqlTime(timestamp, timeZone);
412
+ return parseInt(sqlTime.substring(0, 2));
406
413
  }
407
414
  static getMinute(timestamp, timeZone="Europe/Paris") {
408
- return this.parse(timestamp).toLocaleTimeString('en-GB', {minute: 'numeric', timeZone: timeZone});
415
+ const sqlTime = this.getSqlTime(timestamp, timeZone);
416
+ return parseInt(sqlTime.substring(3, 5));
409
417
  }
410
418
  static getSecond(timestamp, timeZone="Europe/Paris") {
411
- return this.parse(timestamp).toLocaleTimeString('en-GB', {second: 'numeric', timeZone: timeZone});
419
+ const sqlTime = this.getSqlTime(timestamp, timeZone);
420
+ return parseInt(sqlTime.substring(6, 8));
412
421
  }
413
422
 
414
423
  static getSqlDateTime(timestamp, timeZone="Europe/Paris") {
@@ -556,18 +565,18 @@ class SqlDateTime {
556
565
  return DateTime.getSqlDateTime(new Date());
557
566
  }
558
567
 
559
- static getSqlDate(sqlDateTime) {
568
+ static getSqlDate(sqlDateTime, timeZone="UTC") {
560
569
  if (sqlDateTime == null) {
561
570
  return null;
562
571
  }
563
- return DateTime.getSqlDate(this.parse(sqlDateTime));
572
+ return DateTime.getSqlDate(this.parse(sqlDateTime), timeZone);
564
573
  }
565
574
 
566
- static getSqlTime(sqlDateTime) {
575
+ static getSqlTime(sqlDateTime, timeZone="UTC") {
567
576
  if (sqlDateTime == null) {
568
577
  return null;
569
578
  }
570
- return DateTime.getSqlTime(this.parse(sqlDateTime));
579
+ return DateTime.getSqlTime(this.parse(sqlDateTime), timeZone);
571
580
  }
572
581
 
573
582
  static parse(sqlDateTime) {
package/draw.js CHANGED
@@ -1,6 +1,7 @@
1
1
  class HexColor {
2
2
  static check(hexColor) {
3
- return hexColor.match(/^[0-9a-fA-F]{6}$/).length;
3
+ const match = hexColor.match(/^[0-9a-fA-F]{6}$/);
4
+ return match ? match.length > 0 : false;
4
5
  }
5
6
 
6
7
  static convertToRgb(hexColor) {
@@ -9,7 +10,7 @@ class HexColor {
9
10
  }
10
11
 
11
12
  if (hexColor.length === 3) {
12
- hexColor = hexColor+hexColor;
13
+ hexColor = hexColor[0]+hexColor[0]+hexColor[1]+hexColor[1]+hexColor[2]+hexColor[2];
13
14
  }
14
15
 
15
16
  if (!HexColor.check(hexColor)) {
package/duration.js CHANGED
@@ -72,30 +72,24 @@ class Duration {
72
72
  if (withMinutes) {
73
73
  let nbMinutes = this.getNbMinutesRemainingOfDurationInSeconds(durationInSeconds);
74
74
  strMinute += ' ';
75
- //strMinute += sprintf('%02d', nbMinutes);
76
- strMinute += nbMinutes.toString().padStart(2, '0');
77
- if (withMinuteLabel) {
78
- if (fullLabel) {
79
- strMinute += ' minute'+(nbMinutes>1?'s':'');
80
- }
81
- else {
82
- strMinute += 'min';
83
- }
75
+ if (fullLabel) {
76
+ strMinute += nbMinutes.toString()+(withMinuteLabel ? ' minute'+(nbMinutes>1?'s':'') : '');
77
+ }
78
+ else {
79
+ strMinute += nbMinutes.toString().padStart(2, '0')+(withMinuteLabel ? 'min' : '');
84
80
  }
85
81
  }
86
-
82
+
87
83
  // Secondes
88
84
  let strSeconde = '';
89
85
  if (withSeconds) {
90
86
  let nbSecondes = this.getNbSecondsRemainingOfDurationInSeconds(durationInSeconds);
91
87
  strSeconde += ' ';
92
- //strSeconde += sprintf('%02d', nbSecondes);
93
- strSeconde += nbSecondes.toString().padStart(2, '0');
94
88
  if (fullLabel) {
95
- strSeconde += ' seconde'+(nbSecondes>1?'s':'');
89
+ strSeconde += nbSecondes.toString()+' seconde'+(nbSecondes>1?'s':'');
96
90
  }
97
91
  else {
98
- strSeconde += 's';
92
+ strSeconde += nbSecondes.toString().padStart(2, '0')+'s';
99
93
  }
100
94
  }
101
95
 
@@ -191,7 +185,10 @@ class Duration {
191
185
  }
192
186
 
193
187
  static getNbMinutesOfHundredthOfAnHour(durationAsHundredthOfAnHour) {
194
- return Math.floor(Math.getDecimals(Number.roundDecimal(durationAsHundredthOfAnHour, 2)) / 100 * 60);
188
+ // Extraire la partie décimale et convertir en minutes
189
+ // Ex: 1.5h -> 0.5 * 60 = 30 minutes
190
+ const decimalPart = durationAsHundredthOfAnHour - Math.floor(durationAsHundredthOfAnHour);
191
+ return Math.floor(decimalPart * 60);
195
192
  }
196
193
 
197
194
 
package/event_bus.js CHANGED
@@ -30,9 +30,9 @@ class EventBus {
30
30
 
31
31
  if (!!handlers === false) {
32
32
  return;
33
- }
33
+ }
34
34
 
35
- handlers.splice(handlers.indexOf(handler));
35
+ handlers.splice(handlers.indexOf(handler), 1);
36
36
  }
37
37
  }
38
38
 
package/file.js CHANGED
@@ -52,7 +52,7 @@ class File {
52
52
  fileSizeInBytes = fileSizeInBytes / 1024;
53
53
  i++;
54
54
  }
55
- while (fileSizeInBytes > 1024);
55
+ while (fileSizeInBytes >= 1024);
56
56
  //var size = Math.max(fileSizeInBytes, 0.1).toFixed(fractionDigits);
57
57
  let size = Math.max(fileSizeInBytes, 0.1);
58
58
  return (new Intl.NumberFormat(locale, {
package/form_helper.js CHANGED
@@ -294,7 +294,7 @@ class FormHelper {
294
294
  if (typeof errors[property] == 'function') {
295
295
  continue;
296
296
  }
297
- if (typeof errors[property]['error_description'] !== 'undefined') {
297
+ if (errors[property] != null && typeof errors[property]['error_description'] !== 'undefined') {
298
298
  errorLabels.push(errors[property]['error_description']);
299
299
  continue;
300
300
  }
package/http_client.js CHANGED
@@ -1,3 +1,5 @@
1
+ const { JwtSession } = require('./jwt');
2
+ const { UrlAndQueryString } = require('./network');
1
3
 
2
4
  class HTTPClient {
3
5
  static setAuthorizationToken(authorizationToken) {
package/jwt.js CHANGED
@@ -2,9 +2,15 @@ class JwtToken {
2
2
  static parseJwt (token) {
3
3
  let base64Url = token.split('.')[1];
4
4
  let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
5
- let jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
6
- return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
7
- }).join(''));
5
+ let jsonPayload;
6
+ try {
7
+ jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
8
+ return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
9
+ }).join(''));
10
+ } catch (e) {
11
+ // Fallback for characters that can't be decoded
12
+ jsonPayload = atob(base64);
13
+ }
8
14
 
9
15
  return JSON.parse(jsonPayload);
10
16
  }
@@ -75,7 +81,9 @@ class JwtSession {
75
81
  static login(data, redirectUrl, onComplete) {
76
82
  console.log('JwtSession.login()');
77
83
  JwtSession.setToken(data['access_token'] || data['token']);
78
- JwtSession.setRefreshToken(data['refresh_token']);
84
+ if (data['refresh_token'] != null) {
85
+ JwtSession.setRefreshToken(data['refresh_token']);
86
+ }
79
87
 
80
88
  JwtSession.removeItemInStorage('real_users');
81
89
 
@@ -191,7 +199,9 @@ class JwtSession {
191
199
 
192
200
  // on enregistre la session de l'utilisateur simulé
193
201
  JwtSession.setToken(loginData['access_token'] || loginData['token']);
194
- JwtSession.setRefreshToken(loginData['refresh_token']);
202
+ if (loginData['refresh_token'] != null) {
203
+ JwtSession.setRefreshToken(loginData['refresh_token']);
204
+ }
195
205
 
196
206
  if (typeof onComplete == 'function') {
197
207
  onComplete();
@@ -216,7 +226,9 @@ class JwtSession {
216
226
  JwtSession.setItemInStorage('real_users', JSON.stringify(realUsers));
217
227
 
218
228
  JwtSession.setToken(loginData['access_token'] || loginData['token']);
219
- JwtSession.setRefreshToken(loginData['refresh_token']);
229
+ if (loginData['refresh_token'] != null) {
230
+ JwtSession.setRefreshToken(loginData['refresh_token']);
231
+ }
220
232
 
221
233
  if (typeof onComplete == 'function') {
222
234
  onComplete();
package/location.js CHANGED
@@ -1,7 +1,11 @@
1
1
 
2
2
  class Country {
3
+ static setFlagsPath(flagsPath) {
4
+ Country.flagsPath = flagsPath;
5
+ }
6
+
3
7
  static getFlagPath(countryCode) {
4
- return flagsPath+countryCode.toLowerCase()+'.png';
8
+ return (typeof Country.flagsPath != 'undefined' ? Country.flagsPath : '/')+countryCode.toLowerCase()+'.png';
5
9
  }
6
10
  static getFlagImg(countryCode) {
7
11
  return '<span><img src="'+Country.getFlagPath(countryCode)+'" alt="" title="'+Country.getCountryName(countryCode)+'" class="flag" /></span>'
@@ -613,7 +617,7 @@ class GeographicCoordinates {
613
617
  return null;
614
618
  }
615
619
 
616
- return asString ? str.join(',') : [lat, long];
620
+ return asString ? lat+','+long : [lat, long];
617
621
  }
618
622
 
619
623
  static toFixed(latOrLong, fractionDigit=6) {
package/media.js CHANGED
@@ -137,7 +137,7 @@ class VideoMedia {
137
137
  //Source : https://www.npmjs.com/package/mic-check
138
138
  class UserMedia {
139
139
  static hasGetUserMedia() {
140
- return !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
140
+ return !!(typeof navigator !== 'undefined' && (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia));
141
141
  }
142
142
 
143
143
  /** SystemPermissionDenied => (macOS) browser does not have permission to access cam/mic */
package/number.js CHANGED
@@ -78,11 +78,10 @@ Number.padLeft2 = Number.padLeft2 || function(n) {
78
78
  return n > 9 ? "" + n : "0" + n;
79
79
  }
80
80
 
81
- Number.prototype.roundDecimal = Number.prototype.roundDecimal || function(precision) {
81
+ Number.prototype.roundDecimal = Number.prototype.roundDecimal || function(precision=2) {
82
82
  return Number.roundDecimal(this, precision);
83
83
  }
84
- Number.roundDecimal = Number.roundDecimal || function(number, precision) {
85
- precision = precision || 2;
84
+ Number.roundDecimal = Number.roundDecimal || function(number, precision=2) {
86
85
  let tmp = Math.pow(10, precision);
87
86
  return Math.round(number*tmp) / tmp;
88
87
  }
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.4.23",
3
+ "version": "1.4.25",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
- "test": "echo \"Error: no test specified\" && exit 1"
6
+ "test": "jest",
7
+ "test:watch": "jest --watch",
8
+ "test:coverage": "jest --coverage",
9
+ "test:verbose": "jest --verbose"
7
10
  },
8
11
  "keywords": [],
9
12
  "author": "",
@@ -11,5 +14,28 @@
11
14
  "description": "",
12
15
  "dependencies": {
13
16
  "ilib": "^14.16.0"
17
+ },
18
+ "devDependencies": {
19
+ "jest": "^30.2.0",
20
+ "whatwg-fetch": "^3.6.20"
21
+ },
22
+ "jest": {
23
+ "testEnvironment": "node",
24
+ "testMatch": [
25
+ "**/tests/**/*.test.js"
26
+ ],
27
+ "collectCoverageFrom": [
28
+ "*.js",
29
+ "!main.js",
30
+ "!index.js",
31
+ "!node_modules/**",
32
+ "!tests/**"
33
+ ],
34
+ "coverageDirectory": "coverage",
35
+ "coverageReporters": [
36
+ "text",
37
+ "lcov",
38
+ "html"
39
+ ]
14
40
  }
15
41
  }
package/social_network.js CHANGED
@@ -1,3 +1,5 @@
1
+ require('./string');
2
+
1
3
  class SocialNetwork {
2
4
 
3
5
  // ---------- Facebook ----------
@@ -40,6 +42,9 @@ class SocialNetwork {
40
42
  /*
41
43
  <a target="_blank" title="Twitter" href="https://twitter.com/share?url=<?php the_permalink(); ?>&text=<?php the_title(); ?>&via=korben" rel="nofollow" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=400,width=700');return false;"><img src="http://korben.info/wp-content/themes/korben2013/hab/twitter_icon.png" alt="Twitter" /></a>
42
44
  */
45
+ if (typeof text === 'string' && typeof text.escapeHtml === 'function') {
46
+ text = text.escapeHtml();
47
+ }
43
48
  return ''
44
49
  +'<a href="https://twitter.com/share" class="twitter-share-button" data-url="'+url+'" data-text="'+text+'" data-lang="'+lang+'" data-hashtags="'+hashtags+'">Tweeter</a>'
45
50
  +'<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>'
package/string.js CHANGED
@@ -31,7 +31,7 @@ String.prototype.reverseString = String.prototype.reverseString || function() {
31
31
 
32
32
  String.prototype.truncateOnWord = String.prototype.truncateOnWord || function(limit, fromLeft) {
33
33
  if (fromLeft) {
34
- return this.reverseString(this.truncateOnWord(this.reverseString(), limit));
34
+ return this.reverseString().truncateOnWord(limit, false).reverseString();
35
35
  }
36
36
  let TRIM_CHARS = '\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u2028\u2029\u3000\uFEFF';
37
37
  let words = this.split(RegExp('(?=['+TRIM_CHARS+'])'));
@@ -62,15 +62,24 @@ String.prototype.truncateString = String.prototype.truncateString || function(le
62
62
  switch(from) {
63
63
  case 'left':
64
64
  str2 = split ? this.truncateOnWord(length, true) : this.slice(this.length - length);
65
+ if (split) {
66
+ str2 = str2.trimStart();
67
+ return ellipsis + ' ' + str2;
68
+ }
65
69
  return ellipsis + str2;
66
70
  case 'middle':
67
71
  len1 = Math.ceil(length / 2);
68
72
  len2 = Math.floor(length / 2);
69
73
  str1 = split ? this.truncateOnWord(len1) : this.slice(0, len1);
70
74
  str2 = split ? this.truncateOnWord(len2, true) : this.slice(this.length - len2);
71
- return str1 + ellipsis + str2;
75
+ // Enlever les espaces de fin de str1 et de début de str2 pour éviter les doubles espaces
76
+ return str1.trimEnd() + ' ' + ellipsis + ' ' + str2.trimStart();
72
77
  default:
73
78
  str1 = split ? this.truncateOnWord(length) : this.slice(0, length);
79
+ if (split) {
80
+ str1 = str1.trimEnd();
81
+ return str1 + ' ' + ellipsis;
82
+ }
74
83
  return str1 + ellipsis;
75
84
  }
76
85
  };