lxsmnsyc / seroval

Stringify JS values
https://seroval.vercel.app
MIT License
532 stars 6 forks source link

feat: `OpaqueReference` #42

Closed lxsmnsyc closed 4 months ago

lxsmnsyc commented 4 months ago

Fixes #41

This feature adds a very basic OpaqueReference class. The purpose of this class is to wrap user-specified values into "opaque references": references that are hidden from the serializer. An OpaqueReference contains two values: the original value it is trying to hide, and an optional replacement value, which is what the serializer "sees".

A replacement value can only be a value that seroval can serialize. By default, a replacement value is undefined.

Example:

import { serialize, deserialize, OpaqueReference } from 'seroval';

const example = {
  transparent: "This is a transparent value.",
  opaque: new OpaqueReference('This is an opaque value.'),
};

// You can still access the original value:
console.log(example.opaque.value);

// but now it's different
const deserialized = deserialize(serialize(example));
console.log(deserialized.opaque); // undefined