Type

◷ Reading Time: 3 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

asJson

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

 |asJson ()
Example: ("{'person': {'name': 'Alex', 'age': 25}}")|asJson()
Result:
{
     "person" : {
         "name" : "Alex",
         "age" : 25
     }
 }

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 January 12, 2021

Was this article helpful?

Related Articles