Search

Dark theme | Light theme
Showing posts with label DataWeaveDelight:Strings. Show all posts
Showing posts with label DataWeaveDelight:Strings. Show all posts

November 24, 2022

DataWeave Delight: Turn String Into Snake Case With underscore

In a previous post we learned we can turn a string into a string with kebab casing using dasherize from the dw::core::Strings module. If we want to turn a string into a string with camel casing we can use the underscore function. The underscore function will replace spaces, dashes and camel-casing with underscores, which makes the result snake-casing. Any uppercase characters are transformed to lowercase characters.

In the following example we use the underscore function with different input string arguments:

Soure

%dw 2.0
import underscore from dw::core::Strings
    
output application/json
---
{
    // Replaces camel casing with underscores.
    camelCase: underscore("stringInCamelCase"), // string_in_camel_case
    
    // Replaces dashes with underscores.
    kebab_case: underscore("string-with-dashes"), // string_with_dashes
    
    // Replaces spaces with underscores.
    spaces: underscore("string with spaces"), // string_with_spaces
    
    // Uppercase is transformed to lowercase.
    upper: underscore("STRING_WITH_UPPERCASE") // string_with_uppercase
}

Output

{
  "camelCase": "string_in_camel_case",
  "kebab_case": "string_with_dashes",
  "spaces": "string_with_spaces",
  "upper": "string_with_uppercase"
}

Written with DataWeave 2.4.

July 12, 2022

DataWeave Delight: Define Multi Line Strings

To define a string value in DataWeave we can use both double quotes or single quotes. To define a multi line string we simply define a string value over multiple lines. We don't have to do any strange concatenation, but simply define the value over multiple lines. DataWeave will add an end-of-line character automatically after each line.

In the following example we use different ways to defined single and multi line strings:

Source

%dw 2.0

import lines from dw::core::Strings

