Code Monkey home page Code Monkey logo

node-primitive-enum's Issues

Is defaultArrayTransform/defaultObjectTransform/defaultTransform safe to use/why does it exist?

I see in the README:

  • Enum.defaultArrayTransform Default 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.