feat: add wasm-bindgen build option and live example
authorPass Automated Testing Suite <Pass-Automated-Testing-Suite@zx2c4.com>
Wed, 3 Nov 2021 21:34:36 +0000 (22:34 +0100)
committerPass Automated Testing Suite <Pass-Automated-Testing-Suite@zx2c4.com>
Wed, 3 Nov 2021 21:34:36 +0000 (22:34 +0100)
Cargo.toml
README.md
src/constants.rs
src/lib.rs

index aa4afb0..3275a2f 100644 (file)
@@ -3,8 +3,12 @@ name = "poker-eval"
 version = "0.1.0"
 edition = "2021"
 
+[lib]
+crate-type = ["cdylib", "rlib"]
+
 [features]
 parallel = ["rayon"]
 
 [dependencies]
 rayon = { version = "1.5.1", optional = true }
+wasm-bindgen = "0.2.78"
index 3613140..fbeadbe 100644 (file)
--- a/README.md
+++ b/README.md
@@ -2,5 +2,7 @@
 
 Poker hand evaluator
 
-Based on Kevin L. Suffecool poker hand evaluator.
-<http://suffe.cool/poker/evaluator.html>
+This project can be built using wasm-pack.\
+An example is available [here](https://poker-eval.beaudet.xyz).
+
+Based on Kevin L. Suffecool [poker hand evaluator](http://suffe.cool/poker/evaluator.html)
index 3b54d48..419a1c5 100644 (file)
@@ -16,7 +16,7 @@ pub const VALUE_STR: [&str; 10] = [
     "Four of a Kind",
     "Full House",
     "Flush",
-    "Staight",
+    "Straight",
     "Three of a Kind",
     "Two Pair",
     "One Pair",
index d2e951d..99e31af 100644 (file)
@@ -15,6 +15,8 @@ use constants::{
     PERM7, PRIMES, STRAIGHT, STRAIGHT_FLUSH, THREE_OF_A_KIND, TWO_PAIR, UNIQUE5,
 };
 
+use wasm_bindgen::prelude::*;
+
 ///  Initializes a deck of 52 cards as an integer vector of lenght 52.
 ///
 /// The order in which the cards are returned is not random.
@@ -132,6 +134,7 @@ fn find_fast(mut u: u32) -> u32 {
 /// assert_eq!(eval_from_str("4s 4d 4h 4c 5d"), 140);
 /// assert_eq!(eval_from_str("Ts 9s 8s 7s 6s 7h 6d"), 5);
 /// ```
+#[wasm_bindgen]
 pub fn eval_from_str(s: &str) -> u32 {
     let cards = s.split(' ').map(|c| card_from_str(c)).collect::<Vec<_>>();
     match cards.len() {
@@ -187,6 +190,7 @@ pub fn eval_7hand(hand: &[u32; 7]) -> u32 {
 /// assert_eq!(eval, 1433);
 /// assert_eq!(hand_rank(eval), FLUSH);
 /// ```
+#[wasm_bindgen]
 pub fn hand_rank(i: u32) -> u32 {
     match i {
         6186..=7462 => HIGH_CARD,