Monday, August 10, 2020

Mule Develop Tricks: Get The Phone Number

The Requirement

The phone number from customer web site is free string. It can take the following forms:
  1.  +1 (219) 555-8888
  2.  +1 219-555-8888
  3.  (219) 555 8888
  4.  219 555 8888
  5.  219 555-8888
  6.  219-555-8888
  7.  -1-219-555-8888
There could be other form. 

We need to use dataweave 2.0 to extract 10 digits of the phone number to: 2195558888.

Solution

%dw 2.0

output application/json

---

{

mobile: (payload.mobilePhone replace /([^0-9.]+)/ with(""))[-10 to -1]

}


Take Aways

  1. dataweave built in function replace / / with ()
  2. substring [-10 to -1] take last 10 digits

Anypoint Studio Error: The project is missing Munit lIbrary to run tests

Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...