From 0f573989174c60a97415de93025fc9f256cbdd4a Mon Sep 17 00:00:00 2001 From: Sendil Kumar <sendilkumarn@live.com> Date: Sun, 24 Jun 2018 10:08:23 +0200 Subject: [PATCH 1/3] Add to_fixed and to_exponential function --- src/js.rs | 14 ++++++++ tests/all/js_globals/Number.rs | 64 ++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/src/js.rs b/src/js.rs index 01dca660..9b645957 100644 --- a/src/js.rs +++ b/src/js.rs @@ -246,6 +246,20 @@ extern { #[wasm_bindgen(catch, method, js_name = toPrecision)] pub fn to_precision(this: &Number, precision: u8) -> Result<String, JsValue>; + /// The toFixed() method returns a string representing the Number + /// object using fixed-point notation. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed + #[wasm_bindgen(catch, method, js_name = toFixed)] + pub fn to_fixed(this: &Number, digits: u8) -> Result<String, JsValue>; + + /// The toExponential() method returns a string representing the Number + /// object in exponential notation. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed + #[wasm_bindgen(catch, method, js_name = toExponential)] + pub fn to_exponential(this: &Number, fraction_digits: u8) -> Result<String, JsValue>; + /// The toString() method returns a string representing the /// specified Number object. /// diff --git a/tests/all/js_globals/Number.rs b/tests/all/js_globals/Number.rs index da308342..a6b39b27 100644 --- a/tests/all/js_globals/Number.rs +++ b/tests/all/js_globals/Number.rs @@ -125,3 +125,67 @@ fn value_of() { "#) .test() } + +#[test] +fn to_fixed() { + 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 to_fixed(this: &js::Number, digits: u8) -> String { + let result = this.to_fixed(digits); + let result = match result { + Ok(num) => num, + Err(_err) => "RangeError".to_string() + }; + result + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.to_fixed(123.456, 2), "123.46"); + assert.equal(wasm.to_fixed(10, 101), "RangeError"); + } + "#) + .test() +} + +#[test] +fn to_exponential() { + 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 to_exponential(this: &js::Number, fraction_digits: u8) -> String { + let result = this.to_exponential(fraction_digits); + let result = match result { + Ok(num) => num, + Err(_err) => "RangeError".to_string() + }; + result + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.to_exponential(123456, 2), "1.23e+5"); + assert.equal(wasm.to_exponential(10, 101), "RangeError"); + } + "#) + .test() +} From f5e050d08767e9ed0af2d754a62303bc5b0e210d Mon Sep 17 00:00:00 2001 From: Sendil Kumar <sendilkumarn@live.com> Date: Sun, 24 Jun 2018 10:10:52 +0200 Subject: [PATCH 2/3] fix url link --- src/js.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js.rs b/src/js.rs index 9b645957..b16427f6 100644 --- a/src/js.rs +++ b/src/js.rs @@ -256,7 +256,7 @@ extern { /// The toExponential() method returns a string representing the Number /// object in exponential notation. /// - /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential #[wasm_bindgen(catch, method, js_name = toExponential)] pub fn to_exponential(this: &Number, fraction_digits: u8) -> Result<String, JsValue>; From 32bc9f271c4c574847d29576d94ba60f3c2c4b54 Mon Sep 17 00:00:00 2001 From: Sendil Kumar <sendilkumarn@live.com> Date: Sun, 24 Jun 2018 20:48:37 +0200 Subject: [PATCH 3/3] rebase to handle JsString --- src/js.rs | 4 ++-- tests/all/js_globals/Number.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/js.rs b/src/js.rs index e20a1941..c5f0d51e 100644 --- a/src/js.rs +++ b/src/js.rs @@ -249,14 +249,14 @@ extern { /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed #[wasm_bindgen(catch, method, js_name = toFixed)] - pub fn to_fixed(this: &Number, digits: u8) -> Result<String, JsValue>; + pub fn to_fixed(this: &Number, digits: u8) -> Result<JsString, JsValue>; /// The toExponential() method returns a string representing the Number /// object in exponential notation. /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential #[wasm_bindgen(catch, method, js_name = toExponential)] - pub fn to_exponential(this: &Number, fraction_digits: u8) -> Result<String, JsValue>; + pub fn to_exponential(this: &Number, fraction_digits: u8) -> Result<JsString, JsValue>; /// The toString() method returns a string representing the /// specified Number object. diff --git a/tests/all/js_globals/Number.rs b/tests/all/js_globals/Number.rs index 8b55f356..362832e4 100644 --- a/tests/all/js_globals/Number.rs +++ b/tests/all/js_globals/Number.rs @@ -138,11 +138,11 @@ fn to_fixed() { use wasm_bindgen::js; #[wasm_bindgen] - pub fn to_fixed(this: &js::Number, digits: u8) -> String { + pub fn to_fixed(this: &js::Number, digits: u8) -> js::JsString { let result = this.to_fixed(digits); let result = match result { Ok(num) => num, - Err(_err) => "RangeError".to_string() + Err(_err) => "RangeError".into() }; result } @@ -170,11 +170,11 @@ fn to_exponential() { use wasm_bindgen::js; #[wasm_bindgen] - pub fn to_exponential(this: &js::Number, fraction_digits: u8) -> String { + pub fn to_exponential(this: &js::Number, fraction_digits: u8) -> js::JsString { let result = this.to_exponential(fraction_digits); let result = match result { Ok(num) => num, - Err(_err) => "RangeError".to_string() + Err(_err) => "RangeError".into() }; result }