-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Temporal.Duration.toLocaleString()
does not return a proper human-readable string
#26795
Comments
@bartlomieju I think the toLocaleString() function is not supported in Deno because, as mentioned in the attached document, it requires Intl.DurationFormat to work properly. I attempted to address the issue using the attached code as a workaround, but I couldn't identify the exact source of the problem.
Could you provide a hint on where Temporal is used so that I know where to begin making modifications? |
This function will be implemented in V8 in the future. For now const { bind, call } = Function.prototype;
const uncurryThis = bind.bind(call);
const Duration = Temporal.Duration;
const proto = Object.getOwnPropertyDescriptors(Duration.prototype);
const assertDuration = uncurryThis(proto.toLocaleString.value);
const getYears = uncurryThis(proto.years.get);
const getMonths = uncurryThis(proto.months.get);
const getWeeks = uncurryThis(proto.weeks.get);
const getDays = uncurryThis(proto.days.get);
const getHours = uncurryThis(proto.hours.get);
const getMinutes = uncurryThis(proto.minutes.get);
const getSeconds = uncurryThis(proto.seconds.get);
const getMilliseconds = uncurryThis(proto.milliseconds.get);
const getMicroseconds = uncurryThis(proto.microseconds.get);
const getNanoseconds = uncurryThis(proto.nanoseconds.get);
const DurationFormat = Intl.DurationFormat;
const proto2 = Object.getOwnPropertyDescriptors(DurationFormat.prototype);
const formatDuration = uncurryThis(proto2.format.value);
Object.assign(Duration.prototype, {
toLocaleString(locales = undefined, options) {
assertDuration(this);
const durationFormat = new DurationFormat(locales, options);
const duration = {
years: getYears(this),
months: getMonths(this),
weeks: getWeeks(this),
days: getDays(this),
hours: getHours(this),
minutes: getMinutes(this),
seconds: getSeconds(this),
milliseconds: getMilliseconds(this),
microseconds: getMicroseconds(this),
nanoseconds: getNanoseconds(this),
};
return formatDuration(durationFormat, duration);
},
}); |
@0f-0b could you open a PR with your polyfill? |
I opened #27000. |
Version: Deno 2.0.5
Expected:
Actual:
See also:
https://tc39.es/proposal-temporal/docs/duration.html#toLocaleString
The text was updated successfully, but these errors were encountered: