Add Sort to Array

This commit is contained in:
Jonathan Sundqvist 2018-06-22 00:09:49 +02:00
parent de4eea119d
commit a7bb555944
2 changed files with 41 additions and 0 deletions

View File

@ -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

View File

@ -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()