Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose constants #21

Closed
wants to merge 2 commits into from
Closed

Expose constants #21

wants to merge 2 commits into from

Conversation

jksdua
Copy link

@jksdua jksdua commented Sep 27, 2014

Hey Julien,

No functional changes. This exposes some constants that might be handy for libraries/apps using js-quantities.

Personally, I am using KINDS to assert that an untrusted user value matches the correct quantity type.

Please update in npm too :)

@gentooboontoo
Copy link
Owner

Thanks for the PR @jksdua. Right now, I am a little bit reluctant to expose those internal constants and make them part of the API (it would be then difficult to change the implementation without introducing breaking changes over successive versions).

As far as i understand what you are trying to do, why not try something like below?

/* For instance, checking quantities as lengths */
var kind = Qty('m');
kind.isCompatible(untrustedUserValueString);

// or
Qty(untrustedUserValueString).kind() === 'length';

If I miss the point, could you give me some real usage example of how you would use the KINDS constant?

@jksdua
Copy link
Author

jksdua commented Sep 29, 2014

Fair point, maybe we can expose it with an _ which is commonly used to indicate internal API subject to breakages.

In terms of usage, I wanted to add all the quantity types to my jsonschema validation library as custom data types.

Idea was to do this:

// custom-types.js

var Qty = require('js-quantities');

Qty.KINDS.forEach(function(kind) {
  exports[kind] = function(str) {
    var qty = Qty(str);

    if (qty.kind() !== kind) {
      return 'is not a valid ' + kind;
    }
  };
});

Alternatively, I guess I can copy the KINDS array into my own code. I'll just have to manually make sure I re-copy it every time I update js-quantities.

@gentooboontoo
Copy link
Owner

Alternatively, I guess I can copy the KINDS array into my own code. I'll just have to manually make sure I re-copy it every time I update js-quantities.

No, it would not be reasonable. The need to cleanly retrieve the list of kinds is perfectly legitimate here.

There is a pending PR (#14) which exposed a Qty.getKinds() method in one of its intermediate states of implementation but unfortunately it does not implement it anymore in its current state.

If it could suit your needs, we could add that method.

@gentooboontoo
Copy link
Owner

Closing as no more relevant. Qty.getKinds() has been implemented in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants