aotrautils-srv 0.0.985 → 0.0.987

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.
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- /*utils COMMONS library associated with aotra version : «1_29072022-2359 (21/05/2024-03:09:26)»*/
3
+ /*utils COMMONS library associated with aotra version : «1_29072022-2359 (12/06/2024-01:38:53)»*/
4
4
  /*-----------------------------------------------------------------------------*/
5
5
 
6
6
 
@@ -1905,8 +1905,9 @@ window.getOrCreateAttribute=function(parentObject, attributeNameOrAttributesName
1905
1905
  const attributeName=(isArray(attributeNameOrAttributesName) && attributeNameOrAttributesName.length===1)?
1906
1906
  attributeNameOrAttributesName[0]:attributeNameOrAttributesName;
1907
1907
  if(parentObject[attributeName]==null){
1908
- // parentObject[attributeName]=defaultValue;
1909
- // CAUTION : if we have defaultValue as parameter, sometimes because of the recursive call, it can refer to a persisting object ! Thus, creating a strange bug.
1908
+ // NO : parentObject[attributeName]=defaultValue;
1909
+ // BECAUSE CAUTION : if we have defaultValue as parameter, sometimes because of the recursive call, it can refer to a persisting object ! Thus, creating a strange bug.
1910
+ // (SO WE ALWAYS NEED TO START AS A BLANK STATE FOR DEFAULT VALUE OBJECTS !)
1910
1911
  const DEFAULT_VALUE={};
1911
1912
  parentObject[attributeName]=DEFAULT_VALUE;
1912
1913
  }
@@ -1917,8 +1918,9 @@ window.getOrCreateAttribute=function(parentObject, attributeNameOrAttributesName
1917
1918
 
1918
1919
  const attributeName=attributeNameOrAttributesName[0];
1919
1920
  if(parentObject[attributeName]==null){
1920
- // parentObject[attributeName]=defaultValue;
1921
- // CAUTION : if we have defaultValue as parameter, sometimes because of the recursive call, it can refer to a persisting object ! Thus, creating a strange bug.
1921
+ // NO : parentObject[attributeName]=defaultValue;
1922
+ // BECAUSE CAUTION : if we have defaultValue as parameter, sometimes because of the recursive call, it can refer to a persisting object ! Thus, creating a strange bug.
1923
+ // (SO WE ALWAYS NEED TO START AS A BLANK STATE FOR DEFAULT VALUE OBJECTS !)
1922
1924
  const DEFAULT_VALUE={};
1923
1925
  parentObject[attributeName]=DEFAULT_VALUE;
1924
1926
  }
@@ -2215,6 +2217,56 @@ window.compareDates=function(date1=null,date2=null){
2215
2217
  }
2216
2218
 
2217
2219
 
2220
+ window.getRandomDateFormattedString=function(daysSpan=365,format="ddMMYYYY") {
2221
+ const newDate=getRandomDate(daysSpan);
2222
+ const result=formatDate(newDate,format);
2223
+ return result;
2224
+ }
2225
+
2226
+ window.getRandomDate=function(spansRangeDays=365, spansRangeMonths=0, spansRangeYears=0, referenceDate=new Date()) {
2227
+ const start=new Date(referenceDate);
2228
+ const end=new Date(referenceDate);
2229
+
2230
+ if(spansRangeDays){
2231
+ const HALF_SPAN_DAYS=Math.round(spansRangeDays/2);
2232
+ start.setDate(referenceDate.getDate()-HALF_SPAN_DAYS);
2233
+ end.setDate(referenceDate.getDate()+HALF_SPAN_DAYS);
2234
+ }else if(spansRangeMonths){
2235
+ const HALF_SPAN_MONTHS=Math.round(spansRangMonthse/2);
2236
+ start.setMonths(referenceDate.getMonths()-HALF_SPAN_MONTHS);
2237
+ end.setMonths(referenceDate.getMonths()+HALF_SPAN_MONTHS);
2238
+ }if(spansRangeYears){
2239
+ const HALF_SPAN_YEARS=Math.round(spansRangeYears/2);
2240
+ start.setFullYear(referenceDate.getFullYear()-HALF_SPAN_YEARS);
2241
+ end.setFullYear(referenceDate.getFullYear()+HALF_SPAN_YEARS);
2242
+ }
2243
+
2244
+ return getRandomDateInPeriod(start, end);
2245
+ }
2246
+
2247
+ window.getRandomDateInPeriod=function(start, end) {
2248
+ // Generate a random date between start and end
2249
+ const deltaTime= end.getTime() - start.getTime();
2250
+ return new Date(start.getTime() + Math.random() * deltaTime);
2251
+ }
2252
+
2253
+ window.formatDate=function(date, format) {
2254
+ // Extract parts of the date
2255
+ const day = String(date.getDate()).padStart(2, "0");
2256
+ const month = String(date.getMonth() + 1).padStart(2, "0"); // Months are 0-based
2257
+ const year = date.getFullYear();
2258
+ const hours = String(date.getHours()).padStart(2, "0");
2259
+ const minutes = String(date.getMinutes()).padStart(2, "0");
2260
+ const seconds = String(date.getSeconds()).padStart(2, "0");
2261
+ // Replace placeholders in the format string
2262
+ return format.replace("dd", day)
2263
+ .replace("MM", month)
2264
+ .replace("yyyy", year)
2265
+ .replace("HH", hours)
2266
+ .replace("mm", minutes)
2267
+ .replace("ss", seconds);
2268
+ }
2269
+
2218
2270
 
2219
2271
  // ==================== Strings management ====================
2220
2272
 
@@ -3380,7 +3432,9 @@ Math.getRandom=function(ceil,/*OPTIONAL*/floor){// FYI : ceil>floor
3380
3432
  // (random integer values from floor to ceil)
3381
3433
  return (Math.random() * ((ceil-floor) + 1) ) + floor;
3382
3434
  };
3383
-
3435
+ Math.getRandomBool=function(){
3436
+ return (1<=(Math.random()*2));
3437
+ };
3384
3438
 
3385
3439
 
3386
3440
  Math.getRandomIntAroundValue=function(target, area,/*OPTIONAL*/exclusionParam){
@@ -5297,7 +5351,7 @@ AOTRAUTILS_LIB_IS_LOADED=true;
5297
5351
 
5298
5352
 
5299
5353
 
5300
- /*utils SERVER library associated with aotra version : «1_29072022-2359 (21/05/2024-03:09:26)»*/
5354
+ /*utils SERVER library associated with aotra version : «1_29072022-2359 (12/06/2024-01:38:53)»*/
5301
5355
  /*-----------------------------------------------------------------------------*/
5302
5356
 
5303
5357
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aotrautils-srv",
3
- "version": "0.0.985",
3
+ "version": "0.0.987",
4
4
  "main": "aotrautils-srv.build.js",
5
5
  "description": "A library for vanilla javascript utils (server-side) used in aotra javascript CMS",
6
6
  "author": "Jeremie Ratomposon <info@alqemia.com> (https://alqemia.com)",