From f0444d1614082deed18c651b91378d5d695c9a4c Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Fri, 10 Aug 2018 11:20:06 -0700 Subject: [PATCH] js-sys: Add bindings for `Intl.Collator` --- crates/js-sys/src/lib.rs | 44 ++++++++++++++++++++++++++++++++ crates/js-sys/tests/wasm/Intl.rs | 15 ++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 1c4639d8..6cb8c9d4 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -3142,6 +3142,50 @@ pub mod Intl { #[wasm_bindgen(js_name = getCanonicalLocales, js_namespace = Intl)] pub fn get_canonical_locales(s: &JsValue) -> Array; } + + // Intl.Collator + #[wasm_bindgen] + extern "C" { + /// The Intl.Collator object is a constructor for collators, objects + /// that enable language sensitive string comparison. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator + #[wasm_bindgen(js_namespace = Intl)] + #[derive(Clone, Debug)] + pub type Collator; + + /// The Intl.Collator object is a constructor for collators, objects + /// that enable language sensitive string comparison. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator + #[wasm_bindgen(constructor, js_namespace = Intl)] + pub fn new(locales: &Array, options: &Object) -> Collator; + + /// The Intl.Collator.prototype.compare property returns a function that + /// compares two strings according to the sort order of this Collator + /// object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator/compare + #[wasm_bindgen(method, getter, js_class = "Intl.Collator")] + pub fn compare(this: &Collator) -> Function; + + /// The Intl.Collator.prototype.resolvedOptions() method returns a new + /// object with properties reflecting the locale and collation options + /// computed during initialization of this Collator object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator/resolvedOptions + #[wasm_bindgen(method, js_namespace = Intl, js_name = resolvedOptions)] + pub fn resolved_options(this: &Collator) -> Object; + + /// The Intl.Collator.supportedLocalesOf() method returns an array + /// containing those of the provided locales that are supported in + /// collation without having to fall back to the runtime's default + /// locale. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator/supportedLocalesOf + #[wasm_bindgen(static_method_of = Collator, 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 a739656b..e037fdfd 100644 --- a/crates/js-sys/tests/wasm/Intl.rs +++ b/crates/js-sys/tests/wasm/Intl.rs @@ -1,4 +1,4 @@ -use wasm_bindgen::JsValue; +use wasm_bindgen::{JsCast, JsValue}; use wasm_bindgen_test::*; use js_sys::*; @@ -23,3 +23,16 @@ fn get_canonical_locales() { assert_eq!(l, "en-US"); }); } + +#[wasm_bindgen_test] +fn collator() { + let locales = Array::of1(&JsValue::from("en-US")); + let opts = Object::new(); + + let c = Intl::Collator::new(&locales, &opts); + assert!(c.compare().is_instance_of::()); + assert!(c.resolved_options().is_instance_of::()); + + let a = Intl::Collator::supported_locales_of(&locales, &opts); + assert!(a.is_instance_of::()); +}