dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
44 lines (43 loc) • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseStringPath = void 0;
const index_js_1 = require("../../../errors/index.js");
const combineRegExp_js_1 = require("../../../utils/combineRegExp.js");
const listIndexRegex = /\[(\d+)\]/g;
const escapedStrRegex = /\['(.+?)'\]/g;
const regularStrRegex = /[\w#@-]+(?=(\.|\[|$))/g;
const pathRegex = (0, combineRegExp_js_1.combineRegExp)(listIndexRegex, escapedStrRegex, regularStrRegex);
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 index_js_1.DynamoDBToolboxError('actions.invalidExpressionAttributePath', {
message: `Unable to match expression attribute path with schema: ${strPath}`,
payload: { attributePath: strPath }
});
}
return arrayPath;
};
exports.parseStringPath = parseStringPath;