Template overview
This module implements a CVTemplate 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 CVTemplatePlaceholder’s.
” is a “, “-year-old “ and “.” are the immutable parts of the template. We call them CVTemplateSeparator’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 CVTemplate, you must feed it to a CVTemplateParser or CVTemplateFormatter.
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
Table of contents
Constructors
make
Constructor
Signature
export declare const make: <const PS extends CVTemplateParts.Type>(
...templateParts: PS
) => Type<CVTemplateParts.ToPlaceHolderTypes<PS>>
Getters
templateParts
Returns the templateParts property of self
Signature
export declare const templateParts: <PlaceholderTypes extends MTypes.Object>(
self: Type<PlaceholderTypes>
) => CVTemplateParts.Type
Models
Type (class)
Type that represents a CVTemplate
Signature
export declare class Type<PlaceholderTypes> {
private constructor({
syntheticDescription,
placeholderDescription,
templateParts
}: MTypes.Data<Type<PlaceholderTypes>>)
}
make (static method)
Static constructor
Signature
static make<const PS extends CVTemplateParts.Type>(
templateParts: PS,
): Type<CVTemplateParts.ToPlaceHolderTypes<PS>>
[MData.idSymbol] (method)
Returns the id of this
Signature
[MData.idSymbol](): string | (() => string)
syntheticDescription (property)
Synthetic description of self
Signature
readonly syntheticDescription: string
placeholderDescription (property)
Placeholder description of self
Signature
readonly placeholderDescription: string
templateParts (property)
Array of the TemplatePart’s composing this template
Signature
readonly templateParts: CVTemplateParts.Type
Module markers
moduleTag
Module tag
Signature
export declare const moduleTag: "@parischap/conversions/formatting/template/"