mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-06-06 13:30:41 +02:00
replaces parseListAttrAndIndex with parseKeyedAttribute inn stateUtils
This commit is contained in:
parent
8fd83fbd7d
commit
768be76cc8
@ -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];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user