@bemoje/template 2.0.1 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.cjs CHANGED
@@ -156,3 +156,5 @@ exports.JsonFileTemplateStrategy = JsonFileTemplateStrategy;
156
156
  exports.StringTemplateStrategy = StringTemplateStrategy;
157
157
  exports.Template = Template;
158
158
  exports.TextFileTemplateStrategy = TextFileTemplateStrategy;
159
+ //# sourceMappingURL=index.cjs.map
160
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../tschema/src/SchemaValidationError.ts","../../../tschema/src/assertValidSchema.ts","../../src/Template/Template.ts","../../src/strategies/JsonFileTemplateStrategy.ts","../../src/strategies/StringTemplateStrategy.ts","../../src/strategies/TextFileTemplateStrategy.ts"],"names":["SchemaValidationError","Error","value","errors","message","name","Array","isArray","ValueErrorIterator","from","Value","Errors","assertValidSchema","schema","data","isValid","Check","Template","strategy","optionsSchema","template","options","Type","Object","templateSchema","default","assertValidTemplate","createSchema","render","stringTemplate","templateToString","merged","Cast","populated","replaceAll","_","key","String","renderString","string","variables","keys","properties","variable","includes","JsonFileTemplateStrategy","JSON","stringify","parse","StringTemplateStrategy","TextFileTemplateStrategy","join","split"],"mappings":";;;;;;;;AASO,IAAMA,qBAAAA,GAAN,cAAoCC,KAAAA,CAAAA;EAR3C;;;AASWC,EAAAA,KAAAA;AACAC,EAAAA,MAAAA;EACT,WAAA,CAAYA,MAAAA,EAAqDD,SAAgBE,OAAAA,EAAiB;AAChG,IAAA,KAAA,CAAMA,OAAAA,CAAAA;AACN,IAAA,IAAA,CAAKC,IAAAA,GAAO,uBAAA;AACZ,IAAA,IAAA,CAAKH,KAAAA,GAAQA,OAAAA;AACb,IAAA,IAAA,CAAKC,SAASG,KAAAA,CAAMC,OAAAA,CAAQJ,MAAAA,CAAAA,GACxBA,MAAAA,GACAA,kBAAkBK,2BAAAA,GAChBF,KAAAA,C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AAAA,EAAyB;AACnC,IAAA,IAAA,CAAKA,cAAAA,GAAiBA,cAAAA,IAAmBF,YAAAA,CAAKC,MAAAA,CAAO,EAAC,CAAA;AACxD,EAAA;;;;AAKAO,EAAAA,gBAAAA,CAAiBV,QAAAA,EAAkC;AACjD,IAAA,OAAO0B,IAAAA,CAAKC,SAAAA,CAAU3B,QAAAA,EAAU,IAAA,EAAM,CAAA,CAAA;AACxC,EAAA;;;;AAKAQ,EAAAA,MAAAA,CAAOK,SAAAA,EAAmC;AACxC,IAAA,OAAOa,IAAAA,CAAKE,MAAMf,SAAAA,CAAAA;AACpB,EAAA;AACF;AC5BO,IAAMgB,yBAAN,MAAMA;EATb;;;AAUWzB,EAAAA,cAAAA,GAAiBF,aAAKe,MAAAA,EAAM;;;;AAKrCP,EAAAA,gBAAAA,CAAiBV,QAAAA,EAAmC;AAClD,IAAA,OAAOA,QAAAA;AACT,EAAA;;;;AAKAQ,EAAAA,MAAAA,CAAOK,SAAAA,EAAoC;AACzC,IAAA,OAAOA,SAAAA;AACT,EAAA;AACF;ACfO,IAAMiB,2BAAN,MAAMA;EATb;;;AAUW1B,EAAAA,cAAAA,GAAiBF,YAAAA,CAAKhB,KAAAA,CAAMgB,YAAAA,CAAKe,MAAAA,EAAM,CAAA;;;;AAKhDP,EAAAA,gBAAAA,CAAiBV,QAAAA,EAA2C;AAC1D,IAAA,OAAOA,QAAAA,CAAS+B,KAAK,IAAA,CAAA;AACvB,EAAA;;;;AAKAvB,EAAAA,MAAAA,CAAOK,SAAAA,EAA4C;AACjD,IAAA,OAAOA,SAAAA,CAAUmB,MAAM,IAAA,CAAA;AACzB,EAAA;AACF","file":"index.cjs","sourcesContent":["import type { TSchema } from '@sinclair/typebox'\nimport { Value } from '@sinclair/typebox/value'\nimport type { ValueError } from '@sinclair/typebox/compiler'\nimport { ValueErrorIterator } from '@sinclair/typebox/compiler'\n\n/**\n * Error thrown when a value does not match a given schema.\n * Contains an array of ValueError instances with details about each violation.\n */\nexport class SchemaValidationError extends Error {\n readonly value: unknown\n readonly errors: ValueError[]\n constructor(errors: TSchema | ValueError[] | ValueErrorIterator, value: unknown, message: string) {\n super(message)\n this.name = 'SchemaValidationError'\n this.value = value\n this.errors = Array.isArray(errors)\n ? errors\n : errors instanceof ValueErrorIterator\n ? Array.from(errors)\n : Array.from(Value.Errors(errors, value))\n }\n}\n","import { SchemaValidationError } from './SchemaValidationError'\nimport type { Static } from '@sinclair/typebox'\nimport type { TSchema } from '@sinclair/typebox'\nimport { Value } from '@sinclair/typebox/value'\n\n/**\n * Asserts that data conforms to a TypeBox schema, throwing a SchemaValidationError if it doesn't.\n */\nexport function assertValidSchema<Schema extends TSchema>(\n schema: Schema,\n data: unknown,\n message: string\n): asserts data is Static<Schema> {\n const isValid = Value.Check(schema, data)\n if (!isValid) {\n throw new SchemaValidationError(Value.Errors(schema, data), data, message)\n }\n}\n","import type { Static } from '@sinclair/typebox'\nimport type { TObject } from '@sinclair/typebox'\nimport type { TSchema } from '@sinclair/typebox'\nimport type { TemplateStrategy } from '../interfaces/TemplateStrategy'\nimport { Type } from '@sinclair/typebox'\nimport { Value } from '@sinclair/typebox/value'\nimport { assertValidSchema } from '@mono/tschema'\n\nexport interface TemplateOptions<TemplateSchema extends TSchema, OptionsSchema extends TObject> {\n readonly strategy: TemplateStrategy<TemplateSchema>\n readonly optionsSchema?: OptionsSchema\n readonly template: Static<TemplateSchema>\n}\n\n/**\n * A generic template engine that supports variable substitution using the Strategy pattern.\n * Validates templates and options against TypeBox schemas and renders templates with provided data.\n *\n * Supports mustache-style `{{variable}}` syntax for variable substitution.\n *\n * @example\n * ```typescript\n * const strategy = new StringTemplateStrategy()\n * const optionsSchema = Type.Object({\n * name: Type.String({ default: 'World' }),\n * greeting: Type.String({ default: 'Hello' })\n * })\n *\n * const template = new Template({\n * strategy,\n * optionsSchema,\n * template: \"{{greeting}} {{name}}!\"\n * })\n *\n * const result = template.render({ name: 'TypeScript' })\n * // Returns: \"Hello TypeScript!\"\n * ```\n */\nexport class Template<TemplateSchema extends TSchema, OptionsSchema extends TObject> {\n protected readonly strategy: TemplateStrategy<TemplateSchema>\n protected readonly optionsSchema: OptionsSchema\n protected readonly template: Static<TemplateSchema>\n\n constructor(options: TemplateOptions<TemplateSchema, OptionsSchema>) {\n this.strategy = options.strategy\n this.optionsSchema = options.optionsSchema ?? (Type.Object({}) as OptionsSchema)\n this.template = options.template\n this.strategy.templateSchema.default = this.template\n this.assertValidTemplate()\n }\n\n /**\n * Returns the template schema with the template set as the default value.\n */\n createSchema(): TemplateSchema {\n return this.strategy.templateSchema\n }\n\n /**\n * Renders the template with the provided data, returning the result in the template schema format.\n *\n * Variables in the template using `{{variableName}}` syntax are replaced with corresponding\n * values from the data object. Data is validated against the options schema before rendering.\n *\n * @example\n * ```typescript\n * const result = template.render({ name: 'John', age: 30 })\n * ```\n */\n render(data: Static<OptionsSchema> = {}): Static<TemplateSchema> {\n const stringTemplate = this.strategy.templateToString(this.template)\n const merged = Value.Cast(this.optionsSchema, data)\n assertValidSchema(this.optionsSchema, merged, 'Invalid options')\n const populated = stringTemplate.replaceAll(/{{(\\w+)}}/g, (_, key) => String(merged[key]))\n return this.strategy.render(populated)\n }\n\n /**\n * Renders the template and returns the result as a string representation.\n * Convenience method that combines `render()` and `templateToString()`.\n */\n renderString(data: Static<OptionsSchema> = {}): string {\n return this.strategy.templateToString(this.render(data))\n }\n\n /**\n * Validates that the template contains all variables defined in the options schema.\n *\n * @throws {Error} When a variable defined in the options schema is not found in the template\n */\n protected assertValidTemplate() {\n const string = this.strategy.templateToString(this.template)\n const variables = Object.keys(this.optionsSchema.properties)\n for (const variable of variables) {\n if (!string.includes(`{{${variable}}}`)) {\n throw new Error(`Template does not include variable: ${variable}`)\n }\n }\n }\n}\n","import type { Static } from '@sinclair/typebox'\nimport type { TObject } from '@sinclair/typebox'\nimport type { TemplateStrategy } from '../interfaces/TemplateStrategy'\nimport { Type } from '@sinclair/typebox'\n\n/**\n * Template strategy for handling JSON file templates with structured object schemas.\n *\n * Converts structured objects to formatted JSON strings and parses JSON strings\n * back to typed objects. Uses pretty-printing with 2-space indentation for\n * human-readable output.\n *\n * @template Schema - Object schema type extending TObject, defaults to empty object\n */\nexport class JsonFileTemplateStrategy<Schema extends TObject = TObject> implements TemplateStrategy<Schema> {\n readonly templateSchema: Schema\n\n /**\n * Creates a JSON file template strategy with optional schema validation.\n *\n * @param templateSchema - TypeBox object schema for template validation.\n * Defaults to empty object schema if not provided.\n */\n constructor(templateSchema?: Schema) {\n this.templateSchema = templateSchema ?? (Type.Object({}) as Schema)\n }\n\n /**\n * Converts a template object to pretty-printed JSON string.\n */\n templateToString(template: Static<Schema>): string {\n return JSON.stringify(template, null, 2)\n }\n\n /**\n * Parses JSON string back to typed object conforming to schema.\n */\n render(populated: string): Static<Schema> {\n return JSON.parse(populated)\n }\n}\n","import type { Static } from '@sinclair/typebox'\nimport type { TString } from '@sinclair/typebox'\nimport type { TemplateStrategy } from '../interfaces/TemplateStrategy'\nimport { Type } from '@sinclair/typebox'\n\n/**\n * Template strategy for handling simple string templates.\n *\n * Provides pass-through behavior for string templates where the template\n * and rendered output are both plain strings. Useful for text-based templates\n * that don't require parsing or complex structure.\n */\nexport class StringTemplateStrategy implements TemplateStrategy<TString> {\n readonly templateSchema = Type.String()\n\n /**\n * Returns the template string as-is (pass-through).\n */\n templateToString(template: Static<TString>): string {\n return template\n }\n\n /**\n * Returns the populated string as-is (pass-through).\n */\n render(populated: string): Static<TString> {\n return populated\n }\n}\n","import type { Static } from '@sinclair/typebox'\nimport type { TArray } from '@sinclair/typebox'\nimport type { TString } from '@sinclair/typebox'\nimport type { TemplateStrategy } from '../interfaces/TemplateStrategy'\nimport { Type } from '@sinclair/typebox'\n\n/**\n * Template strategy for handling multi-line text file templates.\n *\n * Converts arrays of strings to newline-separated text and parses text files\n * back to string arrays by splitting on newlines. Ideal for processing\n * configuration files, scripts, or any line-based text content.\n */\nexport class TextFileTemplateStrategy implements TemplateStrategy<TArray<TString>> {\n readonly templateSchema = Type.Array(Type.String())\n\n /**\n * Joins string array into newline-separated text.\n */\n templateToString(template: Static<TArray<TString>>): string {\n return template.join('\\n')\n }\n\n /**\n * Splits text content into string array by newlines.\n */\n render(populated: string): Static<TArray<TString>> {\n return populated.split('\\n')\n }\n}\n"]}
package/lib/index.js CHANGED
@@ -151,3 +151,5 @@ var TextFileTemplateStrategy = class {
151
151
  };
