Skip to content

Adds Number to_fixed and to_exponential function #305

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

Merged
merged 4 commits into from
Jun 24, 2018
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
14 changes: 14 additions & 0 deletions src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,20 @@ extern {
#[wasm_bindgen(catch, method, js_name = toPrecision)]
pub fn to_precision(this: &Number, precision: u8) -> Result<JsString, 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<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<JsString, JsValue>;

/// The toString() method returns a string representing the
/// specified Number object.
///
Expand Down
64 changes: 64 additions & 0 deletions tests/all/js_globals/Number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,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) -> js::JsString {
let result = this.to_fixed(digits);
let result = match result {
Ok(num) => num,
Err(_err) => "RangeError".into()
};
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) -> js::JsString {
let result = this.to_exponential(fraction_digits);
let result = match result {
Ok(num) => num,
Err(_err) => "RangeError".into()
};
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()
}