mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-16 10:10:52 +00:00
Add Sort to Array
This commit is contained in:
parent
de4eea119d
commit
a7bb555944
11
src/js.rs
11
src/js.rs
@ -151,6 +151,17 @@ extern {
|
||||
#[wasm_bindgen(method)]
|
||||
pub fn shift(this: &Array) -> JsValue;
|
||||
|
||||
/// The sort() method sorts the elements of an array in place and returns
|
||||
/// the array. The sort is not necessarily stable. The default sort
|
||||
/// order is according to string Unicode code points.
|
||||
///
|
||||
/// The time and space complexity of the sort cannot be guaranteed as it
|
||||
/// is implementation dependent.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
|
||||
#[wasm_bindgen(method)]
|
||||
pub fn sort(this: &Array) -> Array;
|
||||
|
||||
/// The toString() method returns a string representing the specified array and its elements.
|
||||
///
|
||||
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
|
||||
|
@ -40,6 +40,36 @@ fn index_of() {
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sort() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(proc_macro, wasm_custom_section)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::js;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn sort_array(this: &js::Array) -> js::Array {
|
||||
this.sort()
|
||||
}
|
||||
|
||||
"#)
|
||||
.file("test.ts", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let numbers = [3, 1, 6, 2];
|
||||
let sorted = wasm.sort_array(numbers);
|
||||
|
||||
assert.deepStrictEqual(sorted, [1, 2, 3, 6])
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn last_index_of() {
|
||||
project()
|
||||
|
Loading…
x
Reference in New Issue
Block a user