UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

40 lines (39 loc) 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.matchListProjection = exports.matchMapProjection = exports.matchItemProjection = exports.sanitize = void 0; const sanitize = (str) => str.replace(/\\/g, '\\\\').replace(/[-[\]/{}()*+?.^$|]/g, '\\$&'); exports.sanitize = sanitize; const matchItemProjection = (attributeName, projectedAttributes) => { const sanitizedAttributeName = (0, exports.sanitize)(attributeName); return matchProjection(new RegExp(`^(${sanitizedAttributeName}|\\['${sanitizedAttributeName}'\\])(?=\\.|\\[|$)`), projectedAttributes); }; exports.matchItemProjection = matchItemProjection; const matchMapProjection = (attributeName, projectedAttributes) => { const sanitizedAttributeName = (0, exports.sanitize)(attributeName); return matchProjection(new RegExp(`^(\\.${sanitizedAttributeName}|\\['${sanitizedAttributeName}'])(?=\\.|\\[|$)`), projectedAttributes); }; exports.matchMapProjection = matchMapProjection; const matchListProjection = (projectedAttributes) => matchProjection(/\[\d+\]/, projectedAttributes); exports.matchListProjection = matchListProjection; const matchProjection = (attributeNameRegex, projectedAttributes) => { if (projectedAttributes === undefined) { return { isProjected: true }; } const childrenAttributes = []; for (const attributePath of projectedAttributes) { const attributeMatch = attributePath.match(attributeNameRegex); if (attributeMatch !== null) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const firstMatch = attributeMatch[0]; childrenAttributes.push(attributePath.slice(firstMatch.length)); } } if (childrenAttributes.length === 0) { return { isProjected: false }; } if (childrenAttributes.some(attribute => attribute === '')) { // We do not add childrenAttributes as we want all of them return { isProjected: true }; } return { isProjected: true, childrenAttributes }; };