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 = “Jehnny 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 CVTemplateSeperator
’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 : ‘Jehnny’, 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.”
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<PS>
Destructors
templateParts
Returns the templateParts
property of self
Signature
export declare const templateParts: <const PS extends CVTemplateParts.Type>(self: Type<PS>) => PS
Formatting
toFormatter
Returns a function that tries to format an object into a string according to ‘self’. The generated formatter returns a Right
of a string upon success, a Left
otherwise.
Signature
export declare const toFormatter: <const PS extends CVTemplateParts.Type>(
self: Type<PS>
) => MTypes.OneArgFunction<
{
readonly [k in keyof MTypes.ArrayKeys<PS> as PS[k] extends CVTemplatePlaceholder.All
? CVTemplatePlaceholder.ExtractName<PS[k]>
: never]: PS[k] extends CVTemplatePlaceholder.All ? CVTemplatePlaceholder.ExtractType<PS[k]> : never
},
Either.Either<string, MInputError.Type>
>
toThrowingFormatter
Same as toFormatter
but the generated formatter throws in case of failure
Signature
export declare const toThrowingFormatter: <const PS extends CVTemplateParts.Type>(
self: Type<PS>
) => MTypes.OneArgFunction<
{
readonly [k in keyof MTypes.ArrayKeys<PS> as PS[k] extends CVTemplatePlaceholder.All
? CVTemplatePlaceholder.ExtractName<PS[k]>
: never]: PS[k] extends CVTemplatePlaceholder.All ? CVTemplatePlaceholder.ExtractType<PS[k]> : never
},
string
>
Guards
has
Type guard
Signature
export declare const has: (u: unknown) => u is Type<ReadonlyArray<CVTemplatePart.Type<string, unknown>>>
Models
Type (interface)
CVTemplate
Type
Signature
export interface Type<out PS extends CVTemplateParts.Type> extends MInspectable.Type, Pipeable.Pipeable {
/** Array of the TemplatePart's composing this template */
readonly templateParts: PS
/** @internal */
readonly [_TypeId]: {
readonly _P: Types.Covariant<PS>
}
}
Module markers
moduleTag
Module tag
Signature
export declare const moduleTag: "@parischap/conversions/Template/"
Parsing
toParser
Returns a function that tries to parse a text into an object according to ‘self’. The generated parser returns a Right
of an object upon success, a Left
otherwise.
Signature
export declare const toParser: <const PS extends CVTemplateParts.Type>(
self: Type<PS>
) => MTypes.OneArgFunction<
string,
Either.Either<
{
readonly [k in keyof MTypes.ArrayKeys<PS> as PS[k] extends CVTemplatePlaceholder.All
? CVTemplatePlaceholder.ExtractName<PS[k]>
: never]: PS[k] extends CVTemplatePlaceholder.All ? CVTemplatePlaceholder.ExtractType<PS[k]> : never
},
MInputError.Type
>
>
toThrowingParser
Same as toParser
but the generated parser throws in case of failure
Signature
export declare const toThrowingParser: <const PS extends CVTemplateParts.Type>(
self: Type<PS>
) => MTypes.OneArgFunction<
string,
{
readonly [k in keyof MTypes.ArrayKeys<PS> as PS[k] extends CVTemplatePlaceholder.All
? CVTemplatePlaceholder.ExtractName<PS[k]>
: never]: PS[k] extends CVTemplatePlaceholder.All ? CVTemplatePlaceholder.ExtractType<PS[k]> : never
}
>