haimkastner / unitsnet-js

A better way to hold unit variables and easily convert to the destination unit
https://www.npmjs.com/package/unitsnet-js
MIT License
25 stars 8 forks source link

make unit abbreviations used in .toString() accessible #20

Closed DanielBuchberger closed 2 years ago

DanielBuchberger commented 2 years ago

Would it be possible to store the abbreviation of a unit, which is used in the .toString() method in an array or a dictionary, so that you can access the abbreviation based on the given enum? Let's take the temperature for example: https://github.com/haimkastner/unitsnet-js/blob/3e6fa5d22df3d7fa190115e55967395f82b585e8/src/temperature.g.ts#L304-L331

It would be great if I could do the following (which would also shorten the .toString() method):


 unitAbbreviations = {
    Kelvins : 'K',
    DegreesCelsius: '°C',
    // and all the others...
}

 public toString(toUnit: TemperatureUnits = TemperatureUnits.Kelvins): string { 
     return this.convertFromBase(toUnit).toString() + this.unitAbbreviations[toUnit]

Alternatively also a method would work:


 public getUnitAbbreviation(toUnit: TemperatureUnits = TemperatureUnits.Kelvins): string { 
     switch (toUnit) {

            case TemperatureUnits.Kelvins:
                return ` K`;
            case TemperatureUnits.DegreesCelsius:
                return ` °C`;
            case TemperatureUnits.MillidegreesCelsius:
                return ` m°C`;
            case TemperatureUnits.DegreesDelisle:
                return ` °De`;
            case TemperatureUnits.DegreesFahrenheit:
                return ` °F`;
            case TemperatureUnits.DegreesNewton:
                return ` °N`;
            case TemperatureUnits.DegreesRankine:
                return ` °R`;
            case TemperatureUnits.DegreesReaumur:
                return ` °Ré`;
            case TemperatureUnits.DegreesRoemer:
                return ` °Rø`;
            case TemperatureUnits.SolarTemperatures:
                return ` T⊙`;
        default:
            break;
        }
        return '';

Thanks anyways for the effort!

haimkastner commented 2 years ago

Hi @DanielBuchberger Thanks for this good idea.

I have added a new function named getUnitAbbreviation as part of version 1.0.28 The new version is available on the NPM registry.