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 |
---|---|
|
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 |
---|---|
|
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
{
"decodeURI" : decodeURI('http://asd/%20text%20to%20decode%20/text')
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
"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
[%header, cols="1,3"]
|===
| Name | Description
| `text` | URI component string.
|===
== Example
This example decodes a variety of URI components.
=== Source
[source,DataWeave, linenums]
%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 [source,JSON,linenums]
{ 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: [%header, cols="2,2"] |=== | Type (not escaped) | Examples | Reserved characters | ; , / ? : @ & = $ | Unescaped characters | alphabetic, decimal digits, - _ . ! ~ * ' ( ) | Number sign | # |=== == Parameters [%header, cols="1,3"] |=== | 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 [source,DataWeave, linenums]
{ "encodeURI" : encodeURI("http://asd/ text to decode /text"), "not_encoded": encodeURI("http://:;,/?:@&=\$-.!~*'()") }
=== Output [source,JSON,linenums]
{ "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`: [%header, cols="2,2"] |=== | Type | Includes | Reserved characters | | Unescaped characters | alphabetic, decimal digits, - _ . ! ~ * ' ( ) | Number sign | |=== == Parameters [%header, cols="1,3"] |=== | Name | Description | `text` | URI component string. |=== == Example This example encodes a variety of URI components. === Source [source,DataWeave, linenums]
%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 [source,JSON,linenums]
{ "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 [%header, cols="1,3"] |=== | Name | Description | `uri` | The URI input. |=== == Example This example parses a URL. === Source [source,DataWeave, linenums]
%dw 2.0 import * from dw::core::URL output application/json --- { 'composition': parseURI('https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#footer') }
=== Output [source,JSON,linenums]
{ "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 .Definition [source,DataWeave,linenums]
{ isValid: Boolean, host?: String, authority?: String, fragment?: String, path?: String, port?: Number, query?: String, scheme?: String, user?: String, isAbsolute?: Boolean, isOpaque?: Boolean }