Spaces:
Build error
Build error
File size: 1,746 Bytes
d9494a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | // Characters that require bracket escaping in variable paths
const SPECIAL_CHARS_REGEX = /[\s[]/;
export const needsEscaping = (key: string): boolean =>
SPECIAL_CHARS_REGEX.test(key);
export const escapePathSegment = (segment: string): string =>
needsEscaping(segment) ? `[${segment}]` : segment;
export const joinVariablePath = (segments: string[]): string =>
segments.map(escapePathSegment).join('.');
/**
* Parses a variable path string into segments, handling bracket notation.
* Examples:
* "step.normal.key" => ["step", "normal", "key"]
* "step.[key with space].value" => ["step", "key with space", "value"]
* "step.[key.with.dots]" => ["step", "key.with.dots"]
*/
export const parseVariablePath = (path: string): string[] => {
const segments: string[] = [];
let current = '';
let inBracket = false;
let segmentIndex = 0;
while (segmentIndex < path.length) {
const char = path[segmentIndex];
if (char === '[' && !inBracket) {
if (current.length > 0) {
segments.push(current);
current = '';
}
inBracket = true;
segmentIndex++;
continue;
}
if (char === ']' && inBracket) {
segments.push(current);
current = '';
inBracket = false;
segmentIndex++;
// Skip the following dot if present
if (segmentIndex < path.length && path[segmentIndex] === '.') {
segmentIndex++;
}
continue;
}
if (char === '.' && !inBracket) {
if (current.length > 0) {
segments.push(current);
current = '';
}
segmentIndex++;
continue;
}
current += char;
segmentIndex++;
}
if (current.length > 0) {
segments.push(current);
}
return segments;
};
|