Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Replace old as(), typeof() docs with new note (and remove note)

Several operators can be applied to objects of any type.

Table of Contents

...


as()

The

...

as()

...

operators coerces objects of one type to be an object of another. The target type is given as a string argument to the operator. The following types are available:

  • "Array"

  • "Boolean"

  • "Map"

  • "Null"

  • "Number"

  • "RegExp"

  • "String"

The following coercions are valid:

  • Strings can be coerced to numbers (null if not parsable) and regular expressions.
  • Numbers can be coerced to strings.
  • Regular expressions can be coerced to strings.
  • Any expression can be coerced to Boolean.
  • null and false can be coerced to 0,

...

  • and true can be coerced to 1

An error is thrown for other attempted coercions, unless the value being coerced by as() is already of the

...

as()

The as() operators coerces objects of one type to be an object of another. The target type is given as a string argument to the operator. The following types are available in KRL:

  • str. Coerce the target to have type string
  • num. Coerce the target to have type number
  • regexp. Coerce the target to have type regular expression
  • trail. Coerce the target to have type trail

For example, the following constructs a string and then turns it into a regular expression that can be used by the replace() operator:

Code Block
languagejavascript
themeConfluence
("re#q=" + q + "#i").as("regexpRegExp")

...

  • Strings can be coerced to numbers and regular expressions.
  • Numbers can be coerced to strings.
  • Regular expressions can be coerced to strings.
  • Trails can be coerced to arrays.

defaultsTo()

The defaultsTo() operator returns the value of its first argument if the object is null. The second argument is a string and is optional. If present, it is written in the log if the object is null. 

...

Code Block
languagejavascript
themeConfluence
x = some_arr.filter(function(x){x == 5}).klog("Value after filter: ").head();

typeof()

The typeof() operator returns the type of the object to which it is applied. The return value is one of the strings that may be passed to as(). For example:

Code Block
languagejavascript
themeConfluence
nums = [1, 2, 3]
nums_type = nums.typeof() // nums_type = "Array"

...