aotrautils 0.0.809 → 0.0.811

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 (15/02/2024-00:44:12)»*/
3
+ /*utils COMMONS library associated with aotra version : «1_29072022-2359 (21/02/2024-23:22:30)»*/
4
4
  /*-----------------------------------------------------------------------------*/
5
5
 
6
6
 
@@ -4086,6 +4086,122 @@ window.areEquivalentFlatMaps=function(o1,o2,UUID_ATTR_NAME=DEFAULT_UUID_ATTR_NAM
4086
4086
  return areEquivalent;
4087
4087
  }
4088
4088
 
4089
+
4090
+ window.overlaps=(delimitedObj1,delimitedObj2)=>{
4091
+ return (delimitedObj2.start<=delimitedObj1.start && delimitedObj1.start<=delimitedObj2.end)
4092
+ || (delimitedObj2.start<=delimitedObj1.end && delimitedObj1.end <=delimitedObj2.end);
4093
+ }
4094
+
4095
+
4096
+ window.findCommonChunks=(str1,str2,minimalLength=2,invertLogic=false,characterFilterFunction=null)=>{
4097
+ const results=[];
4098
+
4099
+ if(empty(str1)) return results;
4100
+ if(empty(str2)) return results;
4101
+
4102
+
4103
+ // 1) We build a binary correspondances matrix :
4104
+
4105
+ const correspondancesMatrix=[];
4106
+
4107
+ for(let i=0;i<str1.length;i++){
4108
+
4109
+ const currentCharAtRow=str1.charAt(i);
4110
+
4111
+ const row=[];
4112
+ for(let j=0;j<str2.length;j++){
4113
+
4114
+ const currentCharAtColumn=str2.charAt(j);
4115
+
4116
+ if( currentCharAtColumn==currentCharAtRow
4117
+ && (!characterFilterFunction || characterFilterFunction(currentCharAtColumn)))
4118
+ row.push(true && !invertLogic);
4119
+ else
4120
+ row.push(false || invertLogic);
4121
+
4122
+ }
4123
+ correspondancesMatrix.push(row);
4124
+ }
4125
+
4126
+ // 2) We try to walk through the diagonal of each found correspondance to create each chunk, and removing those already walked along the way :
4127
+ // This way we build the raw chunks dictionnary :
4128
+ for(let i=0;i<correspondancesMatrix.length;i++){
4129
+
4130
+ for(let j=0;j<correspondancesMatrix[i].length;j++){
4131
+
4132
+ if(!correspondancesMatrix[i][j]) continue;
4133
+
4134
+ let newChunk=null;
4135
+
4136
+ // Proximity algorithm : Here it's just a simple diagonal walking
4137
+ // (for blobs in a 2D image detection, instead of hunks in a string detection, it would be a recursive algorithm !)
4138
+ let k;
4139
+ for(k=0;(i+k)<correspondancesMatrix.length && (j+k)<correspondancesMatrix[i].length;k++){
4140
+
4141
+ if(!correspondancesMatrix[i+k][j+k]) break;
4142
+
4143
+ if(k==0){
4144
+ newChunk=str1[i+k];
4145
+ }else{
4146
+ newChunk+=str1[i+k];
4147
+ }
4148
+ correspondancesMatrix[i+k][j+k]=false;
4149
+ }
4150
+ if(newChunk && minimalLength<=newChunk.length){
4151
+ results.push(
4152
+ [
4153
+ {start:i+k-newChunk.length,end:i+k-1,value:newChunk,length:newChunk.length},
4154
+ {start:j+k-newChunk.length,end:j+k-1,value:newChunk,length:newChunk.length}
4155
+ ]
4156
+ );
4157
+ }
4158
+
4159
+ }
4160
+ }
4161
+
4162
+
4163
+ // 3) We filter all the overlapping chunks definitions, meaning when chunks overlap, only the biggest is kept !
4164
+
4165
+ const refinedResults=[];
4166
+ for(let i=0;i<results.length;i++){
4167
+ let chunk1=results[i];
4168
+
4169
+ for(let j=0;j<results.length;j++){
4170
+ let chunk2=results[j];
4171
+
4172
+ if(overlaps(chunk1[0],chunk2[0])) continue;
4173
+
4174
+ let chosenChunk=chunk1;
4175
+ if(chunk1[0].length<chunk2[0].length) chosenChunk=chunk2;
4176
+
4177
+ refinedResults.push(chosenChunk);
4178
+
4179
+ }
4180
+ }
4181
+
4182
+ // (we need to do the adding in both directions :)
4183
+ for(let i=0;i<results.length;i++){
4184
+ let chunk1=results[i];
4185
+
4186
+ for(let j=0;j<results.length;j++){
4187
+ let chunk2=results[j];
4188
+
4189
+ if(overlaps(chunk1[1],chunk2[1])) continue;
4190
+
4191
+ let chosenChunk=chunk1;
4192
+ if(chunk1[1].length<chunk2[1].length) chosenChunk=chunk2;
4193
+
4194
+ refinedResults.push(chosenChunk);
4195
+
4196
+ }
4197
+ }
4198
+
4199
+
4200
+ return refinedResults;
4201
+ }
4202
+
4203
+
4204
+
4089
4205
  window.areEquivalentSimple=function(obj1Param,obj2Param, caseSensitive=true){
4090
4206
  const str1=(isString(obj1Param)?obj1Param:stringifyObject(obj1Param));
4091
4207
  const str2=(isString(obj2Param)?obj2Param:stringifyObject(obj2Param));
@@ -5167,7 +5283,7 @@ AOTRAUTILS_LIB_IS_LOADED=true;
5167
5283
 
5168
5284
 
5169
5285
 
5170
- /*utils CLIENT library associated with aotra version : «1_29072022-2359 (15/02/2024-00:44:12)»*/
5286
+ /*utils CLIENT library associated with aotra version : «1_29072022-2359 (21/02/2024-23:22:30)»*/
5171
5287
  /*-----------------------------------------------------------------------------*/
5172
5288
  /* ## Utility global methods in a browser (htmljs) client environment.
5173
5289
  *
@@ -13380,7 +13496,7 @@ createOritaMicroClient=function(url, port, isNode=false, staticMicroClientIdPara
13380
13496
 
13381
13497
 
13382
13498
 
13383
- /*utils GEOMETRY library associated with aotra version : «1_29072022-2359 (15/02/2024-00:44:12)»*/
13499
+ /*utils GEOMETRY library associated with aotra version : «1_29072022-2359 (21/02/2024-23:22:30)»*/
13384
13500
  /*-----------------------------------------------------------------------------*/
13385
13501
 
13386
13502
 
@@ -14619,7 +14735,7 @@ function rayVsUnitSphereClosestPoint(p, r) {
14619
14735
  // MUST REMAIN AT THE END OF THIS LIBRARY FILE !
14620
14736
 
14621
14737
  AOTRAUTILS_GEOMETRY_LIB_IS_LOADED=true;
14622
- /*utils SERVER library associated with aotra version : «1_29072022-2359 (15/02/2024-00:44:12)»*/
14738
+ /*utils SERVER library associated with aotra version : «1_29072022-2359 (21/02/2024-23:22:30)»*/
14623
14739
  /*-----------------------------------------------------------------------------*/
14624
14740
 
14625
14741
 
@@ -15766,7 +15882,7 @@ WebsocketImplementation={
15766
15882
 
15767
15883
 
15768
15884
 
15769
- launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, /*OPTIONAL*/sslOptions=null, httpHandlerParam=null){
15885
+ launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, /*OPTIONAL*/sslOptions=null, httpHandlerParam=null, addCORSHeader=ADD_CORS_HEADER){
15770
15886
 
15771
15887
  const EXCLUDED_FILENAMES_PARTS=[".keyHash."];
15772
15888
 
@@ -15817,7 +15933,7 @@ launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, /
15817
15933
  const headers={ "Content-Type": contentType };
15818
15934
 
15819
15935
  // To remove the CORS error message (cf. https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9)
15820
- if(ADD_CORS_HEADER) headers["Access-Control-Allow-Origin"]="*";
15936
+ if(addCORSHeader) headers["Access-Control-Allow-Origin"]="*";
15821
15937
 
15822
15938
 
15823
15939
  fs.readFile(filePath, function(error, fileContent){
@@ -16288,18 +16404,18 @@ class ListManager{
16288
16404
  return getArraySize(this.itemsInfos);
16289
16405
  }
16290
16406
 
16291
- }
16407
+ };
16292
16408
 
16293
16409
  getListManager=function(config){
16294
16410
  return new ListManager(config);
16295
- }
16411
+ };
16412
+
16296
16413
 
16297
16414
 
16298
16415
  // NO : IN A NODE CONTEXT WITH requre("") WILL RESULT IN AN UNDEFINED FUNCTION ERROR !!!
16299
- // function performHTTPRequestNode(url,httpMethod="GET",headers={},postRequestBodyOrNamedArgs=null){
16416
+ // function performHTTPRequestNode(...){...
16300
16417
  // USE THIS INSTEAD :
16301
- performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequestBodyOrNamedArgs=null){
16302
-
16418
+ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},requestBodyOrNamedArgs=null,addCORSHeader=ADD_CORS_HEADER){
16303
16419
 
16304
16420
  if(!require){
16305
16421
  // TODO : FIXME : Support also a browser context!!
@@ -16307,7 +16423,7 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
16307
16423
  throw new Error("Unsupported context non-nodejs for performHTTPRequestNode().");
16308
16424
  }
16309
16425
 
16310
- const isSecure=completeURL.includes("https://");
16426
+ const isSecure=(!empty(completeURL) && contains(completeURL.toLowerCase(),"https://"));
16311
16427
  const httpHandler = isSecure?require("https"):require("http");
16312
16428
 
16313
16429
  // Options for the HTTP request
@@ -16320,9 +16436,19 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
16320
16436
  options.json=true;
16321
16437
  headers["Content-Type"]="application/json";
16322
16438
  // To remove the CORS error message (cf. https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9)
16323
- if(ADD_CORS_HEADER) headers["Access-Control-Allow-Origin"]="*";
16439
+ if(addCORSHeader) headers["Access-Control-Allow-Origin"]="*";
16440
+ } else if(httpMethod==="GET" && requestBodyOrNamedArgs){
16441
+ // Not the same way to send parameters in GET http method :
16442
+ // DBG
16443
+ lognow("unformatted API URL : "+completeURL);
16444
+
16445
+ completeURL=appendGetParameters(completeURL, requestBodyOrNamedArgs);
16446
+
16447
+ // DBG
16448
+ lognow("formatted API URL : "+completeURL);
16324
16449
  }
