6 releases

0.3.0 Jul 29, 2024
0.2.1 Jul 29, 2024
0.2.0 Jan 7, 2024
0.1.2 Oct 6, 2023

#324 in Template engine


Used in ctgen

BSD-2-Clause

17KB
269 lines

Crates.io API build and test dependency status

handlebars-inflector

String inflector helper for handlebars-rust

Quick Start

Developed and tested with handlebars-rust v4.4.0 and Inflector v0.11.4.
Versions 0.1.* are compatible with handlebars 4.
Versions 0.2.* are compatible with handlebars 5. (Thanks to campeis) Versions 0.3.* are compatible with handlebars 6.

Registration

    use handlebars::Handlebars;
    use handlebars_inflector::HandlebarsInflector;
    
    let mut h = Handlebars::new();
    h.register_helper("inflect", Box::new(HandlebarsInflector));

Usage

The helper is looking for exactly one parameter of type string. Operations can be stacked but are then executed
in a specific predefined order. The operation parameter value doesn't matter as long as the parameter is
present.

{{inflect param to_singular=true to_sentence_case=true to_lower_case=true }}

Operations

List of possible operations in the order of execution:
to_camel_case: product_images to productImages
to_pascal_case: product_images to ProductImages
to_snake_case: ProductImages to product_images
to_screaming_snake_case: ProductImages to PRODUCT_IMAGES
to_kebab_case: product_images to product-images
to_train_case: product_images to Product-Images
to_sentence_case: product_images to Product images
to_title_case: product_images to Product Images
ordinalize: July 1 to July 1st
deordinalize: July 1st to July 1
to_foreign_key: Product image to product_image_id
demodulize: std::io to Io
deconstantize: std::io to Std
to_class_case: product_images to ProductImage
to_table_case: ProductImage to product_images
to_plural: ProductImage to ProductImages
to_singular: product_images to product_image
to_upper_case: product_images to PRODUCT_IMAGES
to_lower_case: ProductImages to productimages

Note that some combinations might not be compatible with one another.
If you still absolutely need to do such type of combination you could nest the operations like:
Bars::Foos to Bar

{{inflect (inflect param deconstantize=true) to_singular=true}}

Acknowledgements

This is basically a thin wrapper around the Inflector crate. Kudos to it's developer.

License

This library (handlebars-inflector) is open sourced under the BSD 2 License.
P.S.: I don't really care about the license so I picked one of my dependencies' license. :)

Dependencies

~3–4.5MB
~88K SLoC