Dataweave 2.4 New Function Modules And Features  Part-1

Authors: Ashish Singh Chauhan & Anjali Kushwaha

1. In dw::Core Module – Introduces new functions and annotations:

1. indexOf() 

A) indexOf(array: Array, value: Any): Number- Returns the index of the first occurrence of the specified element in this array, or -1 if this list does not contain the element.

Parameters- 

  1. Array: the array of elements to search
  2. Value: the value to search

Example →

B) indexOf(string: String, value: String): Number- Returns the index of the first occurrence of the specified character in this string, or -1 if this list does not contain the character.

Parameters- 

  1. string: the string to search
  2. value: the value to search

Example →

2. lastIndexOf()

A) lastIndexOf(array: Array, value: Any): Number- Returns the index of the last occurrence of the specified element in a given array or -1 if the array does not contain the element.

Parameters-

  1. Array: the array of elements to search
  2. Value: the value to search

Example →

 B) lastIndexOf(array: String, value: String): Number- Returns the index of the last occurrence of the specified character in a given string or -1 if the string does not contain the character.

Parameters- 

  1. string: the string to search
  2. value: the value to search

Example →

3) onNull<R>(previous: Null, callback: () -> R) : R- Executes a callback function if the preceding expression returns a null value and then replaces the null value with the result of the callback.

Parameters-

  1. previous: the value of preceding expression.
  2. callback: Callback that generates a new value if previous returns null

Example →

4) then<T, R>(previous: T, callback: (result: T) -> R): R- This function works as a pipe that passes the value returned from the preceding expression to the next (a callback) only if the value returned by the preceding expression is not null.

Parameters-

  1. previous: the value of preceding expression.
  2. callback: callback that processes the result of previous if the result is not null

Example →

2. Dates (dw::core::Dates)

This module contains functions for creating and manipulating dates.

To use this module, you must import it to your DataWeave code, for example, by adding the line import * from dw::core::Dates to the header of your DataWeave script.

  1. atBeginningOfDay(dateTime: DateTime): DateTime: Returns a new DateTime value that changes the Time value in the input to the beginning of the specified day.

The hours, minutes, and seconds in the input change to 00:00:00.

Parameters

dateTime: The DateTime value to reference

  1. atBeginningOfHour(dateTime: DateTime): DateTime- Returns a new DateTime value that changes the Time value in the input to the beginning of the specified hour.

Parameters

dateTime: The DateTime value to reference

  1. atBeginningOfMonth(dateTime: DateTime): DateTime- Returns a new DateTime value that changes the Day value from the input to the first day of the specified month. It also sets the Time value to 00:00:00.

Parameters

dateTime: The DateTime value to reference

  1. atBeginningOfWeek(dateTime: DateTime): DateTime- Returns a new DateTime value that changes the Day and Time values from the input to the beginning of the first day of the specified week.

The function treats Sunday as the first day of the week.

Parameters

dateTime: The DateTime value to reference

  1. atBeginningOfYear(dateTime: DateTime): DateTime- Takes a DateTime value as input and returns a DateTime value for the first day of the year specified in the input. It also sets the Time value to 00:00:00.

Parameters

dateTime: The DateTime value to reference

Combined Example →

  1. date(parts: DateFactory): Date- Creates a Date value from values specified for year, month, and day fields.

Parameters

parts: Number values for year, month, and day fields. The month must be a value between 1 and 12, and the day value must be between 1 and 31.

  1. dateTime(parts: DateTimeFactory): DateTime- Creates a DateTime value from values specified for year, month, day, hour, minutes, seconds, and timezone fields.

Parameters

parts: Number values for year, month, day, hour, minutes, and seconds fields followed by a TimeZone value for the timezone field.  The input fields are parts of a DateTimeFactory type

  1. localDateTime(parts: LocalDateTimeFactory): LocalDateTime- Creates a LocalDateTime value from values specified for year, month, day, hour, minutes, and seconds fields.

Parameters-

parts: Number values for year, month, day, hour, minutes, and seconds fields. The input fields are parts of a LocalDateTimeFactory type.

  1. localTime(parts: LocalTimeFactory): LocalTime- Creates a LocalTime value from values specified for hour, minutes, and seconds fields.

Parameters

parts: Number values for hour, minutes, and seconds fields.The input fields are parts of a LocalDateTimeFactory type.

  1. time(parts: TimeFactory): Time – Creates a Time value from values specified for hour, minutes, seconds, and timezone fields.