16325
16450
 
16451
+
16326
16452
  options.headers=headers;
16327
16453
 
16328
16454
  return new Promise((resolve,reject)=>{
@@ -16341,8 +16467,17 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
16341
16467
 
16342
16468
  // The whole response has been received.
16343
16469
  response.on("end", () => {
16344
- const responseData=parseJSON(responseDataStr);
16345
- resolve(responseData);
16470
+
16471
+ try{
16472
+ const responseData=parseJSON(responseDataStr);
16473
+ resolve(responseData, response, responseDataStr);
16474
+ }catch(error){
16475
+ // DBG
16476
+ lognow("WARN : Could not JSON parse the response data ! Must deal with the string:«"+responseDataStr+"»", error);
16477
+ resolve(responseDataStr, response, responseDataStr);
16478
+ return;
16479
+ }
16480
+
16346
16481
  });
16347
16482
  });
16348
16483
 
@@ -16355,8 +16490,8 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
16355
16490
  // Not the same way to send parameters in POST http method :
16356
16491
  if(contains(["POST","PUT"],httpMethod)){
16357
16492
  // (We need to stringify parameters or else we'll have an error :)
16358
- if(!empty(postRequestBodyOrNamedArgs)){
16359
- request.write(stringifyObject(postRequestBodyOrNamedArgs));
16493
+ if(!empty(requestBodyOrNamedArgs)){
16494
+ request.write(stringifyObject(requestBodyOrNamedArgs));
16360
16495
  }
16361
16496
  }
16362
16497
 
@@ -16365,10 +16500,30 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
16365
16500
 
16366
16501
  });
