buge / ts-units Goto Github PK
View Code? Open in Web Editor NEWPhysical Units for TypeScript
License: Apache License 2.0
Physical Units for TypeScript
License: Apache License 2.0
Because we model only non-zero dimensions, it's currently possible to assign incompatible quantities to one another due to their type covariance:
const speed: Quantity<{length: 1, seconds: -1}> = metersPerSecond(5);
const length: Quantity<{length: 1}> = speed; // ouch
While we can't protect against this for all user-defined dimensions, we should look into making at least the built-in dimensions more robust by modeling known dimensions with exponent 0. I need to think through all the possible corner-cases though.
Hey buge,
thanks a lot for this library, I really appreciate it :)
When I add this library via yarn add @buge/ts-units
, then do yarn install
and yarn start
, I receive the following errors:
Failed to parse source map from '/home/marcel/projects/scale_valve/frontend/.yarn/cache/@buge-ts-units-npm-1.2.2-9c71d2edc8-8b1be825cf.zip/node_modules/@buge/src/dimension.ts' file: Error: ENOENT: no such file or directory, open '/node_modules/@buge/src/dimension.ts'
Failed to parse source map from '/home/marcel/projects/scale_valve/frontend/.yarn/cache/@buge-ts-units-npm-1.2.2-9c71d2edc8-8b1be825cf.zip/node_modules/@buge/src/exponent.ts' file: Error: ENOENT: no such file or directory, open '/node_modules/@buge/src/exponent.ts'
Failed to parse source map from '/home/marcel/projects/scale_valve/frontend/.yarn/cache/@buge-ts-units-npm-1.2.2-9c71d2edc8-8b1be825cf.zip/node_modules/@buge/src/mass/dimension.ts' file: Error: ENOENT: no such file or directory, open '/node_modules/@buge/src/mass/dimension.ts'
Failed to parse source map from '/home/marcel/projects/scale_valve/frontend/.yarn/cache/@buge-ts-units-npm-1.2.2-9c71d2edc8-8b1be825cf.zip/node_modules/@buge/src/mass/index.ts' file: Error: ENOENT: no such file or directory, open '/node_modules/@buge/src/mass/index.ts'
Failed to parse source map from '/home/marcel/projects/scale_valve/frontend/.yarn/cache/@buge-ts-units-npm-1.2.2-9c71d2edc8-8b1be825cf.zip/node_modules/@buge/src/unit.ts' file: Error: ENOENT: no such file or directory, open '/node_modules/@buge/src/unit.ts'
If we take a look at the published dimension.js.map
, there is this:
{"version":3,"file":"dimension.js","sourceRoot":"","sources":["../src/dimension.ts"],"...}
but that file won't get published.
Is there a way you can fix this? :)
Many thanks!
After I installed the package I noticed that volume
it's not in the @buges/ts-units
folder. The build script works fine, I cloned the project and run build script and volume is there so I guess it's only missing in the published package for some reason.
Version installed: 1.2.0
Hi all, Thanks for this amazing libraries.
I'm trying to serialize a length in JSON to store it in a JSON Document but I can seem to find the unit in the serialized object. Am I correct. Ths object seem to be serialized as
{"amount":12.5,"dimension":{"length":1}}
it serialize the same way no matter if its a km or a meter. Any advice on how to proceed? I do not want to loose the unit in the process.
Thanks.
Hello,
It is not an issue, but a question.
As you may know, JavaScript and decimal it's not great love.
So if you do 0.1 + 0.2
the result will be 0.30000000000000004
.
Currently, in your library you are using native representation of numbers, so such issue could arise (which is fine me).
However, there is no easy way to replace the default behaviour. So I had to duplicate the unit.ts
and adapt it to use decimal.js.
Do you plan to offer a way to have better integration with libraries such as decimal.js
?
P.S. Don't hesitate to edit the title of the issue.
Hi 👋,
I’m looking for a way to safely type time units in my project. Unfortunately there are not much libraries out there in the wild, so I was quite exited about this one. Unfortunately I can’t use it in my project, since I can’t write unit tests with the node (v16) assert module:
const assert = require('assert');
const { seconds } = require('@buge/ts-units/time');
assert.deepStrictEqual(seconds(10), seconds(10));
throws AssertionError [ERR_ASSERTION]: Values have same structure but are not reference-equal
.
Also the diff is not really nice since the objects returned by seconds()
are huge with a lot of methods attached. I’m not sure if the root-cause is a problem within nodejs or within this library. But I’m also a bit concerned about those huge objects. For my use case I would need to create quite a few instances with seconds()
within short time-interval so this might affect the performance (I haven’t tested it though).
Ideally seconds()
would just return a data-only object, e.g. { value: 10, unit: 'seconds' }
and all the methods could be moved to standalone functions, e.g.:
const { seconds, Time, plus } = require('@buge/ts-units/time');
console.log(plus(seconds(10), seconds(5));
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.