Class: Format

baja. Format


new Format()

Format is used to format Objects into Strings using
a standardized formatting pattern language. The format String is normal
text with embedded scripts denoted by the percent (%) character. Use
"%%" to escape a real %.

A script is one or more calls chained together using the dot (.) operator.
Calls are mapped to methods using the order given below.

If a script cannot be processed successfully, an error will be returned.

To define an alternate output to use if an error is encountered, include a
? followed by another script within the same % pair. More than one fallback
can be defined by delimiting the fallbacks with a ?.

Given the call "foo" the order of attempted resolutions is:

  1. special call (see below)
  2. getFoo(Context)
  3. foo(Context)
  4. get("foo")

The following special functions are available to use in a script:

  1. lexicon(module:key)
  2. time() returns the current time as an AbsTime
  3. user() returns gets the current user's name
  4. decodeFromString(::) returns the toString of the encoded value for specified escaped string encoding for the specified type in the given module.
  5. escape() returns the escaped text value of the given objects toString()
  6. unescape() returns the unescaped text value of the given objects toString()
  7. substring() returns a substring value of a given objects toString()

This Constructor shouldn't be invoked directly. Please use the make()
methods to create an instance of a Format.

Examples of formats:

"hello world"
"my name is %displayName%"
"my parent's name is %parent.displayName%"
"%parent.value?lexicon(bajaui:dialog.error)%"
"%out.value?out.status?lexicon(bajaui:dialog.error)%"
"The escaped value %out.value.escape%"
"The unescaped valued %out.value.unescape%"
"The first two characters %out.value.substring(2)%"
"The first five characters %out.value.substring(5)%"
"The first five characters %out.value.substring(0, 5)%"
"The last five characters %out.value.substring(-5)%"
"The toString of a decoded baja:AbsTime from %decodeFromString(baja:AbsTime:$32016$2d04$2d10T13$3a37$3a00$2e000$2d04$3a00)%"
Example

Formats can use getter functions from an object as well as function names

var obj = {
  getFoo: function () {
    return {
      getBar: function () {
        return {
          value: function () {
            return 3.1415;
         }
       }
      }
    }
  }
}

var fmt = baja.Format.make("%foo.bar.value%");
return fmt.format( { object: obj } )
  .then(function (value) {
    // prints 3.1415
    console.log(value);
  });

Extends

Members


<static> DEFAULT :baja.Format

Default Format instance.

Type:

valueOf

Return the inner value of the Format.

Overrides:

Methods


<static> format(obj)

Format the specified object using the format pattern.

This method can take an object literal or a single pattern String
argument.

Parameters:
Name Type Description
obj Object
Properties
Name Type Argument Description
pattern String

the format pattern to process.

object Object | baja.Component <optional>

JavaScript Object or baja:Component
referenced by the format scripts.

display Boolean <optional>

if true, the display string of a Property value is used.
If false, the toString version of a Property value is used.
By default, this value is true (in BajaScript, most of the time
we're dealing with mounted Components in a Proxy Component Space).

ok function <optional>

(Deprecated: use Promise) the ok callback
called when the Format string has been processed. The resultant String
will be passed to this function as an argument

fail function <optional>

(Deprecated: use Promise) the fail callback
called if there's a fatal error processing the format.

cx Object <optional>

the designated context to be passed down to the toString method. Defaults to
an empty object.

Returns:
Type
Promise.<string>

<static> make( [pattern])

Make a Format.

Parameters:
Name Type Argument Description
pattern String <optional>

the Format Pattern String.

Returns:
Type
baja.Format

decodeAsync(str [, batch])

The string encoding of certain Simples may include Type information, or
other data that may be require asynchronous operations to decode. BOX is
designed to handle these situations when decoding data from the station,
but when user code needs to decode string-encoded Simples directly, prefer
this method as it gives the individual Simple a chance to import Types,
etc. to ensure that the decoded Simple is fully correct.

The default implementation just returns decodeFromString directly.

Parameters:
Name Type Argument Description
str string
batch baja.comm.Batch <optional>

optional batch to use

Inherited From:
Returns:

may return the Simple instance
directly, or a Promise resolving to same - so wrap in Promise.resolve()
if unsure.

Type
baja.Simple | Promise.<baja.Simple>

decodeFromString(str)

Decode a String to a Format.

Parameters:
Name Type Description
str String
Overrides:
Returns:
Type
baja.Format

encodeToString()

Encode Format to a String.

Overrides:
Returns:
Type
String

equals(obj)

Equality test.

Parameters:
Name Type Description
obj
Inherited From:
Returns:
Type
Boolean

equivalent(obj)

Equivalence test.

equivalent() is used to compare if two Objects have equivalent
state, but might not want to return true for equals since it
it has implied semantics for many operations. The default
implementation returns the result of baja.Object#equals.

Parameters:
Name Type Description
obj
Inherited From:
Returns:
Type
Boolean

format( [obj])

Format the specified object using the format pattern.

Parameters:
Name Type Argument Description
obj Object <optional>
Properties
Name Type Argument Description
ok function <optional>

(Deprecated: use Promise) the ok callback called
when the Format string has been processed. The resultant String will be
passed to this function as an argument

fail function <optional>

(Deprecated: use Promise) the fail callback
called if there's a fatal error processing the format.

See:
Returns:
Type
Promise.<String>

getAgents( [is] [, batch])

Returns a promise that resolves to the agent list for this Object.

Parameters:
Name Type Argument Description
is Array.<String> <optional>

An optional array of filters to add to the
agent query.

batch baja.comm.Batch <optional>

An optional object used to batch network
calls together.

Inherited From:
See:
Returns:

A promise that will resolve with the Agent Info.

Type
Promise

getIcon()

Return the Object's Icon.

Inherited From:
Returns:
Type
baja.Icon

getType()

Get the type of this instance.

Inherited From:
Returns:
Type
Type

getTypeDisplayName( [cx])

Gets the friendly type display name for this object.

Parameters:
Name Type Argument Description
cx Object <optional>

a context to be passed down to Type

Since:
  • Niagara 4.10
Inherited From:
See:
  • baja.Type#getDisplayName
Returns:

If no context is provided, the type
display name is returned synchronously as a string. If context provided,
the type display name is resolved via a promise as a string.

Type
Promise.<string> | string

make( [pattern])

Make a Format.

Parameters:
Name Type Argument Description
pattern String <optional>

the Format Pattern String.

Overrides:
Returns:
Type
baja.Format

newCopy( [exact])

Every value may be cloned using the newCopy method.

Please note that Simples are immutable so they don't
allocate a new instance.

Parameters:
Name Type Argument Description
exact Boolean <optional>

true if an exact copy of the value should be
made (only valid in the Component architecture).

Inherited From:
See:
Returns:

a copy of the value (or the same instance if the value is a
Simple).


toString()

Return a String representation of the Format.

Overrides:
Returns:
Type
String