Class MBTypes

All Implemented Interfaces:
Expression, Function

public class MBTypes extends MBExpression
The expressions in this section are provided for the purpose of testing for and converting between different data types like strings, numbers, and boolean values.

Often, such tests and conversions are unnecessary, but they may be necessary in some expressions where the type of a certain sub-expression is ambiguous. They can also be useful in cases where your feature data has inconsistent types; for example, you could use to-number to make sure that values like ""1.5"" (instead of 1.5) are treated as numeric

  • Constructor Details

    • MBTypes

      public MBTypes(JSONArray json)
  • Method Details

    • typesArray

      public Expression typesArray()
      Asserts that the input is an array (optionally with a specific item type and length). If, when the input expression is evaluated, it is not of the asserted type, then this assertion will cause the whole expression to be aborted. Example: ["array", value]: array ["array", type: "string" | "number" | "boolean", value]: array<type> ["array", type: "string" | "number" | "boolean", N: number (literal), value ]: array<type, N>
      Returns:
      array expression
    • typesBoolean

      public Expression typesBoolean()
      Asserts that the input value is a boolean. If multiple values are provided, each one is evaluated in order until a boolean is obtained. If none of the inputs are booleans, the expression is an error. Example: ["boolean", value]: boolean ["boolean", value, fallback: value, fallback: value, ...]: boolean
      Returns:
      boolean expression
    • typesNumber

      public Expression typesNumber()
      Asserts that the input value is a number. If multiple values are provided, each one is evaluated in order until a number is obtained. If none of the inputs are numbers, the expression is an error. Example: ["number", value]: number ["number", value, fallback: value, fallback: value, ...]: number
      Returns:
      number expression
    • typesObject

      public Expression typesObject()
      Asserts that the input value is an object. If multiple values are provided, each one is evaluated in order until an object is obtained. If none of the inputs are objects, the expression is an error. Example: ["object", value]: object ["object", value, fallback: value, fallback: value, ...]: object
      Returns:
      object expression
    • typesString

      public Expression typesString()
      Asserts that the input value is a string. If multiple values are provided, each one is evaluated in order until a string is obtained. If none of the inputs are strings, the expression is an error. Example: ["string", value]: string ["string", value, fallback: value, fallback: value, ...]: string
      Returns:
      string expression
    • typesToBoolean

      public Expression typesToBoolean()
      Converts the input value to a boolean. The result is false when then input is an empty string, 0, false, null, or NaN; otherwise it is true. Example: ["to-boolean", value]: boolean
      Returns:
      convert to boolean expression
    • typesToColor

      public Expression typesToColor()
      Converts the input value to a color. If multiple values are provided, each one is evaluated in order until the first successful conversion is obtained. If none of the inputs can be converted, the expression is an error. Example: ["to-color", value, fallback: value, fallback: value, ...]: color
      Returns:
      convert to color expression
    • typesToNumber

      public Expression typesToNumber()
      Converts the input value to a number, if possible. If the input is null or false, the result is 0. If the input is true, the result is 1. If the input is a string, it is converted to a number as specified by the "ToNumber Applied to the String Type" algorithm of the ECMAScript Language Specification. If multiple values are provided, each one is evaluated in order until the first successful conversion is obtained. If none of the inputs can be converted, the expression is an error. Example: ["to-number", value, fallback: value, fallback: value, ...]: number
      Returns:
      convert to number expression
    • typesToString

      public Expression typesToString()
      Converts the input value to a string. If the input is null, the result is "null". If the input is a boolean, the result is "true" or "false". If the input is a number, it is converted to a string as specified by the "NumberToString" algorithm of the ECMAScript Language Specification. If the input is a color, it is converted to a string of the form "rgba(r,g,b,a)", where r, g, and b are numerals ranging from 0 to 255, and a ranges from 0 to 1. Otherwise, the input is converted to a string in the format specified by the JSON.stringify function of the ECMAScript Language Specification. Example: ["to-string", value]: string
      Returns:
      convert to string expression
    • typesTypeOf

      public Expression typesTypeOf()
      Returns a string describing the type of the given value. Example: ["typeof", value]: string
      Returns:
      type of content as a string expression
    • getExpression

      public Expression getExpression() throws MBFormatException
      Description copied from class: MBExpression
      Determines which expression to use.
      Specified by:
      getExpression in class MBExpression
      Throws:
      MBFormatException