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

Dollar values not parsed as numbers #96

Open
dahjelle opened this issue Nov 20, 2019 · 2 comments
Open

Dollar values not parsed as numbers #96

dahjelle opened this issue Nov 20, 2019 · 2 comments

Comments

@dahjelle
Copy link

I'm not sure that this is a design goal, but I see that dollar values (and presumably other currencies) are not parsed to be identical to plain numbers.

const {compare} = require("natural-orderby")

console.log("strings and numbers", compare()("1.00", 1)); // 0
console.log("dollar-strings and numbers", compare()("$1.00", 1)); // 1

(See https://runkit.com/dahjelle/5dd552e003c3750014962150 for a runnable version of the above.)

I haven't dived in deep to see how the algorithm in this library works, but does it seem like this is a possible and desired feature?

@yobacca
Copy link
Owner

yobacca commented Nov 20, 2019

Hi,

in general the algorithm of ´natural-orderby´ will sort a list of money values of the same (prefixed) currency correctly, if you are using the same currency prefix.
If you have a list of money values of different currencies, the amounts won't be converted before the comparison. But the values will be sorted by currency followed by the amount.

If you have a list of mixed value types like in your example (money values and plain numbers), by design the algorithm considers the money value as an alphanumeric value. So the plain number always is going to be sorted in first place.

@dahjelle
Copy link
Author

If you have a list of mixed value types like in your example (money values and plain numbers), by design the algorithm considers the money value as an alphanumeric value. So the plain number always is going to be sorted in first place.

Thanks for your response! That makes sense, and is the behavior I'm seeing.

I should give a bit more context to my use-case: I'm not only using natural-orderby for sorting, but I'm also using the compare function for a filtering algorithm. (It's really handy for have the same algorithm for both!) So, for instance, if I want to filter an array of values, I might have:

const {compare} = require("natural-orderby");

const min_amount = 6;
const number_result = ["$5.00", "$6", "$7.00"].filter(function(amount) {
  return compare()(amount, "6") >= 0;
});

const currency_result = ["$5.00", "$6", "$7.00"].filter(function(amount) {
  return compare()(amount, "$6") >= 0;
});

console.log('result with number', number_result); // ["$5.00", "$6", "$7.00"]
console.log('result with formatted currency', currency_result); // ["$6", "$7.00"]

Since I actually don't know a priori whether I'll be filtering data that is regular numeric values or currency values, it'd be handy to be able to compare currency values as numerics always.

I can definitely do it manually by examining the data before I do some filtering, but I thought it was worth opening the discussion to see if it was something worth adding to the library. :-) Perhaps there are other use-cases that it is valuable for?


By the way, thanks for a spiffy little library. It's really been great to use!

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

No branches or pull requests

2 participants