Skip to content

Commit 21cb50e

Browse files
authored
Merge pull request #412 from matiasinsaurralde/intl
Add bindings for `Intl`
2 parents 5a95cb3 + 8352b46 commit 21cb50e

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/js.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,3 +1781,17 @@ extern "C" {
17811781
#[wasm_bindgen(static_method_of = Symbol, getter, structural, js_name = toStringTag)]
17821782
pub fn to_string_tag() -> Symbol;
17831783
}
1784+
1785+
// Intl
1786+
#[wasm_bindgen]
1787+
extern "C" {
1788+
pub type Intl;
1789+
1790+
/// The `Intl.getCanonicalLocales()` method returns an array containing
1791+
/// the canonical locale names. Duplicates will be omitted and elements
1792+
/// will be validated as structurally valid language tags.
1793+
///
1794+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/getCanonicalLocales
1795+
#[wasm_bindgen(static_method_of = Intl, js_name = getCanonicalLocales)]
1796+
pub fn get_canonical_locales(s: &JsValue) -> Array;
1797+
}

tests/all/js_globals/Intl.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![allow(non_snake_case)]
2+
3+
use project;
4+
5+
#[test]
6+
fn get_canonical_locales() {
7+
project()
8+
.file("src/lib.rs", r#"
9+
#![feature(proc_macro, wasm_custom_section)]
10+
11+
extern crate wasm_bindgen;
12+
use wasm_bindgen::prelude::*;
13+
use wasm_bindgen::js;
14+
15+
#[wasm_bindgen]
16+
pub fn get_canonical_locales(v: &JsValue) -> js::Array {
17+
js::Intl::get_canonical_locales(v)
18+
}
19+
"#)
20+
.file("test.js", r#"
21+
import * as assert from "assert";
22+
import * as wasm from "./out";
23+
24+
export function test() {
25+
let locales = ["EN-US", "Fr"];
26+
let canonical_locales = wasm.get_canonical_locales(locales);
27+
assert.deepStrictEqual(canonical_locales, ["en-US", "fr"]);
28+
29+
let single_locale = wasm.get_canonical_locales("EN-US");
30+
assert.equal(single_locale, "en-US");
31+
}
32+
"#)
33+
.test()
34+
}

tests/all/js_globals/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod Date;
1010
mod Error;
1111
mod Function;
1212
mod Generator;
13+
mod Intl;
1314
mod JsString;
1415
mod Map;
1516
mod MapIterator;

0 commit comments

Comments
 (0)