replaces parseListAttrAndIndex with parseKeyedAttribute inn stateUtils

This commit is contained in:
Mose Müller 2024-04-23 14:35:22 +02:00
parent 8fd83fbd7d
commit 768be76cc8

View File

@ -49,7 +49,7 @@ function getNextLevelDictByKey(
attrName: string, attrName: string,
allowAppend: boolean = false allowAppend: boolean = false
): SerializedValue { ): SerializedValue {
const [key, index] = parseListAttrAndIndex(attrName); const [key, index] = parseKeyedAttribute(attrName);
let currentDict: SerializedValue; let currentDict: SerializedValue;
try { try {
@ -87,21 +87,39 @@ function getNextLevelDictByKey(
return currentDict; return currentDict;
} }
function parseListAttrAndIndex(attrString: string): [string, number | null] { function parseKeyedAttribute(attrString: string): [string, string | number | null] {
let index: number | null = null; let key: string | number | null = null;
let attrName = attrString; let attrName = attrString;
if (attrString.includes('[') && attrString.endsWith(']')) { if (attrString.includes('[') && attrString.endsWith(']')) {
const parts = attrString.split('['); const parts = attrString.split('[');
attrName = parts[0]; attrName = parts[0];
const indexPart = parts[1].slice(0, -1); // Removes the closing ']' const keyPart = parts[1].slice(0, -1); // Removes the closing ']'
if (!isNaN(parseInt(indexPart))) { // Check if keyPart is enclosed in quotes
index = parseInt(indexPart); if (
(keyPart.startsWith('"') && keyPart.endsWith('"')) ||
(keyPart.startsWith("'") && keyPart.endsWith("'"))
) {
key = keyPart.slice(1, -1); // Remove the quotes
} else if (keyPart.includes('.')) {
// Check for a floating-point number
const parsedFloat = parseFloat(keyPart);
if (!isNaN(parsedFloat)) {
key = parsedFloat;
} else {
console.error(`Invalid float format for key: ${keyPart}`);
}
} else { } else {
console.error(`Invalid index format in key: ${attrString}`); // Handle integers
const parsedInt = parseInt(keyPart);
if (!isNaN(parsedInt)) {
key = parsedInt;
} else {
console.error(`Invalid integer format for key: ${keyPart}`);
}
} }
} }
return [attrName, index]; return [attrName, key];
} }