aotrautils-srv 0.0.801 → 0.0.803

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:20)»*/
3
+ /*utils COMMONS library associated with aotra version : «1_29072022-2359 (21/02/2024-23:22:39)»*/
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 SERVER library associated with aotra version : «1_29072022-2359 (15/02/2024-00:44:20)»*/
5286
+ /*utils SERVER library associated with aotra version : «1_29072022-2359 (21/02/2024-23:22:39)»*/
5171
5287
  /*-----------------------------------------------------------------------------*/
5172
5288
 
5173
5289
 
@@ -6314,7 +6430,7 @@ WebsocketImplementation={
6314
6430
 
6315
6431
 
6316
6432
 
6317
- launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, /*OPTIONAL*/sslOptions=null, httpHandlerParam=null){
6433
+ launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, /*OPTIONAL*/sslOptions=null, httpHandlerParam=null, addCORSHeader=ADD_CORS_HEADER){
6318
6434
 
6319
6435
  const EXCLUDED_FILENAMES_PARTS=[".keyHash."];
6320
6436
 
@@ -6365,7 +6481,7 @@ launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, /
6365
6481
  const headers={ "Content-Type": contentType };
6366
6482
 
6367
6483
  // 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)
6368
- if(ADD_CORS_HEADER) headers["Access-Control-Allow-Origin"]="*";
6484
+ if(addCORSHeader) headers["Access-Control-Allow-Origin"]="*";
6369
6485
 
6370
6486
 
6371
6487
  fs.readFile(filePath, function(error, fileContent){
@@ -6836,18 +6952,18 @@ class ListManager{
6836
6952
  return getArraySize(this.itemsInfos);
6837
6953
  }
6838
6954
 
6839
- }
6955
+ };
6840
6956
 
6841
6957
  getListManager=function(config){
6842
6958
  return new ListManager(config);
6843
- }
6959
+ };
6960
+
6844
6961
 
6845
6962
 
6846
6963
  // NO : IN A NODE CONTEXT WITH requre("") WILL RESULT IN AN UNDEFINED FUNCTION ERROR !!!
6847
- // function performHTTPRequestNode(url,httpMethod="GET",headers={},postRequestBodyOrNamedArgs=null){
6964
+ // function performHTTPRequestNode(...){...
6848
6965
  // USE THIS INSTEAD :
6849
- performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequestBodyOrNamedArgs=null){
6850
-
6966
+ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},requestBodyOrNamedArgs=null,addCORSHeader=ADD_CORS_HEADER){
6851
6967
 
6852
6968
  if(!require){
6853
6969
  // TODO : FIXME : Support also a browser context!!
@@ -6855,7 +6971,7 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
6855
6971
  throw new Error("Unsupported context non-nodejs for performHTTPRequestNode().");
6856
6972
  }
6857
6973
 
6858
- const isSecure=completeURL.includes("https://");
6974
+ const isSecure=(!empty(completeURL) && contains(completeURL.toLowerCase(),"https://"));
6859
6975
  const httpHandler = isSecure?require("https"):require("http");
6860
6976
 
6861
6977
  // Options for the HTTP request
@@ -6868,9 +6984,19 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
6868
6984
  options.json=true;
6869
6985
  headers["Content-Type"]="application/json";
6870
6986
  // 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)
6871
- if(ADD_CORS_HEADER) headers["Access-Control-Allow-Origin"]="*";
6987
+ if(addCORSHeader) headers["Access-Control-Allow-Origin"]="*";
6988
+ } else if(httpMethod==="GET" && requestBodyOrNamedArgs){
6989
+ // Not the same way to send parameters in GET http method :
6990
+ // DBG
6991
+ lognow("unformatted API URL : "+completeURL);
6992
+
6993
+ completeURL=appendGetParameters(completeURL, requestBodyOrNamedArgs);
6994
+
6995
+ // DBG
6996
+ lognow("formatted API URL : "+completeURL);
6872
6997
  }
6873
6998
 
6999
+
6874
7000
  options.headers=headers;
6875
7001
 
6876
7002
  return new Promise((resolve,reject)=>{
@@ -6889,8 +7015,17 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
6889
7015
 
6890
7016
  // The whole response has been received.
6891
7017
  response.on("end", () => {
6892
- const responseData=parseJSON(responseDataStr);
6893
- resolve(responseData);
7018
+
7019
+ try{
7020
+ const responseData=parseJSON(responseDataStr);
7021
+ resolve(responseData, response, responseDataStr);
7022
+ }catch(error){
7023
+ // DBG
7024
+ lognow("WARN : Could not JSON parse the response data ! Must deal with the string:«"+responseDataStr+"»", error);
7025
+ resolve(responseDataStr, response, responseDataStr);
7026
+ return;
7027
+ }
7028
+
6894
7029
  });
6895
7030
  });
6896
7031
 
@@ -6903,8 +7038,8 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
6903
7038
  // Not the same way to send parameters in POST http method :
6904
7039
  if(contains(["POST","PUT"],httpMethod)){
6905
7040
  // (We need to stringify parameters or else we'll have an error :)
6906
- if(!empty(postRequestBodyOrNamedArgs)){
6907
- request.write(stringifyObject(postRequestBodyOrNamedArgs));
7041
+ if(!empty(requestBodyOrNamedArgs)){
7042
+ request.write(stringifyObject(requestBodyOrNamedArgs));
6908
7043
  }
6909
7044
  }
6910
7045
 
@@ -6913,10 +7048,30 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
6913
7048
 
6914
7049
  });
6915
7050
 
6916
- }
7051
+ };
7052
+
6917
7053
 
6918
7054
 
6919
7055
 
7056
+ replacePathVariablesNamesWithValuesIfPossible=function(apiURL, namedArgs){
7057
+ let result=apiURL;
7058
+ foreach(namedArgs,(namedArgValue, namedArgKey)=>{
7059
+ result=result.replace("{"+namedArgKey+"}",namedArgValue);
7060
+ });
7061
+ return result;
7062
+ };
7063
+
7064
+ appendGetParameters=function(apiURL, namedArgs){
7065
+ let result=apiURL;
7066
+
7067
+ const paramCouples=[];
7068
+ foreach(namedArgs,(value,key)=>{paramCouples.push(key+"="+value);});
7069
+
7070
+ if(!empty(paramCouples)) result+=("?"+paramCouples.join("&"));
7071
+
7072
+ return result;
7073
+ };
7074
+
6920
7075
 
6921
7076
  /* INCLUDED EXTERNAL LIBRAIRIES
6922
7077
  */
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aotrautils-srv",
3
- "version": "0.0.801",
3
+ "version": "0.0.803",
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)",