Add bindings for Intl.NumberFormat and Intl.PluralRules

This commit is contained in:
Thomas Eizinger 2018-08-14 13:46:06 +10:00
parent ade4a2c97a
commit 44f2ac0e9f
2 changed files with 119 additions and 0 deletions

View File

@ -3440,6 +3440,98 @@ pub mod Intl {
#[wasm_bindgen(static_method_of = DateTimeFormat, js_namespace = Intl, js_name = supportedLocalesOf)]
pub fn supported_locales_of(locales: &Array, options: &Object) -> Array;
}
// Intl.NumberFormat
#[wasm_bindgen]
extern "C" {
/// The Intl.NumberFormat object is a constructor for objects
/// that enable language sensitive number formatting.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
#[wasm_bindgen(js_namespace = Intl)]
#[derive(Clone, Debug)]
pub type NumberFormat;
/// The Intl.NumberFormat object is a constructor for objects
/// that enable language sensitive number formatting.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
#[wasm_bindgen(constructor, js_namespace = Intl)]
pub fn new(locales: &Array, options: &Object) -> NumberFormat;
/// The Intl.NumberFormat.prototype.format property returns a getter function that
/// formats a number according to the locale and formatting options of this
/// NumberFormat object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/format
#[wasm_bindgen(method, getter, js_class = "Intl.NumberFormat")]
pub fn format(this: &NumberFormat) -> Function;
/// The Intl.Numberformat.prototype.formatToParts() method allows locale-aware
/// formatting of strings produced by NumberTimeFormat formatters.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/formatToParts
#[wasm_bindgen(method, js_class = "Intl.NumberFormat", js_name = formatToParts)]
pub fn format_to_parts(this: &NumberFormat, number: f64) -> Array;
/// The Intl.NumberFormat.prototype.resolvedOptions() method returns a new
/// object with properties reflecting the locale and number formatting
/// options computed during initialization of this NumberFormat object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/resolvedOptions
#[wasm_bindgen(method, js_namespace = Intl, js_name = resolvedOptions)]
pub fn resolved_options(this: &NumberFormat) -> Object;
/// The Intl.NumberFormat.supportedLocalesOf() method returns an array
/// containing those of the provided locales that are supported in number
/// formatting without having to fall back to the runtime's default locale.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/supportedLocalesOf
#[wasm_bindgen(static_method_of = NumberFormat, js_namespace = Intl, js_name = supportedLocalesOf)]
pub fn supported_locales_of(locales: &Array, options: &Object) -> Array;
}
// Intl.PluralRules
#[wasm_bindgen]
extern "C" {
/// The Intl.PluralRules object is a constructor for objects
/// that enable plural sensitive formatting and plural language rules.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/PluralRules
#[wasm_bindgen(js_namespace = Intl)]
#[derive(Clone, Debug)]
pub type PluralRules;
/// The Intl.PluralRules object is a constructor for objects
/// that enable plural sensitive formatting and plural language rules.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/PluralRules
#[wasm_bindgen(constructor, js_namespace = Intl)]
pub fn new(locales: &Array, options: &Object) -> PluralRules;
/// The Intl.PluralRules.prototype.resolvedOptions() method returns a new
/// object with properties reflecting the locale and plural formatting
/// options computed during initialization of this PluralRules object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/PluralRules/resolvedOptions
#[wasm_bindgen(method, js_namespace = Intl, js_name = resolvedOptions)]
pub fn resolved_options(this: &PluralRules) -> Object;
/// The Intl.PluralRules.prototype.select method returns a String indicating
/// which plural rule to use for locale-aware formatting.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/PluralRules/select
#[wasm_bindgen(method, js_namespace = Intl)]
pub fn select(this: &PluralRules, number: f64) -> JsString;
/// The Intl.PluralRules.supportedLocalesOf() method returns an array
/// containing those of the provided locales that are supported in plural
/// formatting without having to fall back to the runtime's default locale.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/PluralRules/supportedLocalesOf
#[wasm_bindgen(static_method_of = PluralRules, js_namespace = Intl, js_name = supportedLocalesOf)]
pub fn supported_locales_of(locales: &Array, options: &Object) -> Array;
}
}
// Promise

View File

@ -51,3 +51,30 @@ fn date_time_format() {
let a = Intl::DateTimeFormat::supported_locales_of(&locales, &opts);
assert!(a.is_instance_of::<Array>());
}
#[wasm_bindgen_test]
fn number_format() {
let locales = Array::of1(&JsValue::from("en-US"));
let opts = Object::new();
let n = Intl::NumberFormat::new(&locales, &opts);
assert!(n.format().is_instance_of::<Function>());
assert!(n.format_to_parts(42.5).is_instance_of::<Array>());
assert!(n.resolved_options().is_instance_of::<Object>());
let a = Intl::NumberFormat::supported_locales_of(&locales, &opts);
assert!(a.is_instance_of::<Array>());
}
#[wasm_bindgen_test]
fn plural_rules() {
let locales = Array::of1(&JsValue::from("en-US"));
let opts = Object::new();
let r = Intl::PluralRules::new(&locales, &opts);
assert!(r.resolved_options().is_instance_of::<Object>());
assert_eq!(r.select(1_f64), "one");
let r = Intl::PluralRules::supported_locales_of(&locales, &opts);
assert!(r.is_instance_of::<Array>());
}