convert-units
convert-units copied to clipboard
Add the "include" option (counterpart of exclude)
As the lib do provide an exclude option to remove some of the units from a list, it would be nice to have the opposite with an include option. Case is when you only want 3 units from a large list, it's easier to say what you want to keep, than saying all that you don't want.
I think we will implement it in our local version of convert-units, we could do a PR here if you're interested.
Are you referring to the exclude
option for toBest
? So you'd like an include
option for toBest
as well? I am open to the idea of implementing a feature like this in the future. The code sample below (derived from the current beta build though it should work with the current stable build) shows how with could be achieved without needing to change the libraries code:
import configureMeasurements, { allMeasures } from 'convert-units';
const convert = configureMeasurements(allMeasures);
// uses the measure name to get a list of all possible units
const result_1 = convert(12000)
.from('mm')
.toBest({
exclude: convert()
.possibilities('length')
.filter((unit) => !['mm', 'cm'].includes(unit)),
});
console.log(result_1);
// { val: 1200, unit: 'cm', singular: 'Centimeter', plural: 'Centimeters' }
// uses a unit name to get a list of all possible units
const result_2 = convert(12000)
.from('mm')
.toBest({
exclude: convert()
.from('mm')
.possibilities()
.filter((unit) => !['mm', 'cm'].includes(unit)),
});
console.log(result_2);
// { val: 1200, unit: 'cm', singular: 'Centimeter', plural: 'Centimeters' }