Skip to content

Commit

Permalink
LibJS+LibUnicode: Add "microsecond" and "nanosecond" as sanctioned units
Browse files Browse the repository at this point in the history
This is a normative change in the ECMA-402 spec. See:
tc39/ecma402#708
  • Loading branch information
trflynn89 committed Aug 31, 2022
1 parent 5e36a55 commit 8a2a998
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,7 @@ static ErrorOr<void> parse_units(String locale_units_path, UnicodeLocaleData& lo
// LibUnicode generally tries to avoid being directly dependent on ECMA-402, but this rather significantly reduces the amount
// of data generated here, and ECMA-402 is currently the only consumer of this data.
constexpr auto sanctioned_units = JS::Intl::sanctioned_single_unit_identifiers();
if (find(sanctioned_units.begin(), sanctioned_units.end(), unit_name) != sanctioned_units.end())
return true;
static constexpr auto extra_sanctioned_units = JS::Intl::extra_sanctioned_single_unit_identifiers();
return find(extra_sanctioned_units.begin(), extra_sanctioned_units.end(), unit_name) != extra_sanctioned_units.end();
return find(sanctioned_units.begin(), sanctioned_units.end(), unit_name) != sanctioned_units.end();
};

auto parse_units_object = [&](auto const& units_object, Unicode::Style style) {
Expand Down
8 changes: 2 additions & 6 deletions Userland/Libraries/LibJS/Runtime/Intl/SingleUnitIdentifiers.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ constexpr auto sanctioned_single_unit_identifiers()
"megabit"sv,
"megabyte"sv,
"meter"sv,
"microsecond"sv,
"mile"sv,
"mile-scandinavian"sv,
"milliliter"sv,
"millimeter"sv,
"millisecond"sv,
"minute"sv,
"month"sv,
"nanosecond"sv,
"ounce"sv,
"percent"sv,
"petabyte"sv,
Expand All @@ -61,10 +63,4 @@ constexpr auto sanctioned_single_unit_identifiers()
};
}

// Additional single units used in ECMAScript required by the Intl.DurationFormat proposal
constexpr auto extra_sanctioned_single_unit_identifiers()
{
return AK::Array { "microsecond"sv, "nanosecond"sv };
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,15 @@ describe("style=unit", () => {
expect(en2.format(1.2)).toBe("1.2 kilometers per hour");
expect(en2.format(123)).toBe("123 kilometers per hour");

const en3 = new Intl.NumberFormat("en", {
style: "unit",
unit: "nanosecond",
unitDisplay: "long",
});
expect(en3.format(1)).toBe("1 nanosecond");
expect(en3.format(1.2)).toBe("1.2 nanoseconds");
expect(en3.format(123)).toBe("123 nanoseconds");

const ar = new Intl.NumberFormat("ar", {
style: "unit",
unit: "foot",
Expand Down Expand Up @@ -1556,6 +1565,15 @@ describe("style=unit", () => {
expect(en2.format(1.2)).toBe("1.2 km/h");
expect(en2.format(123)).toBe("123 km/h");

const en3 = new Intl.NumberFormat("en", {
style: "unit",
unit: "nanosecond",
unitDisplay: "short",
});
expect(en3.format(1)).toBe("1 ns");
expect(en3.format(1.2)).toBe("1.2 ns");
expect(en3.format(123)).toBe("123 ns");

const ar = new Intl.NumberFormat("ar", {
style: "unit",
unit: "foot",
Expand Down Expand Up @@ -1594,6 +1612,15 @@ describe("style=unit", () => {
expect(en2.format(1.2)).toBe("1.2km/h");
expect(en2.format(123)).toBe("123km/h");

const en3 = new Intl.NumberFormat("en", {
style: "unit",
unit: "nanosecond",
unitDisplay: "narrow",
});
expect(en3.format(1)).toBe("1ns");
expect(en3.format(1.2)).toBe("1.2ns");
expect(en3.format(123)).toBe("123ns");

const ar = new Intl.NumberFormat("ar", {
style: "unit",
unit: "foot",
Expand Down

0 comments on commit 8a2a998

Please sign in to comment.