alessandro trinca tornidor commited on
Commit
f2474aa
·
1 Parent(s): f4cd2d5

feat: now the filter works only on the values of the json output array

Browse files
Files changed (1) hide show
  1. static/index.js +88 -3
static/index.js CHANGED
@@ -250,6 +250,41 @@ const porterStemmer = (function(){
250
  }
251
  })();
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  /**
254
  * Filters elements from an array based on specified conditions.
255
  * @param {Array} inputArray - The array of elements to filter.
@@ -554,16 +589,63 @@ function dynamicSort(property, order) {
554
  }
555
 
556
  /**
557
- * Filters an array of objects by checking if a specified nested value exists within any object.
558
- * The search is case-insensitive and checks all nested properties by converting the object to a JSON string.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
559
  *
560
  * @param {Array<Object>} array - The array of objects to filter.
561
  * @param {string} nestedValue - The value to search for within the objects.
562
  * @returns {Array<Object>} - A new array containing objects where the nested value is found.
 
 
 
 
 
 
 
 
563
  */
564
  function arrayFilterNestedValue(array, nestedValue) {
 
565
  return array.filter(item => {
566
- return JSON.stringify(item).toLowerCase().includes(nestedValue.toLowerCase());
 
 
567
  });
568
  }
569
 
@@ -591,7 +673,9 @@ async function updateWordsFrequencyTables() {
591
 
592
  let inputFilter = document.getElementById("filter-words-frequency")
593
  let inputFilterValue = inputFilter.value
 
594
  if (inputFilterValue !== undefined && inputFilter.value !== "") {
 
595
  reduced = arrayFilterNestedValue(reduced, inputFilterValue)
596
  }
597
 
@@ -720,6 +804,7 @@ function insertCellIntoTRow(currentTBody, i, ii, nthOffset, nTotalRows) {
720
  * @function updateWordsFreqIfPressEnter
721
  */
722
  function updateWordsFreqIfPressEnter() {
 
723
  if(event.key==='Enter'){
724
  updateWordsFrequencyTables()
725
  }
 
250
  }
251
  })();
252
 
253
+ /**
254
+ * Recursively extracts all string values from any level of a nested object or array.
255
+ *
256
+ * This function traverses the input (which can be an object, array, or primitive)
257
+ * and collects every string value it finds, regardless of the key or structure.
258
+ * The result is a flat array of all string values found within the input.
259
+ *
260
+ * @param {*} obj - The input object, array, or value to search for strings.
261
+ * @returns {Array<string>} An array containing all string values found in the input.
262
+ *
263
+ * @example
264
+ * const data = [
265
+ * { word: "hello", nested: { value: "world" } },
266
+ * { words: ["foo", "bar"] },
267
+ * "baz"
268
+ * ];
269
+ * const result = extractStringValues(data);
270
+ * // result: ["hello", "world", "foo", "bar", "baz"]
271
+ */
272
+ function extractStringValues(obj) {
273
+ let result = [];
274
+ if (typeof obj === "string") {
275
+ result.push(obj);
276
+ } else if (Array.isArray(obj)) {
277
+ for (const item of obj) {
278
+ result = result.concat(extractStringValues(item));
279
+ }
280
+ } else if (typeof obj === "object" && obj !== null) {
281
+ for (const key in obj) {
282
+ result = result.concat(extractStringValues(obj[key]));
283
+ }
284
+ }
285
+ return result;
286
+ }
287
+
288
  /**
289
  * Filters elements from an array based on specified conditions.
290
  * @param {Array} inputArray - The array of elements to filter.
 
589
  }
590
 
591
  /**
592
+ * Recursively extracts all string values from any level of a nested object or array.
593
+ *
594
+ * This function traverses the input (which can be an object, array, or primitive)
595
+ * and collects every string value it finds, regardless of the key or structure.
596
+ * The result is a flat array of all string values found within the input.
597
+ *
598
+ * @param {*} obj - The input object, array, or value to search for strings.
599
+ * @returns {Array<string>} An array containing all string values found in the input.
600
+ *
601
+ * @example
602
+ * const data = [
603
+ * { word: "hello", nested: { value: "world" } },
604
+ * { words: ["foo", "bar"] },
605
+ * "baz"
606
+ * ];
607
+ * const result = extractStringValues(data);
608
+ * // result: ["hello", "world", "foo", "bar", "baz"]
609
+ */
610
+ function extractStringValues(obj) {
611
+ let result = [];
612
+ if (typeof obj === "string") {
613
+ result.push(obj);
614
+ } else if (Array.isArray(obj)) {
615
+ for (const item of obj) {
616
+ result = result.concat(extractStringValues(item));
617
+ }
618
+ } else if (typeof obj === "object" && obj !== null) {
619
+ for (const key in obj) {
620
+ result = result.concat(extractStringValues(obj[key]));
621
+ }
622
+ }
623
+ return result;
624
+ }
625
+
626
+ /**
627
+ * Filters an array of objects by checking if a specified value exists within any nested property.
628
+ * The search is case-insensitive and recursively collects all string values from each object,
629
+ * then checks if the concatenated string contains the search value.
630
  *
631
  * @param {Array<Object>} array - The array of objects to filter.
632
  * @param {string} nestedValue - The value to search for within the objects.
633
  * @returns {Array<Object>} - A new array containing objects where the nested value is found.
634
+ *
635
+ * @example
636
+ * const arr = [
637
+ * { a: "hello", b: { c: "world" } },
638
+ * { a: "foo", b: { c: "bar" } }
639
+ * ];
640
+ * arrayFilterNestedValue(arr, "World");
641
+ * // Returns: [{ a: "hello", b: { c: "world" } }]
642
  */
643
  function arrayFilterNestedValue(array, nestedValue) {
644
+ console.log("arrayFilterNestedValue...", array.length, "#")
645
  return array.filter(item => {
646
+ let valuesFromObject = extractStringValues(item).join(" ")
647
+ console.log("arrayFilterNestedValue::contains:", valuesFromObject.toLowerCase().includes(nestedValue.toLowerCase()), valuesFromObject, "#")
648
+ return valuesFromObject.toLowerCase().includes(nestedValue.toLowerCase());
649
  });
650
  }
651
 
 
673
 
674
  let inputFilter = document.getElementById("filter-words-frequency")
675
  let inputFilterValue = inputFilter.value
676
+ console.log("updateWordsFrequencyTables::inputFilter.value:", inputFilter.value, inputFilterValue !== undefined && inputFilter.value !== "", "#")
677
  if (inputFilterValue !== undefined && inputFilter.value !== "") {
678
+ console.log("dentro...")
679
  reduced = arrayFilterNestedValue(reduced, inputFilterValue)
680
  }
681
 
 
804
  * @function updateWordsFreqIfPressEnter
805
  */
806
  function updateWordsFreqIfPressEnter() {
807
+ console.log("updateWordsFreqIfPressEnter::", event.key, "#")
808
  if(event.key==='Enter'){
809
  updateWordsFrequencyTables()
810
  }