Module: automat

Members

(static) automat.encodersRegex :RegExp

Finds string substitution lexemes that require HTML encoding.

Modify to suit.

Type:
  • RegExp
Default Value:
  • %{n}

(static) automat.replacersRegex :RegExp

Finds string substitution lexemes.

Modify to suit.

Type:
  • RegExp
Default Value:
  • ${n}

Methods

(static) append(template, el, referenceNodeopt, …replacementsopt) → {Array.<Node>}

Append or insert Nodes generated from formatted template into given el.

Parameters:
Name Type Attributes Default Description
template string | function

See template parameter of automat.

el HTMLElement
referenceNode Node <optional>
null

Inserts before this element within el or at end of el if null.

replacements * <optional>
<repeatable>

Replacement values for numbered format patterns.

Returns:

Array of the generated nodes (this is an actual Array instance; not an Array-like object).

Type
Array.<Node>

(static) automat(template, …replacementsopt) → {string}

String formatter.

String substitution is performed on numbered replacer patterns like ${n} or encoder patterns like %{n} where n is the zero-based arguments index. So ${0} would be replaced with the first argument following text.

Encoders are just like replacers except the argument is HTML-encoded before being used.

To change the format patterns, assign new RegExp patterns to automat.encoders and automat.replacers.

Parameters:
Name Type Attributes Description
template string | function

A template to be formatted as described above. Overloads:

  • A string primitive containing the template.
  • A function to be called with this as the calling context. The template is the value returned from this call.
replacements * <optional>
<repeatable>

Replacement values for numbered format patterns.

Returns:

The formatted text.

Type
string

(static) firstChild(template) → {HTMLElement}

Use this convenience wrapper to return the first child node described in template.

Parameters:
Name Type Description
template string | function

If a function, extract template from comment within.

Returns:

The first Node in your template.

Type
HTMLElement

(static) firstElement(template) → {HTMLElement}

Use this convenience wrapper to return the first child element described in template.

Parameters:
Name Type Description
template string | function

If a function, extract template from comment within.

Returns:

The first HTMLElement in your template.

Type
HTMLElement

(static) replace(template, elopt, …replacementsopt) → {HTMLElement}

Replace contents of el with Nodes generated from formatted template.

Parameters:
Name Type Attributes Description
template string | function

See template parameter of automat.

el HTMLElement <optional>

Node in which to return markup generated from template. If omitted, a new <div>...</div> element will be created and returned.

replacements * <optional>
<repeatable>

Replacement values for numbered format patterns.

Returns:

The el provided or a new <div>...</div> element, its innerHTML set to the formatted text.

Type
HTMLElement