dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
24 lines (23 loc) • 953 B
JavaScript
import { formatArrayPath } from './formatArrayPath.js';
import { parseStringPath } from './parseStringPath.js';
export class Path {
static fromArray(arrayPath) {
return new Path(formatArrayPath(arrayPath), arrayPath);
}
constructor(strPath = '', arrayPath = parseStringPath(strPath)) {
this.arrayPath = arrayPath;
this.strPath = formatArrayPath(this.arrayPath);
}
prepend(...arrayPath) {
return this.prependPath(Path.fromArray(arrayPath));
}
prependPath(path) {
return new Path([path.strPath, this.strPath].filter(Boolean).join(this.strPath[0] !== '[' ? '.' : ''), path.arrayPath.concat(this.arrayPath));
}
append(...arrayPath) {
return this.appendPath(Path.fromArray(arrayPath));
}
appendPath(path) {
return new Path([this.strPath, path.strPath].filter(Boolean).join(path.strPath[0] !== '[' ? '.' : ''), this.arrayPath.concat(path.arrayPath));
}
}