16367
16502
 
16368
- }
16503
+ };
16504
+
16369
16505
 
16370
16506
 
16371
16507
 
16508
+ replacePathVariablesNamesWithValuesIfPossible=function(apiURL, namedArgs){
16509
+ let result=apiURL;
16510
+ foreach(namedArgs,(namedArgValue, namedArgKey)=>{
16511
+ result=result.replace("{"+namedArgKey+"}",namedArgValue);
16512
+ });
16513
+ return result;
16514
+ };
16515
+
16516
+ appendGetParameters=function(apiURL, namedArgs){
16517
+ let result=apiURL;
16518
+
16519
+ const paramCouples=[];
16520
+ foreach(namedArgs,(value,key)=>{paramCouples.push(key+"="+value);});
16521
+
16522
+ if(!empty(paramCouples)) result+=("?"+paramCouples.join("&"));
16523
+
16524
+ return result;
16525
+ };
16526
+
16372
16527
 
16373
16528
  /* INCLUDED EXTERNAL LIBRAIRIES
16374
16529
  */
aotrautils/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aotrautils",
3
- "version": "0.0.809",
3
+ "version": "0.0.811",
4
4
  "main": "aotrautils.build.js",
5
5
  "description": "A library for vanilla javascript utils (client-side) used in aotra javascript CMS",
6
6
  "author": "Jeremie Ratomposon <info@alqemia.com> (https://alqemia.com)",