dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
24 lines (23 loc) • 800 B
JavaScript
import { isNumber } from '../../../utils/validation/isNumber.js';
import { isString } from '../../../utils/validation/isString.js';
const charsToEscape = ['[', ']', '.'];
export const formatArrayPath = (arrayPath) => {
let path = '';
let isRoot = true;
for (const valuePathPart of arrayPath) {
if (isString(valuePathPart)) {
const shouldBeEscaped = charsToEscape.some(charToEscape => valuePathPart.includes(charToEscape));
if (shouldBeEscaped) {
path += `['${valuePathPart}']`;
}
else {
path += `${isRoot ? '' : '.'}${valuePathPart}`;
}
}
if (isNumber(valuePathPart)) {
path += `[${valuePathPart}]`;
}
isRoot = false;
}
return path;
};