This module contains helper functions to work with Strings.

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

Functions

compose

compose(Array<String>, Array<String>): String

Uses a custom interpolator to replace URL components with a encodeURIComponent result.

Parameters
Name Description

parts

A string array that provides the part of the URL to encode.

Example

This example passes in text to encode using $(myText), where myText is a variable defined in the header. Notice that the spaces in the input are encoded in the output URL as %20. Also notice that the entire input to the compose function is surrounded by backticks.

Source
1
2
3
4
5
6
%dw 2.0
import * from dw::core::URL
var myText = " text to encode "
output application/json
---
{ "composition" : compose `http://asd/$(myText)/text` }
Output
1
{ "composition": "http://asd/%20text%20to%20encode%20/text" }

decodeURI

decodeURI(String): String

Decodes the escape sequences (such as %20) in a URI.

The function replaces each escape sequence in the encoded URI with the character that it represents, but does not decode escape sequences that could not have been introduced by encodeURI. The character # is not decoded from escape sequences.

Parameters
Name Description

text

The URI to decode.

Example

This example decodes a URI that contains the URL percent encoding %20, which is used for spaces.

Source
1
2
3
4
5
6
7
%dw 2.0
import * from dw::core::URL
output application/json
---
{
  "decodeURI" : decodeURI('http://asd/%20text%20to%20decode%20/text')
}
Output
1
2
3
{
  "decodeURI": "http://asd/ text to decode /text"
}

decodeURIComponent

decodeURIComponent(String): String

Decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or a similar routine.

For an example, see encodeURIComponent.

Parameters
Name Description

text

URI component string.

Example

This example decodes a variety of URI components.

Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::core::URL
output application/json
---
{
  "decodeURIComponent": {
    "decodeURIComponent" : decodeURIComponent("%20PATH/%20TO%20/DECODE%20"),
    "decodeURIComponent" : decodeURIComponent("%3B%2C%2F%3F%3A%40%26%3D"),
    "decodeURIComponent" : decodeURIComponent("%2D%5F%2E%21%7E%2A%27%28%29%24"),
  }
}
Output
1
2
3
4
5
6
7
{
   decodeURIComponent: {
     decodeURIComponent: " PATH/ TO /DECODE ",
     decodeURIComponent: ";,/?:@&=",
    decodeURIComponent: "-_.!~*'()\$"
   }
}

encodeURI

encodeURI(String): String

Encodes a URI with UTF-8 escape sequences.

Applies up to four escape sequences for characters composed of two "surrogate" characters. The function assumes that the URI is a complete URI, so it does not encode reserved characters that have special meaning.

The function does not encode these characters with UTF-8 escape sequences:

Type (not escaped) Examples

Reserved characters

; , / ? : @ & = $

Unescaped characters

alphabetic, decimal digits, - _ . ! ~ * ' ( )

Number sign

#

Parameters
Name Description

text

The URI to encode.

Example

This example shows encodes spaces in one URL and lists some characters that do not get encoded in the not_encoded string.

Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::core::URL
output application/json
---
{
    "encodeURI" : encodeURI("http://asd/ text to decode /text"),
    "not_encoded": encodeURI("http://:;,/?:@&=\$_-_.!~*'()")
}
Output
1
2
3
4
{
   "encodeURI": "http://asd/%20text%20to%20decode%20/%25/\"\'/text",
   "not_encoded": "http://:;,/?:@&=$_-_.!~*'()"
}

encodeURIComponent

encodeURIComponent(String): String

Escapes certain characters in a URI component using UTF-8 encoding.

There can be only four escape sequences for characters composed of two "surrogate" * characters. encodeURIComponent escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( ). Note that encodeURIComponent differs from encodeURI in that it encodes reserved characters and the Number sign # of encodeURI:

Type Includes

Reserved characters

Unescaped characters

alphabetic, decimal digits, - _ . ! ~ * ' ( )

Number sign

Parameters
Name Description

text

URI component string.

Example

This example encodes a variety of URI components.

Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
%dw 2.0
import * from dw::core::URL
output application/json
---
{
  "comparing_encode_functions_output" : {
          "encodeURIComponent" : encodeURI(" PATH/ TO /ENCODE "),
          "encodeURI" : encodeURI(" PATH/ TO /ENCODE "),
          "encodeURIComponent_to_hex" : encodeURIComponent(";,/?:@&="),
          "encodeURI_not_to_hex" : encodeURI(";,/?:@&="),
          "encodeURIComponent_not_encoded" : encodeURIComponent("-_.!~*'()"),
          "encodeURI_not_encoded" : encodeURI("-_.!~*'()")
  }
}
Output
1
2
3
4
5
6
7
8
9
10
{
  "comparing_encode_functions_output": {
    "encodeURIComponent": "%20PATH/%20TO%20/ENCODE%20",
    "encodeURI": "%20PATH/%20TO%20/ENCODE%20",
    "encodeURIComponent_to_hex": "%3B%2C%2F%3F%3A%40%26%3D",
    "encodeURI_not_to_hex": ";,/?:@&=",
    "encodeURIComponent_not_encoded": "-_.!~*'()",
    "encodeURI_not_encoded": "-_.!~*'()"
  }
}

parseURI

parseURI(String): URI

Parses a URL and returns a URI object.

The isValid: Boolean property in the output URI object indicates whether the parsing process succeeded. Every field in this object is optional, and a field will appear in the output only if it was present in the URL input.

Parameters
Name Description

uri

The URI input.

Example

This example parses a URL.

Source
1
2
3
4
5
6
7
%dw 2.0
import * from dw::core::URL
output application/json
---
{
  'composition': parseURI('https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#footer')
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
{
  "composition": {
    "isValid": true,
    "raw": "https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#footer",
    "host": "en.wikipedia.org",
    "authority": "en.wikipedia.org",
    "fragment": "footer",
    "path": "/wiki/Uniform_Resource_Identifier",
    "scheme": "https",
    "isAbsolute": true,
    "isOpaque": false
  }
}

Types

URI

Describes the URI type. For descriptions of the fields, see URL Types (dw::core::URL).

Definition
1
{ isValid: Boolean, host?: String, authority?: String, fragment?: String, path?: String, port?: Number, query?: String, scheme?: String, user?: String, isAbsolute?: Boolean, isOpaque?: Boolean }