mirror of
https://github.com/tiennm99/is-a-dev.git
synced 2026-10-20 02:17:34 +00:00
Update json.test.js
This commit is contained in:
1 parent
a0762cc49c
commit
165500fdba
1 file changed
+36
-17
+36
-17
@@ -36,30 +36,49 @@ const domainsPath = path.resolve("domains");
|
|||||||
const files = fs.readdirSync(domainsPath);
|
const files = fs.readdirSync(domainsPath);
|
||||||
|
|
||||||
function findDuplicateKeys(jsonString) {
|
function findDuplicateKeys(jsonString) {
|
||||||
const duplicateKeys = [];
|
const duplicateKeys = new Set();
|
||||||
|
const objectStack = [];
|
||||||
|
|
||||||
const stack = [];
|
const keyRegex = /"(.*?)"\s*:/g;
|
||||||
|
|
||||||
const obj = JSON.parse(jsonString, (key, value) => {
|
let match;
|
||||||
if (typeof key === "string") {
|
let currentKeys = {};
|
||||||
const current = stack[stack.length - 1];
|
let lastIndex = 0;
|
||||||
if (current) {
|
|
||||||
if (current[key]) {
|
for (let i = 0; i < jsonString.length; i++) {
|
||||||
duplicateKeys.push(key);
|
const char = jsonString[i];
|
||||||
|
|
||||||
|
// Start of new object
|
||||||
|
if (char === '{') {
|
||||||
|
objectStack.push(currentKeys);
|
||||||
|
currentKeys = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// End of current object
|
||||||
|
if (char === '}') {
|
||||||
|
currentKeys = objectStack.pop() || {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match key if it's at the current index
|
||||||
|
if (i === lastIndex) {
|
||||||
|
keyRegex.lastIndex = i;
|
||||||
|
match = keyRegex.exec(jsonString);
|
||||||
|
if (match && match.index === i) {
|
||||||
|
const key = match[1];
|
||||||
|
|
||||||
|
if (currentKeys[key]) {
|
||||||
|
duplicateKeys.add(key);
|
||||||
} else {
|
} else {
|
||||||
current[key] = true;
|
currentKeys[key] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastIndex = keyRegex.lastIndex;
|
||||||
|
i = lastIndex - 1; // skip ahead
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof value === "object" && value !== null) {
|
return [...duplicateKeys];
|
||||||
stack.push({});
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
});
|
|
||||||
|
|
||||||
return [...new Set(duplicateKeys)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function validateFields(t, obj, fields, file, prefix = "") {
|
async function validateFields(t, obj, fields, file, prefix = "") {
|
||||||
|
|||||||
Reference in new issue
Block a user