dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
40 lines (39 loc) • 1.67 kB
JavaScript
import { DynamoDBToolboxError } from '../../../errors/index.js';
import { combineRegExp } from '../../../utils/combineRegExp.js';
const listIndexRegex = /\[(\d+)\]/g;
const escapedStrRegex = /\['(.+?)'\]/g;
const regularStrRegex = /[\w#@-]+(?=(\.|\[|$))/g;
const pathRegex = combineRegExp(listIndexRegex, escapedStrRegex, regularStrRegex);
export const parseStringPath = (strPath) => {
var _a;
if (strPath === '') {
return [];
}
const arrayPath = [];
let attrPathTail;
for (const attrMatch of strPath.matchAll(pathRegex)) {
// NOTE: Order of those matches follows those of combined regExps above
const [match, listIndexMatch, escapedStrMatch, tail] = attrMatch;
attrPathTail = tail;
const matchedKey = (_a = escapedStrMatch !== null && escapedStrMatch !== void 0 ? escapedStrMatch : listIndexMatch) !== null && _a !== void 0 ? _a : match;
const matchType = escapedStrMatch !== undefined
? 'escapedStr'
: listIndexMatch !== undefined
? 'listIndex'
: 'regularStr';
switch (matchType) {
case 'listIndex':
arrayPath.push(parseInt(matchedKey));
break;
default:
arrayPath.push(matchedKey);
}
}
if (arrayPath.length === 0 || (attrPathTail !== undefined && attrPathTail.length > 0)) {
throw new DynamoDBToolboxError('actions.invalidExpressionAttributePath', {
message: `Unable to match expression attribute path with schema: ${strPath}`,
payload: { attributePath: strPath }
});
}
return arrayPath;
};