MCPcopy
hub / github.com/4ian/GDevelop / parseVariablePath

Function parseVariablePath

newIDE/app/src/EditorFunctions/ApplyVariableChange.js:33–98  ·  view source on GitHub ↗
(
  variablePath: string
)

Source from the content-addressed store, hash-verified

31
32// Parse variable path to handle both dot notation and array indexing
33const parseVariablePath = (
34 variablePath: string
35): Array<{| type: 'property' | 'index', value: string |}> => {
36 const segments = [];
37 let currentSegment = '';
38 let i = 0;
39
40 while (i < variablePath.length) {
41 const char = variablePath[i];
42
43 if (char === '.') {
44 // End of property segment
45 if (currentSegment.trim()) {
46 segments.push({ type: 'property', value: currentSegment.trim() });
47 currentSegment = '';
48 }
49 i++;
50 } else if (char === '[') {
51 // Start of array index
52 if (currentSegment.trim()) {
53 segments.push({ type: 'property', value: currentSegment.trim() });
54 currentSegment = '';
55 }
56
57 // Find the closing bracket
58 i++; // Skip opening bracket
59 let indexContent = '';
60 let foundClosingBracketWithProperIndex = false;
61 while (i < variablePath.length && variablePath[i] !== ']') {
62 indexContent += variablePath[i];
63 i++;
64 }
65
66 if (i < variablePath.length && variablePath[i] === ']') {
67 // Valid array index
68 const indexValue = indexContent.trim();
69 if (indexValue && !isNaN(parseInt(indexValue, 10))) {
70 segments.push({ type: 'index', value: indexValue });
71 foundClosingBracketWithProperIndex = true;
72 } else {
73 throw new Error(
74 `Content of the index is invalid ("${indexValue}") - it should be a number.`
75 );
76 }
77 i++; // Skip closing bracket
78 }
79
80 if (!foundClosingBracketWithProperIndex) {
81 throw new Error(
82 'Improperly formatted array index. Please check the variable path - it should be formatted like this: `myVar[1].prop`, `myVar`, `myVar.prop`, etc...'
83 );
84 }
85 } else {
86 currentSegment += char;
87 i++;
88 }
89 }
90

Callers 3

applyVariableChangeFunction · 0.85
applyVariableDeletionFunction · 0.85
getVariableAtPathFunction · 0.85

Calls 2

pushMethod · 0.65
trimMethod · 0.45

Tested by

no test coverage detected