Fix https://github.com/twentyhq/twenty/issues/17524 Recent update meant to support variable with spaces and dots broken the database event variables. Inserting a variable `My Name` will be inserted as `{{trigger.[My Name]}}` For example, for a database event, we send `trigger.properties.after.id` instead of `id`. With the new code, we will consider `properties.after.id` as a variable to wrap in `[]`, giving `trigger.[properties.after.id]` which cannot be resolve. Let's only support variable with spaces. Removing the logic to wrap variable with dots.
70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
// 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;
|
|
};
|