UNPKG

dynamodb-toolbox

Version:

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

28 lines (27 loc) 855 B
import { pipe } from './pipe.js'; export class Suffixer { constructor(suffix, { delimiter = '#' } = {}) { this.transformerId = 'suffix'; this.suffix = suffix; this.delimiter = delimiter; } encode(decoded) { return [decoded, this.suffix].join(this.delimiter); } decode(encoded) { return encoded.endsWith(`${this.delimiter}${this.suffix}`) ? encoded.slice(0, encoded.length - this.delimiter.length - this.suffix.length) : encoded; } toJSON() { return { transformerId: this.transformerId, suffix: this.suffix, delimiter: this.delimiter }; } pipe(transformer) { return pipe(this, transformer); } } export const suffix = (suffix, { delimiter = '#' } = {}) => new Suffixer(suffix, { delimiter });