Code Monkey home page Code Monkey logo

ts-units's People

Contributors

buge avatar dependabot[bot] avatar hellow554 avatar lukinoh avatar ofqwx avatar tervay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ts-units's Issues

Covariance permits illogical assignments

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.

Failed to parse source map

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!

Volume it's not included in published package

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

[Question] Lost units in JSON

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.

Alternative to native representation of numbers

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.

Consider providing plain data structures without methods

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));

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.