Skip to content

Latest commit

 

History

History
84 lines (68 loc) · 1.88 KB

README.md

File metadata and controls

84 lines (68 loc) · 1.88 KB

Dataweave Utilities

Collection of useful dataweave functions


Remove Invalid XML Characters

Dataweave Playground

Function
%dw 2.0
output application/xml

var removeInvalidChars = (payload) -> 
    if (payload is String) 
        payload replace /[^\p{Print}]/ with "" 
    else if (payload is Object) 
        payload mapObject { 
            ($$): removeInvalidChars($)
        }
    else if (payload is Array) 
        payload map (item) -> removeInvalidChars(item)
    else 
        payload
---

main: removeInvalidChars(payload)

Simple Cipher

Dataweave Playground

Function
%dw 2.0
output application/json
import * from dw::core::Strings

fun encryptChar(char, index) =
    fromCharCode(
        charCodeAt(char, 0) +
        (((index + 2) mod 8) * 8 - 2) +
        ((index + 2) mod 32) - 16 + 128
    )

fun encryptText(text) =
    if (isEmpty(text)) 
        ""
    else
        reverse(
            (text splitBy "")
                map ((char, index) -> encryptChar(char, index))
                joinBy ""
        )

fun decryptChar(char, index) =
    fromCharCode(
        charCodeAt(char, 0) -
        (((index + 2) mod 8) * 8 - 2) -
        ((index + 2) mod 32) + 16 - 128
    )

fun decryptText(text) =
    if (isEmpty(text)) 
        ""
    else
    (reverse(text) splitBy "")
        map ((char, index) -> decryptChar(char, index))
        joinBy ""

---
{
  "Encrypted message": encryptText(payload.message),
  "Decrypted message": decryptText(encryptText(payload.message))
}