Skip to content

Globalize-runtime cannot have two unit formatters with same options but different number formatters #704

@mattyork

Description

@mattyork

Globalize version: 1.2.3
Globalize compiler version: v0.2.4

Example code (might not compile, but hopefully illustrates the point):

// compilation time - note that the only difference in formatters is the number formatter options
const unitFormatOptions1 = { numberFormatter: globalizeInstance.numberFormatter({minimumIntegerDigits: 1}) };
const unitFormatOptions2 = { numberFormatter: globalizeInstance.numberFormatter({minimumIntegerDigits: 2}) };
compileFormatters([
  globalizeInstance.unitFormatter('hour', unitFormatOptions1),
  globalizeInstance.unitFormatter('hour', unitFormatOptions2)
]);


// ... now to the runtime
const x = globalizeInstance.formatUnit(3, 'hour', unitFormatOptions1); 
const y = globalizeInstance.formatUnit(3, 'hour', unitFormatOptions2);

Expected: x == "3" and y== "03"
Actual: x == "3" and y == "3"

The problem is that the unit formatter takes a number formatter (a function) as one of its options, but the runtime keys are generated by JSON.stringifying the options (see here), and JSON.stringify stringifies functions as empty string.

So, the runtime key is resolved to the same thing in both formatters, even though they have different number formatters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions