Git Product home page Git Product logo

Comments (2)

Lukinoh avatar Lukinoh commented on August 24, 2024

Hello,

I am not sure that the library was thought to have the quantity serializable.
As you mention the following quantities get serialized the same way:

const quantity1 = grams(1);
const quantity2 = kilograms(1);

// Result: {"amount":"1","dimension":{"mass":1}}

If you console.log the quantity1 and quantity2 you can see that the unit is stored in a function.
Hence the JSON.stringify strips it away.

QuantityImpl {
amount: 1,
unit: [Function: makeQuantity] UnitImpl {
  symbol: ' g',
  dimension: { mass: 1 },
  scale: 0.001,
  offset: 0,
  arithmetic: {
    from: [Function: from],
    toNative: [Function: toNative],
    add: [Function: add],
    sub: [Function: sub],
    mul: [Function: mul],
    div: [Function: div],
    pow: [Function: pow],
    abs: [Function: abs],
    compare: [Function: compare]
  }
},
dimension: { mass: 1 }
}

QuantityImpl {
amount: 1,
unit: [Function: makeQuantity] UnitImpl {
  symbol: ' kg',
  dimension: { mass: 1 },
  scale: 1,
  offset: 0,
  arithmetic: {
    from: [Function: from],
    toNative: [Function: toNative],
    add: [Function: add],
    sub: [Function: sub],
    mul: [Function: mul],
    div: [Function: div],
    pow: [Function: pow],
    abs: [Function: abs],
    compare: [Function: compare]
  }
},
dimension: { mass: 1 }
}

I don't know if the library will be update with a serialization feature.
Probably, the best solution you have without modifying the library is to write your own serializer.

The serialize function transform your quantity to:

{
  amount: number;
  symbol: string
}

And the deserialize calls the function that corresponds to the symbol with the amount.

// Trivial implementation
if (symbol === 'g') {
  return grams(amount)
} else if () {
}

from ts-units.

AlexandreRoba avatar AlexandreRoba commented on August 24, 2024

Hi @Lukinoh thanks for your reply, indeed I ended up with the same conclusion. I'm using trpc and superjson and some data cross the wire so I need to serialize it. Again for the library. I'm implementing my own serialization.

from ts-units.

Related Issues (6)

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.