152
152
 
153
153
  export { JsonFileTemplateStrategy, StringTemplateStrategy, Template, TextFileTemplateStrategy };
154
+ //# sourceMappingURL=index.js.map
155
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../tschema/src/SchemaValidationError.ts","../../../tschema/src/assertValidSchema.ts","../../src/Template/Template.ts","../../src/strategies/JsonFileTemplateStrategy.ts","../../src/strategies/StringTemplateStrategy.ts","../../src/strategies/TextFileTemplateStrategy.ts"],"names":["SchemaValidationError","Error","value","errors","message","name","Array","isArray","ValueErrorIterator","from","Value","Errors","assertValidSchema","schema","data","isValid","Check","Template","strategy","optionsSchema","template","options","Type","Object","templateSchema","default","assertValidTemplate","createSchema","render","stringTemplate","templateToString","merged","Cast","populated","replaceAll","_","key","String","renderString","string","variables","keys","properties","variable","includes","JsonFileTemplateStrategy","JSON","stringify","parse","StringTemplateStrategy","TextFileTemplateStrategy","join","split"],"mappings":";;;;;;AASO,IAAMA,qBAAAA,GAAN,cAAoCC,KAAAA,CAAAA;EAR3C;;;AASWC,EAAAA,KAAAA;AACAC,EAAAA,MAAAA;EACT,WAAA,CAAYA,MAAAA,EAAqDD,OAAgBE,OAAAA,EAAiB;AAChG,IAAA,KAAA,CAAMA,OAAAA,CAAAA;AACN,IAAA,IAAA,CAAKC,IAAAA,GAAO,uBAAA;AACZ,IAAA,IAAA,CAAKH,KAAAA,GAAQA,KAAAA;AACb,IAAA,IAAA,CAAKC,SAASG,KAAAA,CAAMC,OAAAA,CAAQJ,MAAAA,CAAAA,GACxBA,MAAAA,GACAA,kB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;;;;AAQT,EAAA,WAAA,CAAYA,cAAAA,EAAyB;AACnC,IAAA,IAAA,CAAKA,cAAAA,GAAiBA,cAAAA,IAAmBF,IAAAA,CAAKC,MAAAA,CAAO,EAAC,CAAA;AACxD,EAAA;;;;AAKAO,EAAAA,gBAAAA,CAAiBV,QAAAA,EAAkC;AACjD,IAAA,OAAO0B,IAAAA,CAAKC,SAAAA,CAAU3B,QAAAA,EAAU,IAAA,EAAM,CAAA,CAAA;AACxC,EAAA;;;;AAKAQ,EAAAA,MAAAA,CAAOK,SAAAA,EAAmC;AACxC,IAAA,OAAOa,IAAAA,CAAKE,MAAMf,SAAAA,CAAAA;AACpB,EAAA;AACF;AC5BO,IAAMgB,yBAAN,MAAMA;EATb;;;AAUWzB,EAAAA,cAAAA,GAAiBF,KAAKe,MAAAA,EAAM;;;;AAKrCP,EAAAA,gBAAAA,CAAiBV,QAAAA,EAAmC;AAClD,IAAA,OAAOA,QAAAA;AACT,EAAA;;;;AAKAQ,EAAAA,MAAAA,CAAOK,SAAAA,EAAoC;AACzC,IAAA,OAAOA,SAAAA;AACT,EAAA;AACF;ACfO,IAAMiB,2BAAN,MAAMA;EATb;;;AAUW1B,EAAAA,cAAAA,GAAiBF,IAAAA,CAAKhB,KAAAA,CAAMgB,IAAAA,CAAKe,MAAAA,EAAM,CAAA;;;;AAKhDP,EAAAA,gBAAAA,CAAiBV,QAAAA,EAA2C;AAC1D,IAAA,OAAOA,QAAAA,CAAS+B,KAAK,IAAA,CAAA;AACvB,EAAA;;;;AAKAvB,EAAAA,MAAAA,CAAOK,SAAAA,EAA4C;AACjD,IAAA,OAAOA,SAAAA,CAAUmB,MAAM,IAAA,CAAA;AACzB,EAAA;AACF","file":"index.js","sourcesContent":["import type { TSchema } from '@sinclair/typebox'\nimport { Value } from '@sinclair/typebox/value'\nimport type { ValueError } from '@sinclair/typebox/compiler'\nimport { ValueErrorIterator } from '@sinclair/typebox/compiler'\n\n/**\n * Error thrown when a value does not match a given schema.\n * Contains an array of ValueError instances with details about each violation.\n */\nexport class SchemaValidationError extends Error {\n readonly value: unknown\n readonly errors: ValueError[]\n constructor(errors: TSchema | ValueError[] | ValueErrorIterator, value: unknown, message: string) {\n super(message)\n this.name = 'SchemaValidationError'\n this.value = value\n this.errors = Array.isArray(errors)\n ? errors\n : errors instanceof ValueErrorIterator\n ? Array.from(errors)\n : Array.from(Value.Errors(errors, value))\n }\n}\n","import { SchemaValidationError } from './SchemaValidationError'\nimport type { Static } from '@sinclair/typebox'\nimport type { TSchema } from '@sinclair/typebox'\nimport { Value } from '@sinclair/typebox/value'\n\n/**\n * Asserts that data conforms to a TypeBox schema, throwing a SchemaValidationError if it doesn't.\n */\nexport function assertValidSchema<Schema extends TSchema>(\n schema: Schema,\n data: unknown,\n message: string\n): asserts data is Static<Schema> {\n const isValid = Value.Check(schema, data)\n if (!isValid) {\n throw new SchemaValidationError(Value.Errors(schema, data), data, message)\n }\n}\n","import type { Static } from '@sinclair/typebox'\nimport type { TObject } from '@sinclair/typebox'\nimport type { TSchema } from '@sinclair/typebox'\nimport type { TemplateStrategy } from '../interfaces/TemplateStrategy'\nimport { Type } from '@sinclair/typebox'\nimport { Value } from '@sinclair/typebox/value'\nimport { assertValidSchema } from '@mono/tschema'\n\nexport interface TemplateOptions<TemplateSchema extends TSchema, OptionsSchema extends TObject> {\n readonly strategy: TemplateStrategy<TemplateSchema>\n readonly optionsSchema?: OptionsSchema\n readonly template: Static<TemplateSchema>\n}\n\n/**\n * A generic template engine that supports variable substitution using the Strategy pattern.\n * Validates templates and options against TypeBox schemas and renders templates with provided data.\n *\n * Supports mustache-style `{{variable}}` syntax for variable substitution.\n *\n * @example\n * ```typescript\n * const strategy = new StringTemplateStrategy()\n * const optionsSchema = Type.Object({\n * name: Type.String({ default: 'World' }),\n * greeting: Type.String({ default: 'Hello' })\n * })\n *\n * const template = new Template({\n * strategy,\n * optionsSchema,\n * template: \"{{greeting}} {{name}}!\"\n * })\n *\n * const result = template.render({ name: 'TypeScript' })\n * // Returns: \"Hello TypeScript!\"\n * ```\n */\nexport class Template<TemplateSchema extends TSchema, OptionsSchema extends TObject> {\n protected readonly strategy: TemplateStrategy<TemplateSchema>\n protected readonly optionsSchema: OptionsSchema\n protected readonly template: Static<TemplateSchema>\n\n constructor(options: TemplateOptions<TemplateSchema, OptionsSchema>) {\n this.strategy = options.strategy\n this.optionsSchema = options.optionsSchema ?? (Type.Object({}) as OptionsSchema)\n this.template = options.template\n this.strategy.templateSchema.default = this.template\n this.assertValidTemplate()\n }\n\n /**\n * Returns the template schema with the template set as the default value.\n */\n createSchema(): TemplateSchema {\n return this.strategy.templateSchema\n }\n\n /**\n * Renders the template with the provided data, returning the result in the template schema format.\n *\n * Variables in the template using `{{variableName}}` syntax are replaced with corresponding\n * values from the data object. Data is validated against the options schema before rendering.\n *\n * @example\n * ```typescript\n * const result = template.render({ name: 'John', age: 30 })\n * ```\n */\n render(data: Static<OptionsSchema> = {}): Static<TemplateSchema> {\n const stringTemplate = this.strategy.templateToString(this.template)\n const merged = Value.Cast(this.optionsSchema, data)\n assertValidSchema(this.optionsSchema, merged, 'Invalid options')\n const populated = stringTemplate.replaceAll(/{{(\\w+)}}/g, (_, key) => String(merged[key]))\n return this.strategy.render(populated)\n }\n\n /**\n * Renders the template and returns the result as a string representation.\n * Convenience method that combines `render()` and `templateToString()`.\n */\n renderString(data: Static<OptionsSchema> = {}): string {\n return this.strategy.templateToString(this.render(data))\n }\n\n /**\n * Validates that the template contains all variables defined in the options schema.\n *\n * @throws {Error} When a variable defined in the options schema is not found in the template\n */\n protected assertValidTemplate() {\n const string = this.strategy.templateToString(this.template)\n const variables = Object.keys(this.optionsSchema.properties)\n for (const variable of variables) {\n if (!string.includes(`{{${variable}}}`)) {\n throw new Error(`Template does not include variable: ${variable}`)\n }\n }\n }\n}\n","import type { Static } from '@sinclair/typebox'\nimport type { TObject } from '@sinclair/typebox'\nimport type { TemplateStrategy } from '../interfaces/TemplateStrategy'\nimport { Type } from '@sinclair/typebox'\n\n/**\n * Template strategy for handling JSON file templates with structured object schemas.\n *\n * Converts structured objects to formatted JSON strings and parses JSON strings\n * back to typed objects. Uses pretty-printing with 2-space indentation for\n * human-readable output.\n *\n * @template Schema - Object schema type extending TObject, defaults to empty object\n */\nexport class JsonFileTemplateStrategy<Schema extends TObject = TObject> implements TemplateStrategy<Schema> {\n readonly templateSchema: Schema\n\n /**\n * Creates a JSON file template strategy with optional schema validation.\n *\n * @param templateSchema - TypeBox object schema for template validation.\n * Defaults to empty object schema if not provided.\n */\n constructor(templateSchema?: Schema) {\n this.templateSchema = templateSchema ?? (Type.Object({}) as Schema)\n }\n\n /**\n * Converts a template object to pretty-printed JSON string.\n */\n templateToString(template: Static<Schema>): string {\n return JSON.stringify(template, null, 2)\n }\n\n /**\n * Parses JSON string back to typed object conforming to schema.\n */\n render(populated: string): Static<Schema> {\n return JSON.parse(populated)\n }\n}\n","import type { Static } from '@sinclair/typebox'\nimport type { TString } from '@sinclair/typebox'\nimport type { TemplateStrategy } from '../interfaces/TemplateStrategy'\nimport { Type } from '@sinclair/typebox'\n\n/**\n * Template strategy for handling simple string templates.\n *\n * Provides pass-through behavior for string templates where the template\n * and rendered output are both plain strings. Useful for text-based templates\n * that don't require parsing or complex structure.\n */\nexport class StringTemplateStrategy implements TemplateStrategy<TString> {\n readonly templateSchema = Type.String()\n\n /**\n * Returns the template string as-is (pass-through).\n */\n templateToString(template: Static<TString>): string {\n return template\n }\n\n /**\n * Returns the populated string as-is (pass-through).\n */\n render(populated: string): Static<TString> {\n return populated\n }\n}\n","import type { Static } from '@sinclair/typebox'\nimport type { TArray } from '@sinclair/typebox'\nimport type { TString } from '@sinclair/typebox'\nimport type { TemplateStrategy } from '../interfaces/TemplateStrategy'\nimport { Type } from '@sinclair/typebox'\n\n/**\n * Template strategy for handling multi-line text file templates.\n *\n * Converts arrays of strings to newline-separated text and parses text files\n * back to string arrays by splitting on newlines. Ideal for processing\n * configuration files, scripts, or any line-based text content.\n */\nexport class TextFileTemplateStrategy implements TemplateStrategy<TArray<TString>> {\n readonly templateSchema = Type.Array(Type.String())\n\n /**\n * Joins string array into newline-separated text.\n */\n templateToString(template: Static<TArray<TString>>): string {\n return template.join('\\n')\n }\n\n /**\n * Splits text content into string array by newlines.\n */\n render(populated: string): Static<TArray<TString>> {\n return populated.split('\\n')\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bemoje/template",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "A TypeScript template engine with schema validation and pluggable rendering strategies.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -8,7 +8,7 @@
8
8
  "access": "public"
9
9
  },
10
10
  "dependencies": {
11
- "@sinclair/typebox": "^0.34.48"
11
+ "@sinclair/typebox": "^0.34.52"
12
12
  },
13
13
  "keywords": [
14
14
  "template",
@@ -27,7 +27,9 @@
27
27
  "./lib/index.js",
28
28
  "./lib/index.d.ts",
29
29
  "./lib/index.cjs",
30
- "./lib/index.d.cts"
30
+ "./lib/index.d.cts",
31
+ "./lib/index.js.map",
32
+ "./lib/index.cjs.map"
31
33
  ],
32
34
  "exports": {
33
35
  ".": {