Dataweave 2.4 New Function Modules And Features Part-2

Authors: Ashish Singh Chauhan & Anjali Kushwaha

1. dw::util::Coercions – is a new DataWeave Module with functions and types to simplify data type transformations, for example, the function toString.

1) toArray(@StreamCapable text: String): Array<String>  – Splits a String value into an Array of characters

Parameters – 

  1. string: The String value to transform into an Array of characters (a Array<String> type)

Example

2) toBinary(str: String, encoding: String): Binary – Transform a String value into a Binary value using the specified encoding

Parameters –

  1. String
  2. encoding: The encoding to apply to the String value. Accepts encodings that are supported by your JDK

Example

3) toBoolean(str: String): Boolean – Transform a String value into a Boolean value

Parameters –

  1. String: String value to transform to boolean

Example

4) toDate(str: String, format: String | Null = null, locale: String | Null = null): Date – Transforms a String value into a Date value and accepts a format and locale

Parameters

  1. String- The String value to transform into a Date value
  2. Format – The formatting to use on the Date value. A null value has no effect on the Date value.
  3. locale- Optional ISO 3166 country code to use, such as US, AR, or ES. A null or absent value uses your JVM default.

Example

5) toDateTime(number: Number, unit: MillisOrSecs | Null = null): DateTime – Transforms a Number value into a DateTime value using milliseconds or seconds as the unit

Parameters

  1. Number: The Number value to transform into a DateTime value
  2. Unit: The unit to use for the conversion: “milliseconds” or “seconds”. A null value for the unit field defaults to “seconds”

Example

6) toLocalDateTime(str: String, format: String | Null = null, locale: String | Null = null): LocalDateTime – Transforms a String value into a LocalDateTime value and accepts a format and locale

Parameters

  1. String- The String value to transform into a Date value
  2. Format – The formatting to use on the localDateTime value. A null value has no effect on the Date value
  3. locale- Optional ISO 3166 country code to use, such as US, AR, or ES. A null or absent value uses your JVM default

Example

7) toPeriod(str: String): Period – Transform a String value into a Period value

Parameters

  1. String

Example

8) toRegex(str: String): Regex – Transforms a String value into a Regex value

Parameters

  1. String

Example

9) toUri(str: String): Uri – Transforms a String value into a Uri value

Parameters

  1. String

Example

2. dw::util::Math – is a new DataWeave Module with functions and variables that provides mathematical functions, for example, the function sin.

1) acos(angle: Number): Number | NaN – Returns an arc cosine value that can range from 0.0 through pi

If the absolute value of the input is greater than 1, the result is null

Parameters

  1. Angle – Number to convert

2) asin(angle: Number): Number | NaN – Returns an arc sine value that can range from -pi/2 through pi/2

If the absolute value of the input is greater than 1, the result is null.

Parameters

  1. Angle – Number to convert

3) atan(angle: Number): Number – Returns an arc tangent value that can range from -pi/2 through pi/2

Parameters

  1. Angle – Number to convert

Combined Example

4) cos(angle: Number): Number – Returns the trigonometric cosine of an angle from a given number of radians

Parameters

  1. Angle – Number to radians in an angle

5) sin(angle: Number): Number – Returns the trigonometric sine of an angle from a given number of radians

Parameters

  1. Angle – Number to radians in an angle

6) tan(angle: Number): Number – Returns the trigonometric tangent of an angle from a given number of radians

Parameters

  1. Angle – Number to radians in an angle

Combined Example

7) log10(a: Number): Number | NaN – Returns the logarithm base 10 of a number

Parameters

  1. num: A Number value that serves as input to the function

8) logn(a: Number): Number | NaN – Returns the natural logarithm (base e) of a number

If the input value is less than or equal to zero, the result is NaN (or null)

Parameters

  1. num: A Number value that serves as input to the function

Combined Example

9) toDegrees(angrad: Number): Number – Converts an angle measured in radians to an approximately equivalent number of degrees

Parameters

  1. angrad: Number of radians to convert to degrees

10) toRadians(angdeg: Number): Number – Converts a given number of degrees in an angle to an approximately equivalent number of radians

Parameters

  1. angdeg: Number of degrees to convert into radians

Combined Example

3. dw::util::Periods – It is a new DataWeave module with functions and types to simplify the creation of Periods, for example, the function days.

1) between(endDateExclusive: Date, startDateInclusive: Date): Period – Returns a Period (P) value consisting of the number of years, months, and days between two Date values

The start date is included, but the end date is not. The result of this method can be a negative period if the end date (endDateExclusive) is before the start date (startDateInclusive).

Parameters –

  1. endDateExclusive: The end date, exclusive
  2. startDateInclusive: The start date, inclusive

Example

2) days(nDays: Number): Period – Creates a Period value from the provided number of days

The function applies the period function to input that is a whole number and the duration function to decimal input

Parameters

  1. days: The number of hours as a whole or decimal number. A positive or negative number is valid

3) months(nMonths: Number): Period – Creates a Period value from the provided number of months

The function applies the period function to the input value

Parameters

  1. months: The number of months as a whole number. A positive or negative number is valid

4) years(nYears: Number): Period – Creates a Period value from the provided number of years

The function applies the period function to the input value.

Parameters –

  1. years: A whole number for the number of years. A positive or negative number is valid

5) duration(period: { days?: Number, hours?: Number, minutes?: Number, seconds?: Number }): Period – Creates a Period value that represents a number of days, hours, minutes, or seconds

Parameters

  1. periods: An object such as {days:4, hours:11, minutes:45, seconds: 55}. The key-value pairs are optional and can be specified in any order

Combined Example

6) hours(nHours: Number): Period – Creates a Period value from the provided number of hours

The function applies the duration function to the input value.

Parameters –

  1. hours: The number of hours as a whole or decimal number. A positive or negative number is valid

7) minutes(nMinutes: Number): Period – Creates a Period value from the provided number of minutes

The function applies the duration function to the input value

Parameters –

  1. minutes: The number of minutes as a whole or decimal number. A positive or negative number is valid.

8) seconds(nSecs: Number): Period -Creates a Period value from the provided number of seconds

Parameters –

  1. seconds: The number of seconds as a whole or decimal number. A positive or negative number is valid

Combined 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.