Skip to main content Link Search Menu Expand Document (external link)

Template.ts overview

This module implements a MTemplate which is a model of a text that has always the same structure. In such a text, there are immutable and mutable parts. Let’s take the following two texts as an example:

  • Text1 = “John is a 47-year-old man.”
  • Text2 = “Jenny is a 5-year-old girl.”

These two texts obviously share the same structure which is the template:

“Placeholder1 is a Placeholder2-year-old Placeholder3”.

Placeholder1, Placeholder2 and Placeholder3 are the mutable parts of the template. They contain valuable information. We call them MTemplatePlaceholder’s.

” is a “, “-year-old “ and “.” are the immutable parts of the template. We call them MTemplateSeparator’s.

From a text with the above structure, we can extract the values of Placeholder1, Placeholder2, and Placeholder3. In the present case:

  • For text1: { Placeholder1 : ‘John’, Placeholder2 : ‘47’, Placeholder3 : ‘man’ }
  • For text2: { Placeholder1 : ‘Jenny’, Placeholder2 : ‘5’, Placeholder3 : ‘girl’}

Extracting the values of placeholders from a text according to a template is called parsing. The result of parsing is an object whose properties are named after the name of the placeholders they represent.

Inversely, given a template and the values of the placeholders that compose it (provided as the properties of an object), we can generate a text. This is called formatting. In the present case, with the object:

{ Placeholder1 : ‘Tom’, Placeholder2 : ‘15’, Placeholder3 : ‘boy’ }

We will obtain the text: “Tom is a 15-year-old boy.”

Once you have created a MTemplate, you must feed it to MString.templateFormat or MString.templateParse.

Note that effect does provide the Schema.TemplateLiteralParser API which partly addresses the same problem. But there are some limitations to that API. For instance, template literal types cannot represent a fixed-length string or a string composed only of capital letters… It is for instance impossible to represent a date in the form YYYYMMDD with the Schema.TemplateLiteralParser. A schema in the form const schema = Schema.TemplateLiteralParser(Schema.NumberFromString,Schema.NumberFromString, Schema.NumberFromString) does not work as the first NumberFromString combinator reads the whole date


Exports Grouped by Category


Constructors

make

Constructor

Signature

declare const make: <const PS extends internalTemplateParts.Type>(
  ...templateParts: PS
) => Type<internalTemplateParts.ToPlaceHolderTypes<PS>>

Source

Getters

templateParts

Returns the templateParts property of self

Signature

declare const templateParts: <PlaceholderTypes extends MTypes.Object>(
  self: Type<PlaceholderTypes>
) => internalTemplateParts.Type

Source

Models

Type (class)

Type that represents a MTemplate

Signature

declare class Type<PlaceholderTypes> {
  private constructor({
    syntheticDescription,
    placeholderDescription,
    templateParts
  }: MTypes.Data<Type<PlaceholderTypes>>)
}

Source

make (static method)

Static constructor

Signature

declare const make: <const PS extends internalTemplateParts.Type>(
  templateParts: PS
) => Type<internalTemplateParts.ToPlaceHolderTypes<PS>>

Source

[MData.idSymbol] (method)

Returns the id of this

Signature

declare const [MData.idSymbol]: () => string | (() => string)

Source

syntheticDescription (property)

Synthetic description of self

Signature

readonly syntheticDescription: string

Source

placeholderDescription (property)

Placeholder description of self

Signature

readonly placeholderDescription: string

Source

templateParts (property)

Array of the TemplatePart’s composing this template

Signature

readonly templateParts: internalTemplateParts.Type

Source

Module markers

moduleTag

Module tag

Signature

declare const moduleTag: "@parischap/effect-lib/Template/"

Source