Skip to content

Move rounding into an explicit function #106

@sffc

Description

@sffc

Currently, the Amount constructor and convertTo function both take rounding options.

As a result, these operations may or may not convert the input to a different data type (a string). This seems challenging for TypeScript and generally for code readability:

function convert(options) {
  let a = new Amount({ value: 1.23, unit: "meter" });
  let b = a.convertTo(options);
  return b;
}

In that code, is b a Number Amount or is it a String Amount? It can't be known without inspecting options.

By having an explicit roundTo function and removing rounding options from the constructor and convertTo, the flow of data types is much more clear.

The main downside of such a model is that you end up with an extra intermediate object: a.convertTo(convertOptions).roundTo(roundOptions) has a short-lived intermediate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions