Browse Source

add capitalize function

Getty Ritter 2 years ago
parent
commit
84502617e5
3 changed files with 20 additions and 0 deletions
  1. 11 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 8 0
      src/interp.rs

+ 11 - 0
Cargo.lock

@@ -500,6 +500,7 @@ dependencies = [
  "regex",
  "rustyline",
  "string-interner",
+ "titlecase",
  "vergen",
 ]
 
@@ -993,6 +994,16 @@ version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
 
+[[package]]
+name = "titlecase"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f565e410cfc24c2f2a89960b023ca192689d7f77d3f8d4f4af50c2d8affe1117"
+dependencies = [
+ "lazy_static",
+ "regex",
+]
+
 [[package]]
 name = "unicode-bidi"
 version = "0.3.7"

+ 1 - 0
Cargo.toml

@@ -27,6 +27,7 @@ rustyline = "*"
 ansi_term = "*"
 string-interner = "*"
 anyhow = "*"
+titlecase = "*"
 
 [build-dependencies]
 vergen = "*"

+ 8 - 0
src/interp.rs

@@ -156,6 +156,14 @@ const BUILTINS: &[BuiltinFunc] = &[
             Ok(Value::Lit(Literal::Str(s.as_str()?.to_uppercase())))
         },
     },
+    BuiltinFunc {
+        name: "capitalize",
+        callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
+            let s = state.eval(expr, env)?;
+            Ok(Value::Lit(Literal::Str(titlecase::titlecase(s.as_str()?))))
+
+        },
+    },
     BuiltinFunc {
         name: "to-lower",
         callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {