Add bindings for Intl.DateTimeFormat

This commit is contained in:
Thomas Eizinger 2018-08-13 09:22:36 +10:00
parent 539e987cdb
commit ea74b8acba
2 changed files with 66 additions and 0 deletions

View File

@ -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

View File

@ -36,3 +36,18 @@ fn collator() {
let a = Intl::Collator::supported_locales_of(&locales, &opts);
assert!(a.is_instance_of::<Array>());
}
#[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::<Function>());
assert!(c.format_to_parts(&epoch).is_instance_of::<Array>());
assert!(c.resolved_options().is_instance_of::<Object>());
let a = Intl::DateTimeFormat::supported_locales_of(&locales, &opts);
assert!(a.is_instance_of::<Array>());
}