haimkastner / unitsnet-js Goto Github PK
View Code? Open in Web Editor NEWA 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
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
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!
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:
unitsnet-js/src/temperature.g.ts
Lines 304 to 331 in 3e6fa5d
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!
For example, most of the bit rate
unit prefixes are not supported yet.
unitsnet-js/generator-scripts/src/units-generator.ts
Lines 9 to 25 in 619d18a
Which doesn't contain the prefixes Kibi
Mebi
Gibi
Tebi
Pebi
Exbi
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
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
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.
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
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?
When trying to execute the OpenAPI Spec Example I'm getting a CORS issue.
Specs:
https://haimkastner.github.io/unitsnet-openapi-spec-example/
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.
For example in length, acceleration, speed.
We have meters, centimeters, micrometers,...
But no millimeters.
Is there a reason for that?
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.