Skip to content

Amount + Sequence Units #118

@sffc

Description

@sffc

Now that both of these proposals are Stage 2 🎊, we need to figure out how they fit together.

We had some brainstorming discussions with myself and @eemeli and @lucacasonato and @ljharb and others at TC39.

Some points that people have raised; not all of these bullet points are universally held:

  • Adding sequence units should not (significantly) hurt ergonomics of non-sequence units
  • Sequence units should be first-class in most functionality, including the constructor, convertTo, and formatting
  • We should try to keep just 1 Amount type, not adding a second to support sequence units

There are a handful of shapes to consider:

  1. Amount is a single unit; sequence units are returned as arrays
  2. Amount is a sequence unit; single units are returned as objects
  3. Amount is a sequence unit; getting a single unit means querying the first entry of it

A design that seems to solve most of the design constraints is an Amount.prototype.total function that behaves like this:

let amt1 = new Amount(123, { unit: "centimeter" });
amt1.getMeasures()  // [{ unit: "centimeter", value: 123 }]
amt1.total()  // { unit: "centimeter", value: 123 }]

let amt2 = amt1.convertTo({ units: ["meter", "centimeter"] });
amt1.getMeasures()  // [{ unit: "meter", value: 1 }, { unit: "centimeter", value: 23 }]
amt2.total()  // { unit: "meter", value: 1.23 }

Some open questions:

  • Maybe .getMeasures() should be a getter that returns a deeply frozen array
  • Maybe we should allow unit: "meter-and-centimeter" and/or drop the units option
  • Should we still have a .unit getter, and if so, what should it return?
  • Should we still have a .value getter, and if so, what should it return?
  • Should we allow only sanctioned units to be a member of a sequence unit, so that we can enforce the correct order and implement .total(), or should we throw in .total() when the unit is not sanctioned? Also see Include conversion data for all units, not just Intl-sanctioned units? #117

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