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

More aliases for common units #551

Closed
robinrodricks opened this Issue Jan 20, 2016 · 9 comments

Comments

Projects
None yet
2 participants
@robinrodricks

robinrodricks commented Jan 20, 2016

Where do I add additional aliases for common units? Currently the following do not work:

  • for hour : h, hr, hrs
  • for minute : m, min, mins
  • for second : s, sec, secs

As I understand the current ALIASES list only allows one alias per unit type, and its already used up for plural functionality. If it supported an array of aliases I could just add it in. Also, m is currently used for 'meter' so it will conflict, therefore, it may only be possible to add 'min' and 'mins' for minutes.

The docs say 'min' is used for liquid volume/minim, but I would rather rewire it for minute. Many of these changes I could make in a fork but currently this is impossible as the ALIASES list does not take an array.

@josdejong

This comment has been minimized.

Owner

josdejong commented Jan 20, 2016

Thanks, yes lets add these aliases.

If you want to use them right now, you could use them in the expression parser like:

math.import({hrs: math.unit('hour')});
math.eval('5 hrs in minutes'); // 300 minutes

If min is documented to be liquid volume I think we should remove that as it's conflicting with the function min (sibling of max).

@robinrodricks

This comment has been minimized.

robinrodricks commented Jan 21, 2016

Is this correct then?

math.import({h: math.unit('hour')});
math.import({hr: math.unit('hour')});
math.import({hrs: math.unit('hour')});
math.import({mins: math.unit('minute')});
math.import({secs: math.unit('second')});

Or should I wrap it ilke so:

math.import({
    h: math.unit('hour'),
    hr: math.unit('hour'),
    hrs: math.unit('hour'),
    mins: math.unit('minute'),
    secs: math.unit('second')
});

I leave out the following:

  • m which is used for meter
  • s which is assigned to second
  • min which is mapped to the min() function
  • sec which is mapped to the sec() function

Perhaps you could add this into the lib making the unit system smarter!

@josdejong

This comment has been minimized.

Owner

josdejong commented Jan 21, 2016

yes that would be correct.

I was thinking, though, the solution using math.import will work in the expression parser, but will not allow you to create a unit like math.unit('5 hrs'). So a better workaround would be:

var aliases = {
  h: 'hour',
  hrs: 'hour',
  // ...
};

for (var name in aliases) {
  if (aliases.hasOwnProperty(name)) {
    var original = math.type.Unit.UNITS[aliases[name]];
    var alias = Object.create(original);
    alias.name = name;
    math.type.Unit.UNITS[name] = alias;
  }
}

Anyway it's just a few lines of code to add these aliases to math.js itself, we will do that.

@robinrodricks

This comment has been minimized.

robinrodricks commented Jan 21, 2016

OK thats nice. Thanks for the help.

@robinrodricks

This comment has been minimized.

robinrodricks commented Jan 21, 2016

I finally managed to add the following aliases in:

  • for hour : h, hr, hrs
  • for minute : mins
  • for seconds : secs

I would like to see at least these 5 added into the library. What say?

Sadly I had to exclude min and sec since there were functions by that name. I wonder if there's a way to respond to these 2 since they are extremely common in colloquial syntax. Maybe "prefer units" if there is no ( token after the identifier token, and "prefer functions" if there is a (. Sounds complicated though.

@josdejong

This comment has been minimized.

Owner

josdejong commented Jan 25, 2016

I've added these aliases to math.js, they will be available in the first next release. (h is already available).

If you want to use sec and min for units, you can always rename these functions, and override them as units with the solutions we discussed above. Of course that makes the original functions harder to use. Could be something like:

math.import({
  secTheFunction: math.sec,
  minTheFunction: math.min,
  sec:            math.unit('second'),
  min:            math.unit('minute')
}, {override: true});
@robinrodricks

This comment has been minimized.

robinrodricks commented Jan 26, 2016

Wow. Thats amazing. Thanks for the help!! I tried overloading but it crashed with an error. override is what I needed.

@robinrodricks

This comment has been minimized.

robinrodricks commented Jan 26, 2016

Oh my, for some reason the following overrides "sec" correctly but "min" refuses to be overridden.

math.import({
     minFunc: math.min,
     secFunc: math.sec,
     min: math.unit('minute'),
     mins: math.unit('minute'),
     sec: math.unit('second'),
     secs: math.unit('second'),
}, {override:true});

When I type 25 min in sec, it doesn't evaluate. Only 25 mins in sec does.

Also, calling min(10,20) still works, means I have not overridden it fully.

@robinrodricks

This comment has been minimized.

robinrodricks commented Jan 26, 2016

Its alright. Don't bother. I had to change the source code in various places to change min to minFunc. I don't think you should waste your time on this. Thanks for all the help, dude!

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