Skip to content

Commit

Permalink
Merge pull request #14 from alerque/wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque authored Jul 15, 2024
2 parents 50f9ae2 + eb085f0 commit 11aa8c5
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 76 deletions.
202 changes: 128 additions & 74 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ lua52 = ["luamodule", "mlua/lua52"]
lua51 = ["luamodule", "mlua/lua51"]
luajit = ["luamodule", "mlua/luajit"]
pythonmodule = ["pyo3"]
wasm = ["wasm-bindgen"]

[profile.release]
lto = true
Expand All @@ -64,9 +65,13 @@ unicode_titlecase = "2.3"
features = [ "extension-module" ]

[dependencies.titlecase]
version = "3.2"
version = "3.3"
features = [ "perf" ]

[dependencies.wasm-bindgen]
version = "0.2"
optional = true

[build-dependencies]
strum = "0.26"
strum_macros = "0.26"
Expand Down
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ docdir = $(datarootdir)/doc/$(TRANSFORMED_PACKAGE_NAME)
licensedir = $(datarootdir)/licenses/$(TRANSFORMED_PACKAGE_NAME)

bin_PROGRAMS = decasify
decasify_SOURCES = src/bin/decasify.rs src/cli.rs src/lib.rs src/lua.rs src/python.rs src/types.rs
decasify_SOURCES = src/bin/decasify.rs src/cli.rs src/lib.rs src/lua.rs src/python.rs src/types.rs src/wasm.rs
EXTRA_decasify_SOURCES = tests/cli.rs
EXTRA_DIST = pyproject.toml spec/decasify_spec.lua tests/test_all.py
dist_doc_DATA = README.md CHANGELOG.md
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,22 @@ input = "title with a twist: a colon"
output = titlecase(input, InputLocale.EN, StyleGuide.DaringFireball)
print(output)
```

## Use as JavaScript (WASM) Module

Depend on the WASM based JavaScript module in your project with `npm add decasify`:

Then import and use the provided functions and classes:

```javascript
import { titlecase, uppercase, lowercase, InputLocale, StyleGuide } from 'decasify';

var input = "ILIK SU VE İTEN RÜZGARLAR"
var output = titlecase(input, InputLocale.TR)
console.log(output)

var input = "title with a twist: a colon"
var output = titlecase(input, InputLocale.EN, StyleGuide.DaringFireball)
console.log(output)
```

3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ AM_COND_IF([DEVELOPER_MODE], [
QUE_PROGVAR([maturin])
QUE_PROGVAR([pytest])
QUE_PROGVAR([uv])
# WASM build and testing dependencies
QUE_PROGVAR([npm])
QUE_PROGVAR([wasmpack], [wasm-pack])
# Release tooling
QUE_PROGVAR([gitcliff], [git-cliff])
])
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub mod lua;
#[cfg(feature = "pythonmodule")]
pub mod python;

#[cfg(feature = "wasm")]
pub mod wasm;

/// Convert a string to title case following typesetting conventions for a target locale
pub fn to_titlecase(string: &str, locale: InputLocale, style: Option<StyleGuide>) -> String {
let words: Vec<&str> = string.split_whitespace().collect();
Expand Down
5 changes: 5 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use strum_macros::{Display, VariantNames};
#[cfg(feature = "pythonmodule")]
use pyo3::prelude::*;

#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

pub type Result<T> = result::Result<T, Box<dyn error::Error>>;

#[derive(Debug)]
Expand All @@ -20,6 +23,7 @@ impl error::Error for DecasifyError {}
/// Locale selector to change language support rules of case functions.
#[derive(Default, Display, VariantNames, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "pythonmodule", pyclass(eq, eq_int))]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[strum(serialize_all = "lowercase")]
pub enum InputLocale {
#[default]
Expand All @@ -41,6 +45,7 @@ pub enum TargetCase {
/// Style guide selector to change grammar and context rules used for title casing.
#[derive(Default, Display, VariantNames, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "pythonmodule", pyclass(eq, eq_int))]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[strum(serialize_all = "lowercase")]
pub enum StyleGuide {
#[strum(serialize = "ap")]
Expand Down
24 changes: 24 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::*;
use std::result::Result;
use wasm_bindgen::prelude::*;

pub use crate::types::{InputLocale, StyleGuide};

#[wasm_bindgen]
pub fn titlecase(
input: &str,
locale: InputLocale,
style: Option<StyleGuide>,
) -> Result<String, JsError> {
Ok(to_titlecase(input, locale, style))
}

#[wasm_bindgen]
pub fn lowercase(input: &str, locale: InputLocale) -> Result<String, JsError> {
Ok(to_lowercase(input, locale))
}

#[wasm_bindgen]
pub fn uppercase(input: &str, locale: InputLocale) -> Result<String, JsError> {
Ok(to_uppercase(input, locale))
}
9 changes: 9 additions & 0 deletions tests/wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { titlecase, uppercase, lowercase, InputLocale, StyleGuide } from '../pkg';

var input = "ILIK SU VE İTEN RÜZGARLAR"
var output = titlecase(input, InputLocale.TR)
console.log(output)

var input = "title with a twist: a colon"
var output = titlecase(input, InputLocale.EN, StyleGuide.DaringFireball)
console.log(output)

0 comments on commit 11aa8c5

Please sign in to comment.