Type

◷ Reading Time: 4 minutes

as

Converts values to certain types. It returns object of a Type

 |as (typeName)
  • typeName (String): Name of the type to convert (Mandatory)
Example: {Title:'a title', PublishedAt:now(), Author:{Name:'Joe', Family:'Smith'}}|as (TBook)
Result: An object of TBook (when TBook is already registered)

is

Determines whether an object is from a specific type.It returns a boolean value.

 |is (typeName)
  • typeName (String): Name of the type to convert (Mandatory)
Example: list 
        |any  (x, x |is (Car)  
        
Result: true

asString

Converts the input to string typed value.

 |asString (surroundWithQuote)
  • surroundWithQuote (Boolean): Whether to surround the output with quotations (Optional)
Example: 55|asString (true) 
Result: '55'

As we passed true, it is surrounded with quotations.
Example: {'person': {'name': 'Alex', 'age': 25}} |asString () 
Result: {"person":{"name":"Alex","age":25}}

Returns the JSON as a string.
Example: 34|asString () 
Result: 34

Using string as a monad
Example: asString (34) 
Result: 34

Using string as a function

asInt

Converts the input to integer typed value and returns an integer value.

 |asInt ()
Example: '34'|asInt ()
Result: 34

asDouble

Converts the input to double typed value and returns a double value.
This is similar to add adding a ‘d’ at the end of a number (eg. 34d).

 |asDouble ()
Sample:'2.5'|asDouble () 
Result: 2.5

asDecimal

Converts the input to decimal typed value and returns a decimal value.
This is similar to add adding an ‘m’ at the end of a number (eg. 34m).

 |asDecimal ()
Example:'22.34'|asDecimal () 
Result: 22.35

asBoolean

Converts the input to a boolean typed value and returns a Boolean value (true/false) .

 |asBoolean ()
Example: 'false'|asBoolean () 
Result: False

asDate

Converts the input to DateTime typed value and returns a DateTime value.

 |asDate (format)
  • format (Format): Custom format to convert the input to DateTime (Optional)
Example: '03/22/2009'|asDate (dd/MM/yyyy)  
Result: 03/22/2009
Example: asDate ('03/22/2009')  
Result: 03/22/2009
DateTime format specifier
Below is the list of formatted:

Year
y: The year, from 0 to 99
yy: The year, from 00 to 99
yyy: The year, with a minimum of three digits
yyyy: The year as a four-digit number

Month
M: The month, from 1 through 12
MM: The month, from 01 through 12
MMM: The abbreviated name of the month
MMMM: The full name of the month

Day
d: The day of the month, from 1 through 31
dd: The day of the month, from 01 through 31
ddd: The day of the month, from 01 through 31
dddd: The full name of the day of the week

Hour
h: The hour, using a 12-hour clock from 1 to 12
hh: The hour, using a 12-hour clock from 01 to 12
H: The hour, using a 24-hour clock from 0 to 23
HH: The hour, using a 24-hour clock from 00 to 23

Minute
m: The minute, from 0 through 59
mm: The minute, from 00 through 59

Second
s: The second, from 0 through 59
ss: The second, from 00 through 59

AM/PM
t: The first character of the AM/PM designator
tt: The AM/PM designator

For example: MM/dd/YYYY shows the abbreviated name of the month, the day of the month, from 01 through 31 and the year as a four-digit number separated with /

asJson

Converts the input to a JSON value and returns a JSON value

 |asJson (environment)
  • environment (Key-value pair): This is an optional parameter. If you want to set a specific environment, this parameter can be used ({environment:’php’} for PHP Json convertion).
Example: 

phpJson := 'a:9:{s:12:"cfdb7_status";s:4:"read";s:9:"your-name";s:1:"a";s:10:"your-email";s:11:"a@gmail.com";s:12:"your-message";s:3:"abc";s:5:"title";s:9:"downloads";s:3:"ref";s:47:"c20651a0-1ec0-45af-b999-bb08c3d48ce4.1646401368";s:6:"source";s:0:"";s:5:"topic";s:0:"";s:3:"ads";s:0:"";}'

phpJson |asJson({environment:'php'})

Result:

{
    "cfdb7_status" : "read",
    "your-message" : "abc",
    "topic" : "",
    "your-name" : "a",
    "your-email" : "a@gmail.com",
    "title" : "downloads",
    "source" : "",
    "ads" : "",
    "ref" : "c20651a0-1ec0-45af-b999-bb08c3d48ce4.1646401368"
}

asJsonString

Converts the input to a its JSON string representation and returns a string.

 |asJsonString ()
Example: ({'person': {'name': 'Alex', 'age': 25}})|asJsonString ()  
Result: {'person': {'name': 'Alex', 'age': 25}}
Updated on April 6, 2022

Was this article helpful?

Related Articles