Closed halaprop closed 3 years ago
@halaprop, maybe something similar to middlewares?
That might be the most general form. Simpler (and enough for me) would be to let the caller specify the second parameter to the library's JSON.stringify() call.
// client code
const blacklist = [ 'foo', 'bar' ]
const options = {
prefix: 'app_',
// etc
replacer: (key, value) => blacklist.includes(key) ? undefined : value
}
Then in vue2-storage...
toJSON(data, options = {}) {
const ttl = ('ttl' in options) ? options.ttl : this.options.ttl;
return JSON.stringify({
value: data,
ttl: ttl > 0 ? ttl + Date.now() : 0,
},
options.replacer // JSON stringify filters values through this
);
}
Yes, I meant it. This is a great idea!
Thanks for this library. Would be great to have an optional replacer function in the options that gets passed to JSON.stringify. This would allow callers to add objects with cyclic references.