1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use handlebars::HelperDef;
use handlebars::{handlebars_helper, Handlebars};
use inflector::Inflector;

#[macro_export]
macro_rules! handlebars_register_inflector {
    ($engine:ident, $fct_name:ident) => {
        {
            handlebars_helper!($fct_name: |v: str| v.$fct_name());
            $engine.register_helper(stringify!($fct_name), Box::new($fct_name))
        }
    }
}

pub fn register(handlebars: &mut Handlebars) -> Vec<Box<dyn HelperDef + 'static>> {
    vec![
        {
            handlebars_helper!(to_lower_case: |v: str| v.to_lowercase());
            handlebars.register_helper("to_lower_case", Box::new(to_lower_case))
        },
        {
            handlebars_helper!(to_upper_case: |v: str| v.to_uppercase());
            handlebars.register_helper("to_upper_case", Box::new(to_upper_case))
        },
        {
            handlebars_helper!(trim: |v: str| v.trim());
            handlebars.register_helper("trim", Box::new(trim))
        },
        {
            handlebars_helper!(trim_start: |v: str| v.trim_start());
            handlebars.register_helper("trim_start", Box::new(trim_start))
        },
        {
            handlebars_helper!(trim_end: |v: str| v.trim_end());
            handlebars.register_helper("trim_end", Box::new(trim_end))
        },
        {
            handlebars_helper!(replace: |v: str, from: str, to: str| v.replace(from, to));
            handlebars.register_helper("replace", Box::new(replace))
        },
        handlebars_register_inflector!(handlebars, to_camel_case),
        handlebars_register_inflector!(handlebars, to_pascal_case),
        handlebars_register_inflector!(handlebars, to_snake_case),
        handlebars_register_inflector!(handlebars, to_screaming_snake_case),
        handlebars_register_inflector!(handlebars, to_kebab_case),
        handlebars_register_inflector!(handlebars, to_train_case),
        handlebars_register_inflector!(handlebars, to_sentence_case),
        handlebars_register_inflector!(handlebars, to_title_case),
        handlebars_register_inflector!(handlebars, to_class_case),
        handlebars_register_inflector!(handlebars, to_table_case),
        handlebars_register_inflector!(handlebars, to_plural),
        handlebars_register_inflector!(handlebars, to_singular),
    ]
    .into_iter()
    .flatten()
    .collect()
}

#[cfg(test)]
mod tests {
    use crate::tests::assert_helpers;
    use crate::tests::assert_renders;
    use std::error::Error;

    #[test]
    fn test_register_string_helpers() -> Result<(), Box<dyn Error>> {
        assert_helpers(
            "Hello foo-bars",
            vec![
                ("to_lower_case", "hello foo-bars"),
                ("to_upper_case", "HELLO FOO-BARS"),
                ("to_camel_case", "helloFooBars"),
                ("to_pascal_case", "HelloFooBars"),
                ("to_snake_case", "hello_foo_bars"),
                ("to_screaming_snake_case", "HELLO_FOO_BARS"),
                ("to_kebab_case", "hello-foo-bars"),
                ("to_train_case", "Hello-Foo-Bars"),
                ("to_sentence_case", "Hello foo bars"),
                ("to_title_case", "Hello Foo Bars"),
                ("to_class_case", "HelloFooBar"),
                ("to_table_case", "hello_foo_bars"),
                ("to_plural", "bars"),
                ("to_singular", "bar"),
            ],
        )?;
        Ok(())
    }

    #[test]
    fn test_helper_trim() -> Result<(), Box<dyn Error>> {
        assert_renders(vec![
            (r##"{{ trim "foo" }}"##, r##"foo"##),
            (r##"{{ trim "  foo" }}"##, r##"foo"##),
            (r##"{{ trim "foo  " }}"##, r##"foo"##),
            (r##"{{ trim " foo " }}"##, r##"foo"##),
        ])?;
        Ok(())
    }

    #[test]
    fn test_helper_trim_start() -> Result<(), Box<dyn Error>> {
        assert_renders(vec![
            (r##"{{ trim_start "foo" }}"##, r##"foo"##),
            (r##"{{ trim_start "  foo" }}"##, r##"foo"##),
            (r##"{{ trim_start "foo  " }}"##, r##"foo  "##),
            (r##"{{ trim_start " foo " }}"##, r##"foo "##),
        ])?;
        Ok(())
    }

    #[test]
    fn test_helper_trim_end() -> Result<(), Box<dyn Error>> {
        assert_renders(vec![
            (r##"{{ trim_end "foo" }}"##, r##"foo"##),
            (r##"{{ trim_end "  foo" }}"##, r##"  foo"##),
            (r##"{{ trim_end "foo  " }}"##, r##"foo"##),
            (r##"{{ trim_end " foo " }}"##, r##" foo"##),
        ])?;
        Ok(())
    }

    #[test]
    fn test_helper_replace() -> Result<(), Box<dyn Error>> {
        assert_renders(vec![(r##"{{ replace "foo" "oo" "aa"}}"##, r##"faa"##)])?;
        Ok(())
    }
}