-
Notifications
You must be signed in to change notification settings - Fork 0
Description
I see in the README:
Enum.defaultArrayTransformDefault transform to use on arrays.
If I were to set that from my one package, wouldn’t I break all other packages using node-primitive-enum which assume default behavior? Consider that one project may serialize to JSON with the default values and then later another package which overrides the defaults is added to the application. Depending on the order of require() calls, the enum instances which one project would use to deserialize the JSON might suddenly be using a different, incompatible mapping without any code change but due to an external dependency addition.
For a demo, see https://gist.github.com/binki/c75c9387f616ad6f6bda20d4afe1f7fa
Could this API be removed or replaced to make it harder to encounter such an issue?
A way to avoid this while still enabling the convenience of defaults would be to allow a local instance of Enum be created with those particular options. E.g., something like:
const EnumCore = require('primitive-enum');
const Enum = Enum.withOptions({
defaultArrayTransform: Enum.identity,
});
const enum1 = Enum(['a', 'b']);For project-wide use, a project could define its own module exporting its withOptions()-ized Enum variant and just require() that module in all the different modules which use primitive-enum.