Unit conversion : what do you think about this js lib?

I’m working on our farm data input forms, which will push to farmOS. But it’s also giving me a chance to review what we’ll need for the API switchboard, and a reminder of the tricky bits.

One thing I’m considering is units - so complicated… but there is (what seems to be) an amazing unit conversion library. It doesn’t have every unit we’d want but it has many (like I’d way row feet, yards, etc for smaller producers. You can also do rates with different values.

Here’s the lib: https://gentooboontoo.github.io/js-quantities/?utm_source=cdnjs&utm_medium=cdnjs_link&utm_campaign=cdnjs_library
Here’s the list of units: https://github.com/gentooboontoo/js-quantities/blob/master/src/quantities/definitions.js

Seems like a well designed lib. Any ideas @mstenta @sudokita @mark.easter @plawrence maybe @jgaehring ? Is this something we could use across many OpenTEAM components - would you consider integrating it into your software or does this not solve a problem for you?

It seems (especially given his clear ‘definitions’ file) that this seems like a great start. Maybe fork or just contribute back additional items as needed.

2 Likes

As someone who doesn’t like to use libraries, I would definitely use this library. Looks very well designed. And this seals the deal for me:

JS-quantities is built as UMD and ES modules and can be used with Node.js and browsers. It has no dependencies.

Although I don’t know that Field Kit has a clear-cut need for this right now, I can easily see where I might at some point, perhaps soon. @mstenta or @paul121 would know better, but I imagine there are PHP/Drupal utils that farmOS would use for something like this, if not already.

Yea, these definitions are great! I wonder if these definition files could actually be more useful than the library itself, as a way of specifying a set of units officially supported by OpenTEAM members and projects. It could be published, with a note that JS-quantities provides an implementation for conversions etc, and then it would be up to folks to decide if they wanted to use JS-quantities, or some other implementation or roll their own.

It’d be great if we could eventually make this extensible, either as a fork, or as something we could contribute back. That way you could pick and choose which definitions to use, bringing your own custom units and/or omitting some of the default definitions.

1 Like

I support any/all experimentation with standardized unit conversion libraries! :slight_smile:

units - so complicated

Agreed! In farmOS core we basically decided to take an unopinionated approach to units, to enable as much flexibility as possible, and leave it to the higher level UIs and convention layers to make those decisions. So it makes perfect sense to me to try out this library client side.

For context, here are some old threads in the farmOS issue queue that might be helpful to read:

https://www.drupal.org/project/farm_log/issues/2353137

https://www.drupal.org/project/farm/issues/2361915

Sorry for the slow reply.

Thanks @jgaehring and @mstenta for the feedback. Seems like this is worth pursuing. I’m extracting myself from more non-technical work and would love to try to get this in place in an upcoming work package. Maybe see if we could maintain a branch of it and push back changes to your point.

It would make our forms super duper powerful :slight_smile: I could image a ‘units’ type question which necessarily includes units. You could select a few options to allow the user to choose, or they can type to dropdown. Then when you go to download the data, even if users put in all kinds of different units, you can choose to convert them all to whatever desired unit you want… That’d be awesome. Ditto for people using APIs, which would help mightily on the everyone-talking-to-everyone from.

1 Like