From ea74b8acbaeafa9cc74eac730e33a9a0c3d8656d Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 13 Aug 2018 09:22:36 +1000 Subject: [PATCH] Add bindings for Intl.DateTimeFormat --- crates/js-sys/src/lib.rs | 51 ++++++++++++++++++++++++++++++++ crates/js-sys/tests/wasm/Intl.rs | 15 ++++++++++ 2 files changed, 66 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index bf3c9704..7ce4d79d 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -3361,6 +3361,57 @@ pub mod Intl { #[wasm_bindgen(static_method_of = Collator, js_namespace = Intl, js_name = supportedLocalesOf)] pub fn supported_locales_of(locales: &Array, options: &Object) -> Array; } + + // Intl.DateTimeFormat + #[wasm_bindgen] + extern "C" { + /// The Intl.DateTimeFormat object is a constructor for objects + /// that enable language-sensitive date and time formatting. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat + #[wasm_bindgen(js_namespace = Intl)] + #[derive(Clone, Debug)] + pub type DateTimeFormat; + + /// The Intl.DateTimeFormat object is a constructor for objects + /// that enable language-sensitive date and time formatting. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat + #[wasm_bindgen(constructor, js_namespace = Intl)] + pub fn new(locales: &Array, options: &Object) -> DateTimeFormat; + + /// The Intl.DateTimeFormat.prototype.format property returns a getter function that + /// formats a date according to the locale and formatting options of this + /// Intl.DateTimeFormat object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/format + #[wasm_bindgen(method, getter, js_class = "Intl.DateTimeFormat")] + pub fn format(this: &DateTimeFormat) -> Function; + + /// The Intl.DateTimeFormat.prototype.formatToParts() method allows locale-aware + /// formatting of strings produced by DateTimeFormat formatters. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/formatToParts + #[wasm_bindgen(method, js_class = "Intl.DateTimeFormat", js_name = formatToParts)] + pub fn format_to_parts(this: &DateTimeFormat, date: &Date) -> Array; + + /// The Intl.DateTimeFormat.prototype.resolvedOptions() method returns a new + /// object with properties reflecting the locale and date and time formatting + /// options computed during initialization of this DateTimeFormat object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/resolvedOptions + #[wasm_bindgen(method, js_namespace = Intl, js_name = resolvedOptions)] + pub fn resolved_options(this: &DateTimeFormat) -> Object; + + /// The Intl.DateTimeFormat.supportedLocalesOf() method returns an array + /// containing those of the provided locales that are supported in date + /// and time formatting without having to fall back to the runtime's defaul + /// locale. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/supportedLocalesOf + #[wasm_bindgen(static_method_of = DateTimeFormat, js_namespace = Intl, js_name = supportedLocalesOf)] + pub fn supported_locales_of(locales: &Array, options: &Object) -> Array; + } } // Promise diff --git a/crates/js-sys/tests/wasm/Intl.rs b/crates/js-sys/tests/wasm/Intl.rs index e037fdfd..0cff1763 100644 --- a/crates/js-sys/tests/wasm/Intl.rs +++ b/crates/js-sys/tests/wasm/Intl.rs @@ -36,3 +36,18 @@ fn collator() { let a = Intl::Collator::supported_locales_of(&locales, &opts); assert!(a.is_instance_of::()); } + +#[wasm_bindgen_test] +fn date_time_format() { + let locales = Array::of1(&JsValue::from("en-US")); + let opts = Object::new(); + let epoch = Date::new(&JsValue::from(0)); + + let c = Intl::DateTimeFormat::new(&locales, &opts); + assert!(c.format().is_instance_of::()); + assert!(c.format_to_parts(&epoch).is_instance_of::()); + assert!(c.resolved_options().is_instance_of::()); + + let a = Intl::DateTimeFormat::supported_locales_of(&locales, &opts); + assert!(a.is_instance_of::()); +}