devicer.js 1.0.12 → 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.12",
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) => {
@@ -40,7 +41,7 @@ function compareArrays(arr1, arr2, max_depth = 5) {
40
41
  fields += subData[0] - 1; // Subtract 1 for the index itself
41
42
  matches += subData[1];
42
43
  }
43
- if (sortedArr1[i] === sortedArr2[i]) {
44
+ if (sortedArr2.includes(sortedArr1[i]) && sortedArr1[i]) {
44
45
  matches++;
45
46
  }
46
47
  }
@@ -52,19 +53,20 @@ function compareDatasets(data1, data2, max_depth = 5) {
52
53
  let matches = 0;
53
54
  // Ensure max_depth is not exceeded
54
55
  if (max_depth <= 0) {
55
- 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
56
58
  }
57
59
  for (const key in data1) {
58
60
  if (data1[key] !== undefined && data2[key] !== undefined) {
59
61
  fields++;
60
- if ((typeof data1[key] == "object" && data1[key]) &&
61
- (typeof data2[key] == "object" && data2[key])) {
62
- 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);
63
64
  fields += subData[0] - 1; // Subtract 1 for the key itself
64
65
  matches += subData[1];
65
66
  }
66
- else if (Array.isArray(data1[key]) && Array.isArray(data2[key])) {
67
- 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);
68
70
  fields += subData[0] - 1; // Subtract 1 for the key itself
69
71
  matches += subData[1];
70
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 (sortedArr1[i] === sortedArr2[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,8 +75,42 @@ 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
83
+ });
84
+
85
+ it("should handle arrays with shuffled identical elements", () => {
86
+ const arr1 = [1, 2, 3];
87
+ const arr2 = [3, 1, 2];
88
+ const result = compareArrays(arr1, arr2);
89
+ console.log("Result:", result);
90
+ expect(result).toEqual([3, 3]); // 3 fields, 3 matches
91
+ });
92
+
93
+ it("should handle arrays with shuffled different elements", () => {
94
+ const arr1 = ["a", "b", "c"];
95
+ const arr2 = ["c", "d", "a"];
96
+ const result = compareArrays(arr1, arr2);
97
+ console.log("Result:", result);
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
81
115
  });
82
116
  });