Search

Dark theme | Light theme

November 20, 2022

DataWeave Delight: Turn String Into Kebab Casing With dasherize

The dw::core::Strings module has useful functions for working with string values. One of the functions is dasherize. The function takes a string argument and replaces spaces, underscores and camel-casing into dashes. The resulting string value with hyphens is also called kebab-casing. The dasherize function also turns any uppercase character to lowercase.

In the following example we have different input arguments with camel-casing, underscores and spaces:

Source

%dw 2.0
import dasherize from dw::core::Strings

output application/json
---
{
    // Replaces camel casing with dashes.
    camelCase: dasherize("stringInCamelCase"), // string-in-camel-case

    // Replaces underscores with dashes.
    snake_case: dasherize("string_with_underscores"), // string-with-underscores

    // Replaces spaces with dashes.
    spaces: dasherize("string with spaces"), // string-with-spaces

    // Uppercase is transformed to lowercase.
    upper: dasherize("STRING_WITH_UPPERCASE") // string-with-uppercase
}

Output

{
  "camelCase": "string-in-camel-case",
  "snake_case": "string-with-underscores",
  "spaces": "string-with-spaces",
  "upper": "string-with-uppercase"
}

Written with DataWeave 2.4.