dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
29 lines (28 loc) • 975 B
JavaScript
import { pipe } from './pipe.js';
export class JSONStringifier {
constructor({ space, replacer, reviver } = {}) {
this.transformerId = 'jsonStringify';
this.space = space;
this.replacer = replacer;
this.reviver = reviver;
}
encode(formatted) {
return JSON.stringify(formatted, this.replacer, this.space);
}
decode(transformed) {
return JSON.parse(transformed, this.reviver);
}
toJSON() {
if (this.replacer !== undefined || this.reviver !== undefined) {
console.warn('Schema DTO is probably incomplete when using `replacer` or `reviver` options in JSON Stringifier.');
}
return {
transformerId: this.transformerId,
...(this.space !== undefined ? { space: this.space } : {})
};
}
pipe(transformer) {
return pipe(this, transformer);
}
}
export const jsonStringify = (options = {}) => new JSONStringifier(options);