-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A new optional feature 'wasm' has been introduced in the codebase. This enables `wasm-bindgen` support, making `Decimal` compatible with the `wasm_bindgen` attribute macro, and exposes `fromNumber()` and `toNumber()` methods to convert between `Decimal` and the primitive `number` type.
- Loading branch information
1 parent
c5d4afe
commit 587addd
Showing
5 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use num_traits::{FromPrimitive, ToPrimitive}; | ||
use wasm_bindgen::prelude::wasm_bindgen; | ||
|
||
use crate::Decimal; | ||
|
||
#[wasm_bindgen] | ||
impl Decimal { | ||
/// Returns a new `Decimal` object instance by converting a primitive number. | ||
#[wasm_bindgen(js_name = fromNumber)] | ||
#[must_use] | ||
pub fn from_number(value: f64) -> Option<Decimal> { | ||
Decimal::from_f64(value) | ||
} | ||
|
||
/// Returns the value of this `Decimal` converted to a primitive number. | ||
/// | ||
/// # Caution | ||
/// At the time of writing this implementation the conversion from `Decimal` to `f64` cannot | ||
/// fail. To prevent undefined behavior in case the underlying implementation changes `f64::NAN` | ||
/// is returned as a stable fallback value. | ||
#[wasm_bindgen(js_name = toNumber)] | ||
#[must_use] | ||
pub fn to_number(&self) -> f64 { | ||
self.to_f64().unwrap_or(f64::NAN) | ||
} | ||
} |