Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Array#flat and Array#flatMap to js-sys (fixes #1454) #1573

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,23 @@ extern "C" {
#[wasm_bindgen(method, js_name = findIndex)]
pub fn find_index(this: &Array, predicate: &mut dyn FnMut(JsValue, u32, Array) -> bool) -> i32;

/// The flat() method creates a new array with all sub-array elements concatenated into it
/// recursively up to the specified depth.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat)
#[wasm_bindgen(method)]
pub fn flat(this: &Array, depth: i32) -> Array;

/// The flatMap() method first maps each element using a mapping function, then flattens
/// the result into a new array.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap)
#[wasm_bindgen(method, js_name = flatMap)]
pub fn flat_map(
this: &Array,
callback: &mut dyn FnMut(JsValue, u32, Array) -> Vec<JsValue>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure of the best type to use for the callback here. In JS, it's possible for the flatMap callback to return a non-array value v, which has the same effect as returning the one-element array [v]. But it seems cleaner to be more strict in the Rust type.

I think Vec<JsValue> is the most appropriate return value here (as opposed to e.g. a slice), but it might be worth double-checking this; I don't have a lot of experience with lifetimes.

) -> Array;

/// The `forEach()` method executes a provided function once for each array element.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
Expand Down
38 changes: 38 additions & 0 deletions crates/js-sys/tests/wasm/Array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,44 @@ fn filter() {
);
}

#[wasm_bindgen_test]
fn flat() {
let array = js_array![
js_array!["a", "b", "c"],
"d",
js_array!["e", js_array!["f", "g"]]
];

assert_eq!(
to_rust(&array.flat(1).slice(0, 5)),
vec!["a", "b", "c", "d", "e"]
);

assert_eq!(array.flat(1).length(), 6);

assert_eq!(
to_rust(&array.flat(2)),
vec!["a", "b", "c", "d", "e", "f", "g"]
);
}

#[wasm_bindgen_test]
fn flat_map() {
let array = js_array![1, 2, 3, 1];

assert_eq!(
to_rust(
&array.flat_map(&mut |val, _, _| match val.as_f64().map(|v| v as i32) {
Some(1) => vec![JsString::from("x").into(), JsString::from("x").into()],
Some(2) => vec![],
Some(3) => vec![JsString::from("z").into()],
_ => panic!("Unexpected conversion"),
})
),
vec!["x", "x", "z", "x", "x"]
);
}

#[wasm_bindgen_test]
fn index_of() {
let chars = js_array!["a", "c", "x", "n"];
Expand Down