GithubHelp home page GithubHelp logo

haimkastner / unitsnet-js Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 8.0 2.6 MB

A better way to hold unit variables and easily convert to the destination unit

Home Page: https://www.npmjs.com/package/unitsnet-js

License: MIT License

TypeScript 99.90% ANTLR 0.10%
conversion units-of-measure units unit-converter converter unit measure measures measurement measurements

unitsnet-js's Issues

Math in JS

Shouldn't some library be used to do Math within JavaScript land? It's not accurate to use native math operations with decimals.

For example common problem
image

Full support for units.net features?

Hey there,

Stumbled on this library recently, for what it offers it’s a great API. I was wondering if you’d looked into the possibility of a full replication of units.net? An example of what might be possible, is cross conversion of units when you multiply and divide, so length.mul(anotherLength) returns an area, that sort of thing.

looking in the original repo, it’s all driven by even more codegen than is used here. I was just wondering if you’d already looked into what that whole task would look like, and if it would even work.

not sure if typescript method overloading would allow this sort of thing, for example.

That full API, where units are tracked for you in a type safe way, would be pretty freakin cool, I think.

thanks!

make unit abbreviations used in .toString() accessible

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:

public toString(toUnit: TemperatureUnits = TemperatureUnits.Kelvins): string {
switch (toUnit) {
case TemperatureUnits.Kelvins:
return this.Kelvins + ` K`;
case TemperatureUnits.DegreesCelsius:
return this.DegreesCelsius + ` °C`;
case TemperatureUnits.MillidegreesCelsius:
return this.MillidegreesCelsius + ` m°C`;
case TemperatureUnits.DegreesDelisle:
return this.DegreesDelisle + ` °De`;
case TemperatureUnits.DegreesFahrenheit:
return this.DegreesFahrenheit + ` °F`;
case TemperatureUnits.DegreesNewton:
return this.DegreesNewton + ` °N`;
case TemperatureUnits.DegreesRankine:
return this.DegreesRankine + ` °R`;
case TemperatureUnits.DegreesReaumur:
return this.DegreesReaumur + ` °Ré`;
case TemperatureUnits.DegreesRoemer:
return this.DegreesRoemer + ` °Rø`;
case TemperatureUnits.SolarTemperatures:
return this.SolarTemperatures + ` T⊙`;
default:
break;
}
return this.value.toString();

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!

Generate all prefixes units

For example, most of the bit rate unit prefixes are not supported yet.

const prefixesFactor: { [key in Prefix]: number } = {
Exa: 1e18,
Peta: 1e15,
Tera: 1e12,
Giga: 1e9,
Mega: 1e6,
Kilo: 1e3,
Hecto: 1e2,
Deca: 1e1,
Deci: 1e-1,
Centi: 1e-2,
Milli: 1e-3,
Micro: 1e-6,
Nano: 1e-9,
Pico: 1e-12,
Femto: 1e-15,
};

Which doesn't contain the prefixes Kibi Mebi Gibi Tebi Pebi Exbi

Allow truncate fraction digits on 'toString'

When formatting a unit to string, it's printed as:

console.info(angle.toString(AngleUnits.Radians)); // 3.141592653589793 rad

It could be nice to truncate the fraction for printing as:

console.info(angle.toString(AngleUnits.Radians, 2)); // 3.14 rad

Length.multiply() multiplicator

Hey,

I noticed that you can muliply a length unit by another length:

const length1 = Length.FromMeters(10);
const length3 = Length.FromMeters(3);
const results3 = length1.multiply(length3);

console.log(results3.toString(LengthUnits.Meters)) // 30 m

In my oppinion this is not correct. The result should not be 30 meters, but 30 squaremeters if we want to stay mathematically correct.

The param for length.multiply() should just be a number (as the multiplicator). This would cause in mathematically correct behavior.

Kind regards,
Georg

Missing Unit Abbreviations

Hello,

I've been using the unitsnet-js library and I noticed that some of the unit abbreviations seem to be missing, particularly for units of length and force. For instance, I could not find abbreviations for kilometers, centimeters, millimeters, and kilonewtons etc.

Here's a simple example demonstrating this:

import { Length, LengthUnits } from 'unitsnet-js';

let length = Length.FromLengths(5, LengthUnits.Kilometers);
console.log(length.getUnitAbbreviation());  // Expected output: 'km'

In this case, I'd expect the output to be 'km' for kilometers, but it seems like the function returns an empty string.

This appears to be a significant issue, as it limits the functionality of the library. Are there any plans to add these missing abbreviations in a future update? If these abbreviations are intentionally missing, could you provide guidance on the best way to handle these cases?

Thank you for your attention to the matter.

fix 'toString' format of prefixes units

The prefixes to string currently are using the original unit abbreviation.

For example, the abbreviation of meters is m and the kilometers abbreviation are also m instead of km

Equally use singular for unit types as in the c# project

We're using the unitsnet-js as a way to have the same information in our frontend, that we also have in our backend.
However in this package all unitnames are in plural (e.g. Meters, Millimeters, Inches ) while in the c# project they are singular (e.g. Meter, Millimeter, Inch ) which makes conversion between our c# backend and our typescript frontend really painful.
Is this a conscious decision?

Implement lazy load for each unit property

Don't convert from the base again for each request for a specific unit, just after the first unit request keeps the result for the next request.

For example for Angle the base unit is Degrees, so in the first request for the angle as Radians convert from the base and keep the radians value for the radians' request.

Why ist "Millimeter" Missing in all Units?

Why ist Millimeter missing in all Units.
For example in length, acceleration, speed.
We have meters, centimeters, micrometers,...
But no millimeters.
Is there a reason for that?

Support Cross-Technologies Unit & API Specifications

As part of the powerful and wide support in multiple languages in the unit definition, I think it will be a good idea to standardize the way how unit is represented in an API spec, and how it will be exposed/loaded.

The API object should look similar to:

export interface LengthDto {
    value: number;
    unit: LengthUnits;
}

And this is also how it will be represented in the OpenAPI specification.

The JSON will look like this:

{
   "value":100.01,
   "unit":"Meter"
}

As part of the full JSON payload something like:

{
   "someInfo":"someValue",
   "someInfoLength":{
      "value":100.01,
      "unit":"Meter"
   }
}

Any unit class will have an API to expose and load it:

export interface LengthDto {
    value: number;
    unit: LengthUnits;
}

export  class Length {

  ...
  ...

  public toDto(holdInUnit: LengthUnits = LengthUnits.Meters): LengthDto {
        return {
            value: this.convert(holdInUnit),
            unit: holdInUnit
        } ;
    }

    public static fromDto(dtoLength: LengthDto): Length {
        return new Length(dtoLength.value, dtoLength.unit);
    }
}

See also #29 for motivation

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.