-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be51ec4
commit 011e7c0
Showing
18 changed files
with
2,720 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,20 @@ | ||
[package] | ||
name = "cpc-wasm" | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
cpc = { path = "../" } | ||
decimal = { version = "2.1", default-features = false, features = [ | ||
"serde", | ||
"ord_subset", | ||
] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde-wasm-bindgen = "0.6" | ||
wasm-bindgen = "0.2" | ||
|
||
[package.metadata.wasm-pack.profile.release] | ||
wasm-opt = ["-Oz"] |
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,32 @@ | ||
use decimal::d128; | ||
use serde::Serialize; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[derive(Serialize)] | ||
pub struct NumberWasm { | ||
pub value: String, | ||
pub quantity: String, | ||
pub unit: String, | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn eval_wasm(input: &str) -> Result<JsValue, String> { | ||
let number = cpc::eval(input, true, false)?; | ||
// 0.2/0.01 results in 2E+1, but if we add zero it becomes 20 | ||
let quantity = number.value + d128!(0); | ||
let word = match quantity == d128!(1) { | ||
true => number.unit.singular(), | ||
false => number.unit.plural(), | ||
}; | ||
let output_text = match word { | ||
"" => format!("{quantity}"), | ||
_ => format!("{quantity} {word}"), | ||
}; | ||
let number_wasm = NumberWasm { | ||
value: output_text, | ||
quantity: quantity.to_string(), | ||
unit: word.into(), | ||
}; | ||
let js_value = serde_wasm_bindgen::to_value(&number_wasm).unwrap(); | ||
Ok(js_value) | ||
} |
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,10 @@ | ||
.DS_Store | ||
node_modules | ||
/build | ||
/.svelte-kit | ||
/package | ||
.env | ||
.env.* | ||
!.env.example | ||
vite.config.js.timestamp-* | ||
vite.config.ts.timestamp-* |
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 @@ | ||
engine-strict=true |
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,38 @@ | ||
# create-svelte | ||
|
||
Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/main/packages/create-svelte). | ||
|
||
## Creating a project | ||
|
||
If you're seeing this, you've probably already done this step. Congrats! | ||
|
||
```bash | ||
# create a new project in the current directory | ||
npm create svelte@latest | ||
|
||
# create a new project in my-app | ||
npm create svelte@latest my-app | ||
``` | ||
|
||
## Developing | ||
|
||
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server: | ||
|
||
```bash | ||
npm run dev | ||
|
||
# or start the server and open the app in a new browser tab | ||
npm run dev -- --open | ||
``` | ||
|
||
## Building | ||
|
||
To create a production version of your app: | ||
|
||
```bash | ||
npm run build | ||
``` | ||
|
||
You can preview the production build with `npm run preview`. | ||
|
||
> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment. |
Oops, something went wrong.