output application/json
---
{
    doubleQuotes: "String value defined using double quotes",
    singleQuotes: 'String value defined using single quotes',

    // Multi line strings can be defined as well.
    multiLineDoubleQuotes: "This is a multi line
string value
with double quotes",
    multiLineSingleQuotes: 'This is a multi line
string value with 
single quotes',

    // We can use the lines function to transform
    // each line into an element in an array.
    multiLines: lines("Multiline
string transformed
to array of strings")
}

Output

{
  "doubleQuotes": "String value defined using double quotes",
  "singleQuotes": "String value defined using single quotes",
  "multiLineDoubleQuotes": "This is a multi line\nstring value\nwith double quotes",
  "multiLineSingleQuotes": "This is a multi line\nstring value with \nsingle quotes",
  "multiLines": [
    "Multiline",
    "string transformed",
    "to array of strings"
  ]
}

Written with DataWeave 2.4.

June 27, 2022

DataWeave Delight: Getting The Ordinal Value For A Number

The dw::core::Strings has a lot of functions to deal with strings. One of the functions is ordinalize that takes a number as argument and returns the ordinal value as string.

The following exmaple shows several numbers transformed to their ordinal values:

Source

%dw 2.0

import ordinalize from dw::core::Strings

output application/json
---
{
    "1": ordinalize(1),
    "2": ordinalize(2),
    "3": ordinalize(3),
    "10": ordinalize(10),
    "location": "The restaurant is at the " ++ ordinalize(4) ++ " floor."
}

Output

{
  "1": "1st",
  "2": "2nd",
  "3": "3rd",
  "10": "10th",
  "location": "The restaurant is at the 4th floor."
}

Written with DataWeave 2.4.

February 10, 2022

DataWeave Delight: Turn string value into array of characters

A string value can be seen as an array of characters and if we want to transform our string value to an array we can use the toArray function in the dw::util::Coercions module. Once we have transformed our string to an array we can use all functions that work on arrays. The nice thing about DataWeave is that some functions that work on arrays already have an overloaded version that accepts a string value. Then we don't have to explicitly use the toArray function, but we can simply use our original value when we invoke the function.

In the following example we use the toArray function and also see the groupBy function that already accepts a string value and treats it is as an array:

Source

%dw 2.0

import toArray from dw::util::Coercions
import isUpperCase from dw::core::Strings

output application/json
---
{
    // toArray will convert a string to an array of characters
    toArray: toArray("DataWave"),
    
    // joinBy function only accepts an array argument
    toArrayJoinBy: toArray("abcd") joinBy "-",

    // groupBy function has been overloaded to accept string,
    // but internally makes it an array of characters
    groupByString: "AbCd" groupBy ((character, index) -> isUpperCase(character))
}

Output

{
  "toArray": [
    "D",
    "a",
    "t",
    "a",
    "W",
    "e",
    "a",
    "v",
    "e"
  ]
  "toArrayJoinBy": "a-b-c-d",
  "groupByString": {
    "true": "AC",
    "false": "bd"
  }
}

Written with DataWeave 2.4.

February 6, 2022

DataWeave Delight: Using string interpolation

In DataWeave we can use expressions in strings that will be evaluated and inserted into the string value. This is called string interpolation. The expression must be enclosed in parentheses where the first parenthesis is prefixed with a dollar sign: $(<expression>). The expression must return a string value or can be automatically coerced into a string value in order for it to work. The expression can also be a variable.

In the following example we use a variable, selector, the upper function and a calculation that returns a number that is coerced into a string:

Source

%dw 2.0

var prefix = "Hi"

var user = { alias: "mrhaki", name: "Hubert Klein Ikkink"}

var count = 41

output text/plain
---
"$(prefix), 
welcome $(user.name) also known as $(upper(user.alias)).
You're visitor $(count + 1)."

Output

Hi, 
welcome Hubert Klein Ikkink also known as MRHAKI.
You're visitor 42.

Written with DataWeave 2.4.

February 2, 2022

DataWeave Delight: Join array elements into a string

DataWeave has a stanard function to join array elements into a string value: joinBy in the dw::Core module. The joinBy function takes an array as first argument and a string value that is used as separator. The array should have string values or values that can be coerced into string values.

In the following example we use the function joinBy that will join elements in an array with a given separator:

Source

%dw 2.0

output application/json  
---
{
  // Join with default separator ("") using infix notation
  join: ["a", "b", "c"] joinBy "",

  // Join with ", " as separator
  separator: joinBy(["a", "b", "c"], ", "),
  
  // Elements can also be non-string values
  // if they can be coerced into string values
  range: [1, 2, 3, 4] joinBy "|"
}

Output

{
  "join": "abc",
  "separator": "a, b, c",
  "range": "1|2|3|4"
}  

Written with DataWeave 2.4.

DataWeave Delight: Wrapping string values

If we want to wrap a string value with another string value we can use the wrapWith and wrapIfMissing functions defined in the dw::core::Strings module. The first argument of these functions is the string we want to wrap and the second argument is the string value that wraps the first argument. The function wrapIfMissing will only apply the wrapper string if it was not already applied.

To remove a wrapped character from a wrapped string we can use the unwrap function. The first argument is the string value that is already wrapped and the second argument the character we want to use for unwrapping. The second argument can only be a single character, but we can repeatedly invoke the unwrap function to remove multiple wrap characters.

In the following example we use all three functions in different ways:

Source

%dw 2.0
import wrapWith, wrapIfMissing, unwrap from dw::core::Strings
output application/json  
---
{
  wrapWith: {
    // Wrap with single quotes
    sample1: wrapWith("DataWeave", "'"),

    // Even if already wrapped, keep on wrapping
    sample2: wrapWith("'DataWeave'", "'"),

    // We can use more than one character to wrap
    sample3: wrapWith("DataWeave", "{data}"),

    // Using infix notation
    sample4: "mrhaki" wrapWith "__"
  },

  wrapIfMissing: {
    // Wrap value only if not already wrapped  
    sample1: wrapIfMissing("DataWeave", "'"),

    // If already wrapped with given value then don't wrap
    sample2: wrapIfMissing("'DataWeave'", "'")
  },

  unwrap: {
    // Remove wrapping character(s)  
    sample1: unwrap("'DataWeave'", "'"),

    // If given wrap characters are not used, return the value
    sample2: unwrap("DataWeave", "'"),

    // Unwrap with more than one character only removes one wrap character
    sample3: unwrap("##DataWeave##", "##"),

    // Unwrap with more than one character gives unexpected results
    sample4: unwrap(unwrap("##DataWeave##", "#"), "#"),

    // With infix notation
    sample5: ("##DataWeave##" unwrap "#") unwrap "#"
  }
}

Output

{
  "wrapWith": {
    "sample1": "'DataWeave'",
    "sample2": "''DataWeave''",
    "sample3": "{data}DataWeave{data}",
    "sample4": "__mrhaki__"
  },
  "wrapIfMissing": {
    "sample1": "'DataWeave'",
    "sample2": "'DataWeave'"
  },
  "unwrap": {
    "sample1": "DataWeave",
    "sample2": "DataWeave",
    "sample3": "#DataWeave#",
    "sample4": "DataWeave",
    "sample5": "DataWeave"
  }
}

Written with DataWeave 2.4.

February 1, 2022

DataWeave Delight: Reverse a string value

DataWeave 2.4 introduced the reverse function in the Strings module. With this function we reverse the string value that we pass in as argument. The result is the reversed string.

In the following example we reverse the value DataWeave Delight:

Source

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

output text/plain  
---
reverse("DataWeave Delight")

Output

thgileD evaeWataD

Written with DataWeave 2.4.

DataWeave Delight: Repeating a string value

The Strings module in DataWeave has some very useful functions to work with string values. For example to repeat a string value we can use the repeat function. The first argument is the string value we want to repeat and the second argument the number of times the value must be repeated.

In the following example we use the repeat function to get 20 asterisks:

Source

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

output text/plain
---
repeat("*", 20)

Output

********************

The string value can also be more than one character:

Source

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

output text/plain
---
repeat("mrhaki ", 5)

Output

mrhaki mrhaki mrhaki mrhaki mrhaki

Written with DataWeave 2.4