gentooboontoo / js-quantities

JavaScript library for quantity calculation and unit conversion
http://gentooboontoo.github.io/js-quantities/
MIT License
396 stars 102 forks source link

get kind base-unit #93

Closed Shoelace closed 4 years ago

Shoelace commented 6 years ago

Hi, love the library.. trying to use it in my project.

i want to let my user select "default" unit types for any of the kinds. ie length -> km, mass -> pound , speed -> knot or whatever. so i tried to create a table of "kinds" with their associated "units" and i've hit 2 issues.

( see https://jsfiddle.net/shoelace/anxqj0g2/ for my example )

  1. i wanted to default each kind to be the unit of its basetype.. ie from Qty.toBase() but i cant seem to retreive it.

ideally i'd like a function like Qty.getbaseUnit('area') ==> m2 (or hectare ) or qty.Base() ==> m2

which brings me to

  1. the toBase() unit is not always in the list returned Qty.getUnits( kind );

eg for area

var qty = Qty(1,'area').toBase();  //4046.85642 m2

and m2 is not in Qty.getUnits( 'area' );

am i missing something? is there a better way of doing this?

orontee commented 4 years ago

I guess "base unit" refers to "SI base units" (cf. https://en.wikipedia.org/wiki/SI_base_unit) and there's no such unit for areas.

gentooboontoo commented 4 years ago

@orontee is right. toBase returns a quantity with units only composed of "base" units (SI).

Regarding areas, m2 is not really a base unit but a unit based on the SI unit of length.

BTW, you could use something like below to achieve what you want to do :

let getBaseUnits = kind => {
  return Qty(Qty.getUnits(kind)[0]).toBase().units();
};

getBaseUnits('area');

I am closing the issue but feel free to reopen if needed.

Shoelace commented 3 years ago

thanks @gentooboontoo , i'll see what I can do with that snippet. :)