Update json.test.js

This commit is contained in:
William Harrison
2025-04-11 11:46:26 +08:00
committed by GitHub
parent bc619508f2
commit 7879637c2d
+7 -10
View File
@@ -37,8 +37,7 @@ const files = fs.readdirSync(domainsPath);
function findDuplicateKeys(jsonString) {
const duplicateKeys = new Set();
const objectStack = [];
const keyMapStack = [];
const keyStack = [];
const keyRegex = /"(.*?)"\s*:/g;
@@ -47,29 +46,27 @@ function findDuplicateKeys(jsonString) {
const char = jsonString[i];
if (char === '{') {
keyMapStack.push({});
objectStack.push('{');
keyStack.push({});
i++;
continue;
}
if (char === '}') {
keyMapStack.pop();
objectStack.pop();
keyStack.pop();
i++;
continue;
}
keyRegex.lastIndex = i;
const match = keyRegex.exec(jsonString);
if (match && objectStack.length) {
if (match && match.index === i && keyStack.length > 0) {
const key = match[1];
const currentKeyMap = keyMapStack[keyMapStack.length - 1];
const currentScope = keyStack[keyStack.length - 1];
if (currentKeyMap[key]) {
if (currentScope[key]) {
duplicateKeys.add(key);
} else {
currentKeyMap[key] = true;
currentScope[key] = true;
}
i = keyRegex.lastIndex;