Skip to content

EduardaSRBastos/dataweave-utilities

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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))
}

About

Collection of useful dataweave functions

Topics

Resources

License

Stars

Watchers

Forks