diff --git a/finlib-wasm/js-local/bootstrap.js b/finlib-wasm/js-local/bootstrap.js new file mode 100644 index 0000000..c9c7c75 --- /dev/null +++ b/finlib-wasm/js-local/bootstrap.js @@ -0,0 +1,5 @@ +// A dependency graph that contains any wasm must all be imported +// asynchronously. This `bootstrap.js` file does the single async import, so +// that no one else needs to worry about it again. +import("./index.js") + .catch(e => console.error("Error importing `index.js`:", e)); diff --git a/finlib-wasm/js-local/index.html b/finlib-wasm/js-local/index.html new file mode 100644 index 0000000..5b91294 --- /dev/null +++ b/finlib-wasm/js-local/index.html @@ -0,0 +1,14 @@ +<!DOCTYPE html> +<html> + +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>finlib</title> +</head> +<body> + +<script src="./bootstrap.js"></script> +</body> + +</html> \ No newline at end of file diff --git a/finlib-wasm/js-local/index.js b/finlib-wasm/js-local/index.js new file mode 100644 index 0000000..11fd2bd --- /dev/null +++ b/finlib-wasm/js-local/index.js @@ -0,0 +1,4 @@ +import { ValueAtRisk } from "finlib"; + +console.log(ValueAtRisk.varcovar([1, 2, 3, 4], 0.1)); +console.log(ValueAtRisk.varcovar([1, 2, 3, 4], 0.05)); diff --git a/finlib-wasm/js-local/package.json b/finlib-wasm/js-local/package.json new file mode 100644 index 0000000..2422e4a --- /dev/null +++ b/finlib-wasm/js-local/package.json @@ -0,0 +1,23 @@ +{ + "name": "finlib-test", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "build": "webpack --config webpack.prod.js --env production", + "devbuild": "webpack --config webpack.dev.js", + "start": "webpack serve --config webpack.dev.js --progress" + }, + "dependencies": { + "finlib": "file:../pkg" + }, + "author": "", + "license": "ISC", + "description": "", + "devDependencies": { + "copy-webpack-plugin": "^11.0.0", + "webpack": "^5.88.2", + "webpack-cli": "^5.1.4", + "webpack-dev-server": "^4.15.1", + "webpack-merge": "^5.9.0" + } +} diff --git a/finlib-wasm/js-local/webpack.common.js b/finlib-wasm/js-local/webpack.common.js new file mode 100644 index 0000000..8a84006 --- /dev/null +++ b/finlib-wasm/js-local/webpack.common.js @@ -0,0 +1,22 @@ +const CopyWebpackPlugin = require("copy-webpack-plugin"); +const path = require('path'); + +module.exports = { + entry: "./bootstrap.js", + output: { + path: path.resolve(__dirname, "dist"), + filename: "bootstrap.js", + }, + experiments: { + asyncWebAssembly: true + }, + plugins: [ + new CopyWebpackPlugin({ + patterns: [ + { + from: path.resolve(__dirname, "index.html") + } + ] + }) + ], +}; diff --git a/finlib-wasm/js-local/webpack.dev.js b/finlib-wasm/js-local/webpack.dev.js new file mode 100644 index 0000000..958485e --- /dev/null +++ b/finlib-wasm/js-local/webpack.dev.js @@ -0,0 +1,8 @@ +const { merge } = require('webpack-merge'); +const common = require('./webpack.common.js'); + +module.exports = merge(common, { + mode: 'development', + devtool: 'inline-source-map', + watch: true +}); \ No newline at end of file diff --git a/finlib-wasm/js-local/webpack.prod.js b/finlib-wasm/js-local/webpack.prod.js new file mode 100644 index 0000000..1958c4f --- /dev/null +++ b/finlib-wasm/js-local/webpack.prod.js @@ -0,0 +1,7 @@ +const { merge } = require('webpack-merge'); +const common = require('./webpack.common.js'); + +module.exports = merge(common, { + mode: 'production', + devtool: 'source-map' +}); \ No newline at end of file diff --git a/finlib-wasm/js-repo/bootstrap.js b/finlib-wasm/js-repo/bootstrap.js new file mode 100644 index 0000000..c9c7c75 --- /dev/null +++ b/finlib-wasm/js-repo/bootstrap.js @@ -0,0 +1,5 @@ +// A dependency graph that contains any wasm must all be imported +// asynchronously. This `bootstrap.js` file does the single async import, so +// that no one else needs to worry about it again. +import("./index.js") + .catch(e => console.error("Error importing `index.js`:", e)); diff --git a/finlib-wasm/js-repo/index.html b/finlib-wasm/js-repo/index.html new file mode 100644 index 0000000..5b91294 --- /dev/null +++ b/finlib-wasm/js-repo/index.html @@ -0,0 +1,14 @@ +<!DOCTYPE html> +<html> + +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>finlib</title> +</head> +<body> + +<script src="./bootstrap.js"></script> +</body> + +</html> \ No newline at end of file diff --git a/finlib-wasm/js-repo/index.js b/finlib-wasm/js-repo/index.js new file mode 100644 index 0000000..11fd2bd --- /dev/null +++ b/finlib-wasm/js-repo/index.js @@ -0,0 +1,4 @@ +import { ValueAtRisk } from "finlib"; + +console.log(ValueAtRisk.varcovar([1, 2, 3, 4], 0.1)); +console.log(ValueAtRisk.varcovar([1, 2, 3, 4], 0.05)); diff --git a/finlib-wasm/js-repo/package.json b/finlib-wasm/js-repo/package.json new file mode 100644 index 0000000..eb575e7 --- /dev/null +++ b/finlib-wasm/js-repo/package.json @@ -0,0 +1,23 @@ +{ + "name": "finlib-test", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "build": "webpack --config webpack.prod.js --env production", + "devbuild": "webpack --config webpack.dev.js", + "start": "webpack serve --config webpack.dev.js --progress" + }, + "dependencies": { + "@sarsoo/finlib": "0.0.1" + }, + "author": "", + "license": "ISC", + "description": "", + "devDependencies": { + "copy-webpack-plugin": "^11.0.0", + "webpack": "^5.88.2", + "webpack-cli": "^5.1.4", + "webpack-dev-server": "^4.15.1", + "webpack-merge": "^5.9.0" + } +} diff --git a/finlib-wasm/js-repo/webpack.common.js b/finlib-wasm/js-repo/webpack.common.js new file mode 100644 index 0000000..8a84006 --- /dev/null +++ b/finlib-wasm/js-repo/webpack.common.js @@ -0,0 +1,22 @@ +const CopyWebpackPlugin = require("copy-webpack-plugin"); +const path = require('path'); + +module.exports = { + entry: "./bootstrap.js", + output: { + path: path.resolve(__dirname, "dist"), + filename: "bootstrap.js", + }, + experiments: { + asyncWebAssembly: true + }, + plugins: [ + new CopyWebpackPlugin({ + patterns: [ + { + from: path.resolve(__dirname, "index.html") + } + ] + }) + ], +}; diff --git a/finlib-wasm/js-repo/webpack.dev.js b/finlib-wasm/js-repo/webpack.dev.js new file mode 100644 index 0000000..958485e --- /dev/null +++ b/finlib-wasm/js-repo/webpack.dev.js @@ -0,0 +1,8 @@ +const { merge } = require('webpack-merge'); +const common = require('./webpack.common.js'); + +module.exports = merge(common, { + mode: 'development', + devtool: 'inline-source-map', + watch: true +}); \ No newline at end of file diff --git a/finlib-wasm/js-repo/webpack.prod.js b/finlib-wasm/js-repo/webpack.prod.js new file mode 100644 index 0000000..1958c4f --- /dev/null +++ b/finlib-wasm/js-repo/webpack.prod.js @@ -0,0 +1,7 @@ +const { merge } = require('webpack-merge'); +const common = require('./webpack.common.js'); + +module.exports = merge(common, { + mode: 'production', + devtool: 'source-map' +}); \ No newline at end of file diff --git a/finlib-wasm/js/index.js b/finlib-wasm/js/index.js deleted file mode 100644 index e69de29..0000000 diff --git a/finlib-wasm/js/package.json b/finlib-wasm/js/package.json deleted file mode 100644 index fbf570b..0000000 --- a/finlib-wasm/js/package.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "finlib", - "version": "1.0.0", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "dependencies": { - "draught": "file:../pkg" - }, - "author": "", - "license": "ISC", - "description": "" -} diff --git a/finlib-wasm/rename-pkg.sh b/finlib-wasm/rename-pkg.sh index 7979734..48734f9 100755 --- a/finlib-wasm/rename-pkg.sh +++ b/finlib-wasm/rename-pkg.sh @@ -1 +1 @@ -sed -i -e 's/"name": "finlib-wasm"/"name": "finlib"/g' pkg/package.json \ No newline at end of file +sed -i -e 's/"name": "finlib-wasm"/"name": "@sarsoo\/finlib"/g' pkg/package.json \ No newline at end of file diff --git a/finlib-wasm/src/lib.rs b/finlib-wasm/src/lib.rs index 1445120..41b63f6 100644 --- a/finlib-wasm/src/lib.rs +++ b/finlib-wasm/src/lib.rs @@ -1,11 +1,35 @@ use wasm_bindgen::prelude::wasm_bindgen; #[wasm_bindgen] -pub fn compound(principal: f64, rate: f64, time: f64, n: f64) -> f64 { - finlib::interest::compound(principal, rate, time, n) +pub struct Interest { } + +#[wasm_bindgen] +impl Interest { + pub fn compound(principal: f64, rate: f64, time: f64, n: f64) -> f64 { + finlib::interest::compound(principal, rate, time, n) + } } #[wasm_bindgen] -pub fn covariance(slice: Vec<f64>, slice_two: Vec<f64>) -> Option<f64> { - finlib::stats::covariance(&slice, &slice_two) -} \ No newline at end of file +pub struct ValueAtRisk { } + +#[wasm_bindgen] +impl ValueAtRisk { + pub fn historical(values: Vec<f64>, confidence: f64) -> f64 { + finlib::risk::var::historical::value_at_risk(&values, confidence) + } + + pub fn varcovar(values: Vec<f64>, confidence: f64) -> f64 { + finlib::risk::var::varcovar::value_at_risk(&values, confidence) + } +} + +#[wasm_bindgen] +pub struct Stats { } + +#[wasm_bindgen] +impl Stats { + pub fn covariance(slice: Vec<f64>, slice_two: Vec<f64>) -> Option<f64> { + finlib::stats::covariance(&slice, &slice_two) + } +}