devicer.js 1.0.13 → 1.1.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "devicer.js",
3
- "version": "1.0.13",
3
+ "version": "1.1.0",
4
4
  "description": "Open-Source Digital Fingerprinting Middleware",
5
5
  "main": "src/main.js",
6
6
  "scripts": {
@@ -7,7 +7,8 @@ function compareArrays(arr1, arr2, max_depth = 5) {
7
7
  let matches = 0;
8
8
  // Ensure max_depth is not exceeded
9
9
  if (max_depth <= 0) {
10
- throw new Error("Max depth exceeded");
10
+ console.warn("Max depth exceeded in compareArrays");
11
+ return [0, 0]; // Return 0 fields and matches if max depth is exceeded
11
12
  }
12
13
  // Sort arrays to ensure consistent comparison
13
14
  const sortedArr1 = arr1.map((item) => JSON.stringify(item)).sort().map((item) => {
@@ -26,7 +27,6 @@ function compareArrays(arr1, arr2, max_depth = 5) {
26
27
  return undefined;
27
28
  }
28
29
  });
29
- console.log("Sorted Arrays:", sortedArr1, sortedArr2);
30
30
  const maxLength = Math.min(arr1.length, arr2.length);
31
31
  for (let i = 0; i < maxLength; i++) {
32
32
  fields++;
@@ -41,8 +41,7 @@ function compareArrays(arr1, arr2, max_depth = 5) {
41
41
  fields += subData[0] - 1; // Subtract 1 for the index itself
42
42
  matches += subData[1];
43
43
  }
44
- if (sortedArr2.includes(sortedArr1[i])) {
45
- console.log("Match found:", sortedArr1[i]);
44
+ if (sortedArr2.includes(sortedArr1[i]) && sortedArr1[i]) {
46
45
  matches++;
47
46
  }
48
47
  }
@@ -54,19 +53,20 @@ function compareDatasets(data1, data2, max_depth = 5) {
54
53
  let matches = 0;
55
54
  // Ensure max_depth is not exceeded
56
55
  if (max_depth <= 0) {
57
- throw new Error("Max depth exceeded");
56
+ console.warn("Max depth exceeded in compareDatasets");
57
+ return [0, 0]; // Return 0 fields and matches if max depth is exceeded
58
58
  }
59
59
  for (const key in data1) {
60
60
  if (data1[key] !== undefined && data2[key] !== undefined) {
61
61
  fields++;
62
- if ((typeof data1[key] == "object" && data1[key]) &&
63
- (typeof data2[key] == "object" && data2[key])) {
64
- const subData = compareDatasets(data1[key], data2[key], max_depth - 1);
62
+ if (Array.isArray(data1[key]) && Array.isArray(data2[key])) {
63
+ const subData = compareArrays(data1[key], data2[key], max_depth - 1);
65
64
  fields += subData[0] - 1; // Subtract 1 for the key itself
66
65
  matches += subData[1];
67
66
  }
68
- else if (Array.isArray(data1[key]) && Array.isArray(data2[key])) {
69
- const subData = compareArrays(data1[key], data2[key], max_depth - 1);
67
+ else if ((typeof data1[key] == "object" && data1[key]) &&
68
+ (typeof data2[key] == "object" && data2[key])) {
69
+ const subData = compareDatasets(data1[key], data2[key], max_depth - 1);
70
70
  fields += subData[0] - 1; // Subtract 1 for the key itself
71
71
  matches += subData[1];
72
72
  }
@@ -11,7 +11,8 @@ export function compareArrays(
11
11
 
12
12
  // Ensure max_depth is not exceeded
13
13
  if (max_depth <= 0) {
14
- throw new Error("Max depth exceeded");
14
+ console.warn("Max depth exceeded in compareArrays");
15
+ return [0, 0]; // Return 0 fields and matches if max depth is exceeded
15
16
  }
16
17
 
17
18
  // Sort arrays to ensure consistent comparison
@@ -45,9 +46,7 @@ export function compareArrays(
45
46
  );
46
47
  fields += subData[0] - 1; // Subtract 1 for the index itself
47
48
  matches += subData[1];
48
- }
49
-
50
- else if (
49
+ } else if (
51
50
  (typeof sortedArr1[i] == "object" && sortedArr1[i]) &&
52
51
  (typeof sortedArr2[i] == "object" && sortedArr2[i])
53
52
  ) {
@@ -60,7 +59,7 @@ export function compareArrays(
60
59
  matches += subData[1];
61
60
  }
62
61
 
63
- if (sortedArr2.includes(sortedArr1[i])) {
62
+ if (sortedArr2.includes(sortedArr1[i]) && sortedArr1[i]) {
64
63
  matches++;
65
64
  }
66
65
  }
@@ -77,13 +76,18 @@ export function compareDatasets(
77
76
 
78
77
  // Ensure max_depth is not exceeded
79
78
  if (max_depth <= 0) {
80
- throw new Error("Max depth exceeded");
79
+ console.warn("Max depth exceeded in compareDatasets");
80
+ return [0, 0]; // Return 0 fields and matches if max depth is exceeded
81
81
  }
82
82
 
83
83
  for (const key in data1) {
84
84
  if (data1[key] !== undefined && data2[key] !== undefined) {
85
85
  fields++;
86
- if (
86
+ if (Array.isArray(data1[key]) && Array.isArray(data2[key])) {
87
+ const subData = compareArrays(data1[key], data2[key], max_depth - 1);
88
+ fields += subData[0] - 1; // Subtract 1 for the key itself
89
+ matches += subData[1];
90
+ } else if (
87
91
  (typeof data1[key] == "object" && data1[key]) &&
88
92
  (typeof data2[key] == "object" && data2[key])
89
93
  ) {
@@ -94,15 +98,7 @@ export function compareDatasets(
94
98
  );
95
99
  fields += subData[0] - 1; // Subtract 1 for the key itself
96
100
  matches += subData[1];
97
- }
98
-
99
- else if (Array.isArray(data1[key]) && Array.isArray(data2[key])) {
100
- const subData = compareArrays(data1[key], data2[key], max_depth - 1);
101
- fields += subData[0] - 1; // Subtract 1 for the key itself
102
- matches += subData[1];
103
- }
104
-
105
- else if (data1[key] == data2[key]) {
101
+ } else if (data1[key] == data2[key]) {
106
102
  matches++;
107
103
  }
108
104
  }
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from "vitest";
2
- import { compareArrays, compareDatasets } from "../src/libs/confidence";
2
+ import { compareArrays } from "../src/libs/confidence";
3
3
 
4
4
  describe("Array Comparison", () => {
5
5
  it("should compare two identical arrays", () => {
@@ -75,9 +75,11 @@ describe("Array Comparison", () => {
75
75
  });
76
76
 
77
77
  it("should throw an error for max depth exceeded", () => {
78
- const arr1 = [1, [2, [3, [4, [5, [6]]]]]];
79
- const arr2 = [1, [2, [3, [4, [5, [6]]]]]];
80
- expect(() => compareArrays(arr1, arr2, 0)).toThrow("Max depth exceeded");
78
+ const arr1 = [1, 2, 3, [4, 5, 6]];
79
+ const arr2 = [1, 2, 3, [4, 5, 6]];
80
+ const result = compareArrays(arr1, arr2, 1); // Set max depth to 1
81
+ console.log("Result:", result);
82
+ expect(result).toEqual([3, 3]); // 3 fields, 3 matches
81
83
  });
82
84
 
83
85
  it("should handle arrays with shuffled identical elements", () => {
@@ -93,6 +95,22 @@ describe("Array Comparison", () => {
93
95
  const arr2 = ["c", "d", "a"];
94
96
  const result = compareArrays(arr1, arr2);
95
97
  console.log("Result:", result);
96
- expect(result).toEqual([3, 2]); // 3 fields, 3 matches
98
+ expect(result).toEqual([3, 2]); // 3 fields, 2 matches
99
+ });
100
+
101
+ it("should handle arrays with mixed types", () => {
102
+ const arr1 = [1, "two", true];
103
+ const arr2 = [1, "two", false];
104
+ const result = compareArrays(arr1, arr2);
105
+ console.log("Result:", result);
106
+ expect(result).toEqual([3, 2]); // 3 fields, 2 matches
107
+ });
108
+
109
+ it("should not count undefined values as matches", () => {
110
+ const arr1 = [1, 2, undefined];
111
+ const arr2 = [1, 2, undefined];
112
+ const result = compareArrays(arr1, arr2);
113
+ console.log("Result:", result);
114
+ expect(result).toEqual([3, 2]); // 3 fields, 2 matches
97
115
  });
98
116
  });