Closed jksdua closed 9 years ago
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?
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
.
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.
Closing as no more relevant. Qty.getKinds()
has been implemented in the meantime.
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 :)