mirror of
https://github.com/denoland/deno.git
synced 2025-01-21 13:00:36 -05:00
fix(unstable/temporal): respect locale in Duration.prototype.toLocaleString
(#27000)
Adds a temporary polyfill for `Duration.prototype.toLocaleString()` that will be removed once native support in V8 lands.
This commit is contained in:
parent
e8d731c05f
commit
25aed5071f
3 changed files with 59 additions and 0 deletions
|
@ -37,6 +37,7 @@ const {
|
||||||
ObjectHasOwn,
|
ObjectHasOwn,
|
||||||
ObjectKeys,
|
ObjectKeys,
|
||||||
ObjectGetOwnPropertyDescriptor,
|
ObjectGetOwnPropertyDescriptor,
|
||||||
|
ObjectGetOwnPropertyDescriptors,
|
||||||
ObjectPrototypeIsPrototypeOf,
|
ObjectPrototypeIsPrototypeOf,
|
||||||
ObjectSetPrototypeOf,
|
ObjectSetPrototypeOf,
|
||||||
PromisePrototypeThen,
|
PromisePrototypeThen,
|
||||||
|
@ -45,6 +46,7 @@ const {
|
||||||
Symbol,
|
Symbol,
|
||||||
SymbolIterator,
|
SymbolIterator,
|
||||||
TypeError,
|
TypeError,
|
||||||
|
uncurryThis,
|
||||||
} = primordials;
|
} = primordials;
|
||||||
const {
|
const {
|
||||||
isNativeError,
|
isNativeError,
|
||||||
|
@ -459,6 +461,51 @@ function exposeUnstableFeaturesForWindowOrWorkerGlobalScope(unstableFeatures) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function shimTemporalDurationToLocaleString() {
|
||||||
|
const DurationFormat = Intl.DurationFormat;
|
||||||
|
if (!DurationFormat) {
|
||||||
|
// Intl.DurationFormat can be disabled with --v8-flags=--no-harmony-intl-duration-format
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const DurationFormatPrototype = DurationFormat.prototype;
|
||||||
|
const formatDuration = uncurryThis(DurationFormatPrototype.format);
|
||||||
|
|
||||||
|
const Duration = Temporal.Duration;
|
||||||
|
const DurationPrototype = Duration.prototype;
|
||||||
|
const desc = ObjectGetOwnPropertyDescriptors(DurationPrototype);
|
||||||
|
const assertDuration = uncurryThis(desc.toLocaleString.value);
|
||||||
|
const getYears = uncurryThis(desc.years.get);
|
||||||
|
const getMonths = uncurryThis(desc.months.get);
|
||||||
|
const getWeeks = uncurryThis(desc.weeks.get);
|
||||||
|
const getDays = uncurryThis(desc.days.get);
|
||||||
|
const getHours = uncurryThis(desc.hours.get);
|
||||||
|
const getMinutes = uncurryThis(desc.minutes.get);
|
||||||
|
const getSeconds = uncurryThis(desc.seconds.get);
|
||||||
|
const getMilliseconds = uncurryThis(desc.milliseconds.get);
|
||||||
|
const getMicroseconds = uncurryThis(desc.microseconds.get);
|
||||||
|
const getNanoseconds = uncurryThis(desc.nanoseconds.get);
|
||||||
|
|
||||||
|
ObjectAssign(DurationPrototype, {
|
||||||
|
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);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE(bartlomieju): remove all the ops that have already been imported using
|
// NOTE(bartlomieju): remove all the ops that have already been imported using
|
||||||
// "virtual op module" (`ext:core/ops`).
|
// "virtual op module" (`ext:core/ops`).
|
||||||
const NOT_IMPORTED_OPS = [
|
const NOT_IMPORTED_OPS = [
|
||||||
|
@ -821,6 +868,12 @@ function bootstrapMainRuntime(runtimeOptions, warmup = false) {
|
||||||
});
|
});
|
||||||
delete globalThis.Temporal.Now.timeZone;
|
delete globalThis.Temporal.Now.timeZone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// deno-lint-ignore prefer-primordials
|
||||||
|
if (new Temporal.Duration().toLocaleString("en-US") !== "PT0S") {
|
||||||
|
throw "V8 supports Temporal.Duration.prototype.toLocaleString now, no need to shim it";
|
||||||
|
}
|
||||||
|
shimTemporalDurationToLocaleString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup `Deno` global - we're actually overriding already existing global
|
// Setup `Deno` global - we're actually overriding already existing global
|
||||||
|
@ -1024,6 +1077,8 @@ function bootstrapWorkerRuntime(
|
||||||
});
|
});
|
||||||
delete globalThis.Temporal.Now.timeZone;
|
delete globalThis.Temporal.Now.timeZone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shimTemporalDurationToLocaleString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup `Deno` global - we're actually overriding already existing global
|
// Setup `Deno` global - we're actually overriding already existing global
|
||||||
|
|
|
@ -5,3 +5,4 @@ iso8601
|
||||||
UTC
|
UTC
|
||||||
undefined
|
undefined
|
||||||
undefined
|
undefined
|
||||||
|
1 day, 6 hr, 30 min
|
||||||
|
|
|
@ -9,3 +9,6 @@ console.log(zoned.timeZoneId);
|
||||||
console.log(zoned.calendar);
|
console.log(zoned.calendar);
|
||||||
// @ts-expect-error: undefined check
|
// @ts-expect-error: undefined check
|
||||||
console.log(zoned.timeZone);
|
console.log(zoned.timeZone);
|
||||||
|
|
||||||
|
const duration = Temporal.Duration.from("P1DT6H30M");
|
||||||
|
console.log(duration.toLocaleString("en-US"));
|
||||||
|
|
Loading…
Add table
Reference in a new issue