-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseDiff.js
More file actions
166 lines (153 loc) · 3.91 KB
/
useDiff.js
File metadata and controls
166 lines (153 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { useState } from "react"
function useDiff() {
const [isLoading, setIsLoading] = useState(false)
const [missingKeys, setMissingKeys] = useState({
compOne: [],
compTwo: [],
totalCount: 0,
timeTaken: 0,
})
const [metaData, setMetaData] = useState({
fileOne: {
name: "",
size: 0,
flattenObject: [],
},
fileTwo: {
name: "",
size: 0,
flattenObject: [],
},
timeTakenToCompare: 0,
})
const compareContent = (files) => {
setIsLoading(true)
const { fileOne, fileTwo } = files
console.log("compareContent")
try {
const t1 = performance.now()
const arr1 = flattenObject1(fileOne.content, 0)
const arr2 = flattenObject1(fileTwo.content, 0)
const arrSet1 = new Set(arr1.map((item) => item.keyValue))
const arrSet2 = new Set(arr2.map((item) => item.keyValue))
const comp1 = arr1.filter((item) => !arrSet2.has(item.keyValue))
const comp2 = arr2.filter((item) => !arrSet1.has(item.keyValue))
const t2 = performance.now()
console.log(`compare completed in ${t2 - t1} milliseconds`)
setMissingKeys({
compOne: comp1,
compTwo: comp2,
totalCount: comp1.length + comp2.length,
timeTaken: t2 - t1,
})
setMetaData({
fileOne: {
name: fileOne.name,
size: bytesToSize(fileOne.file.size),
flattenObject: arr1,
},
fileTwo: {
name: fileTwo.name,
size: bytesToSize(fileTwo.file.size),
flattenObject: arr2,
},
t1,
t2,
timeTakenToCompare: t2 - t1,
})
setIsLoading(false)
return {
compOne: comp1,
compTwo: comp2,
totalCount: comp1.length + comp2.length,
timeTaken: t2 - t1,
error: null,
}
} catch (error) {
setIsLoading(false)
console.log(error)
}
return {
compOne: [],
compTwo: [],
totalCount: 0,
timeTaken: 0,
error: "Something went wrong",
}
}
function bytesToSize(bytes) {
if (!bytes) return 0
var sizes = ["Bytes", "KB", "MB", "GB", "TB"]
if (bytes === 0) return "0 Byte"
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)))
return Math.round(bytes / Math.pow(1024, i), 2) + " " + sizes[i]
}
const flattenObject1 = (obj, level) => {
let arr = []
if (typeof obj === "object") {
Object.keys(obj).forEach((key) => {
if (typeof obj[key] === "object" && obj[key] !== null) {
arr.push({ keyValue: key, level, type: Array.isArray(obj[key]) ? "array" : typeof obj[key] })
arr.push(
...flattenObject1(obj[key], level + 1).map((subKey) => ({
...subKey,
keyValue: `${key}.${subKey.keyValue}`,
}))
)
} else {
arr.push({ keyValue: key, level, type: typeof obj[key], value: obj[key] })
}
})
}
return arr
}
// eslint-disable-next-line no-extend-native
String.prototype.trunc = function (n) {
if (this.length < n) return this
return this.slice(0, n / 2 - 1) + (this.length > n ? "..." : "") + this.slice(this.length - n / 2 + 1, this.length)
}
const clearFileData = (name) => {
console.log(name)
setMissingKeys({
...missingKeys,
[name]: [],
})
setMetaData({
...metaData,
[name]: {
name: "",
size: 0,
flattenObject: [],
},
})
}
const clearComparison = () => {
setMissingKeys({
compOne: [],
compTwo: [],
totalCount: 0,
})
setMetaData({
fileOne: {
name: "",
size: 0,
},
fileTwo: {
name: "",
size: 0,
},
timeTakenToCompare: 0,
})
setIsLoading(false)
}
return {
one: 1,
compareContent,
missingKeys,
clearComparison,
metaData,
isLoading,
clearFileData,
}
}
export default useDiff