Parameters

parts: Number values for hour, minutes, and seconds fields, and a TimeZone value for the timezone field.The input fields are parts of a TimeFactory type.

Combined Example →

  1. today(): Date – Returns the date for today as a Date type.
  2. tomorrow(): Date – Returns the date for tomorrow as a Date type.
  3. yesterday(): Date- Returns the date for yesterday as a Date type.

Combined Example →

3. dw::core::Strings : introduces new functions:

1. collapse(text: String): Array<String>- Collapses the string into substrings of equal characters.

Each substring contains a single character or identical characters that are adjacent to one another in the input string. Empty spaces are treated as characters.

Example –

2. countCharactersBy(text: String, predicate: (character: String) -> Boolean): Number- Counts the number of times an expression that iterates through each character in a string returns true.

Parameters

  1. string: The string to which the predicate applies
  2. predicate: Expression to apply to each character in the text string. The expression must return a Boolean value.

Example →

3. countMatches(text: String, pattern: String): Number- Counts the number of matches in a string.

Parameters

  1. string: string to search for match
  2. pattern: substring to find in string

Example →

4. first(text: String, amount: Number): String – Returns characters from the beginning of a string to the specified number of characters in the string, for example, the first two characters of a string

If the number is equal to or greater than the number of characters in the string, the function returns the entire string.

Parameters

  1. String
  2. Amount: The number of characters to return. Negative numbers and 0 return an empty string. Decimals are rounded down to the nearest whole number

Example →

5. everyCharacter(text: String, condition: (character: String) -> Boolean): Boolean- Checks whether a condition is valid for every character in a string.

Parameters

  1. String
  2. Condition: Expression that iterates through the characters in the string that it checks and returns a Boolean value.

Example →

6. hammingDistance(a: String, b: String): Number | Null- Returns the Hamming distance between two strings

Parameters

  1. String 1
  2. String 2

Example →

7. last(text: String, amount: Number): String- Returns characters from the end of string to a specified number of characters, for example, the last two characters of a string

Parameters

  1. String
  2. Amount: The number of characters to return. Negative numbers and 0 return an empty string. Decimals are rounded down to the nearest whole number

Example

8. levenshteinDistance(a: String, b: String): Number- Returns the Levenshtein distance (or edit distance) between two strings

Parameters

  1. String 1
  2. String 2

Example →

9. lines(text: String): Array<String>- Returns an array of lines from a string

Parameters

  1. String

Example →

10. mapString(@StreamCapable text: String, mapper: (character: String, index: Number) -> String): String – Applies an expression to every character of a string

Parameters-

  1. String
  2. Mapper – Expression that applies to each character ($) or index ($$) of the text string and returns a string.

Example →

11. remove(text: String, toRemove: String): String- Removes all occurrences of a specified pattern from a string

Parameters

  1. String
  2. toRemove

Example →

12. replaceAll(text: String, target: String, replacement: String): String- Replaces all substrings that match a literal search string with a specified replacement string

Parameters

  1. String
  2. Target – string to find and replace
  3. Replacement – replacement value

Example →

13. reverse(text: String): String- Reverses sequence of characters in a string

Parameters

  1. String

Example →

14. someCharacter(text: String, condition: (character: String) -> Boolean): Boolean- Checks whether a condition is valid for at least one of the characters or blank spaces in a string

Parameters-

  1. String
  2. Condition: Expression that iterates through the characters and spaces in the string and returns a Boolean value.

Example →

15. substring(text: String, from: Number, until: Number): String – Returns a substring that spans from the character at the specified from index to the last character before the until index

The characters in the substring satisfy the condition from <= indexOf(string) < until.

Parameters

  1. String
  2. From – lower index to include
  3. Until – upper index to exclude

Example →

16. substringBy(text: String, predicate: (character: String, index: Number) -> Boolean): Array<String> – Splits a string at each character where the predicate expression returns true

Parameters

  1. String
  2. Predicate: Expression that tests each character and returns a Boolean value. The expression can iterate over each character and index of the string.

Example →

17. substringEvery(text: String, amount: Number): Array<String> – Splits a string into an array of substrings equal to a specified length

The last substring can be shorter than that length. If the length is greater than or equal to the length of the string to split, the function returns the entire string.

Parameters

  1. String
  2. Amount: desired length of each substring

Example →

18. words(text: String): Array<String> – Returns an array of words from a string

Separators between words include blank spaces, new lines, and tabs.

Parameters

  1. String

Example →

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.