diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-02-24 19:56:01 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-02-24 19:56:01 +0100 |
| commit | efde5cac88078f10485f715be66a27efba2f23d8 (patch) | |
| tree | 6d8d8d778e88b317f0e85a8815f887757a445e36 /src/library/utility.rs | |
| parent | ecd2bca606c0533ec6426b03fc216df256d43c3f (diff) | |
Lower and upper on templates
Diffstat (limited to 'src/library/utility.rs')
| -rw-r--r-- | src/library/utility.rs | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/src/library/utility.rs b/src/library/utility.rs index 051dd885..ceae20bf 100644 --- a/src/library/utility.rs +++ b/src/library/utility.rs @@ -4,6 +4,7 @@ use std::cmp::Ordering; use std::str::FromStr; use super::prelude::*; +use super::{Case, TextNode}; use crate::eval::Array; /// Ensure that a condition is fulfilled. @@ -253,12 +254,22 @@ pub fn range(_: &mut Context, args: &mut Args) -> TypResult<Value> { /// Convert a string to lowercase. pub fn lower(_: &mut Context, args: &mut Args) -> TypResult<Value> { - Ok(args.expect::<EcoString>("string")?.to_lowercase().into()) + case(Case::Lower, args) } /// Convert a string to uppercase. pub fn upper(_: &mut Context, args: &mut Args) -> TypResult<Value> { - Ok(args.expect::<EcoString>("string")?.to_uppercase().into()) + case(Case::Upper, args) +} + +/// Change the case of a string or template. +fn case(case: Case, args: &mut Args) -> TypResult<Value> { + let Spanned { v, span } = args.expect("string or template")?; + Ok(match v { + Value::Str(v) => Value::Str(case.apply(&v).into()), + Value::Template(v) => Value::Template(v.styled(TextNode::CASE, Some(case))), + v => bail!(span, "expected string or template, found {}", v.type_name()), + }) } /// The length of a string, an array or a dictionary. |
