I wanted to write a function in DataWeave (DW 2.0, Mule Runtime 4.3) that could decorate a text message with a banner of * around it, mostly to help call out events in the log.
What I came up with was this, but it feels a little bit Rube Goldberg still. So is there a much more efficient way to write this that I've overlooked?
%dw 2.0
output text/plain
var msg = "WT 3-4"
fun banner(in) =
do {
var width = sizeOf(in) + 4
var standout = 1 to (width) map "*" joinBy ""
---
standout ++ "\n* " ++ in ++ " *\n" ++ standout
}
---
//banner ("Hello World")
banner (msg)
This gives me:
***************
* Hello World *
***************
and
**********
* WT 3-4 *
**********
respectively.
My objections to this are many, but this question is primarily about the construction of the banner strings.
var standout = 1 to (sizeOf(in) + 4) map "*" joinBy ""
There HAS to be a better way to arithmetically assemble a string than using map() and joinBy() on the width parameter.
Right?
Well, that is embarrassing. When this idea is refactored for string interpolation and the use of the right tool, the answer looks much better.
%dw 2.0
output text/plain
import repeat from dw::core::Strings
var msg = "Read the Release Notes!"
fun banner(in) =
do {
var width = sizeOf(in) + 4
var standout = repeat("*", width)
---
"$(standout)\n* $(in) *\n$(standout)"
}
---
banner(msg)
The highly apt output then becomes:
***************************
* Read the Release Notes! *
***************************