From cd710aab9e5a81ecd07754023c80eb46f8c3006d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BECHER?= Date: Sat, 24 May 2025 13:34:58 +0200 Subject: [PATCH] refactoring --- src/index.ts | 1 - src/pattern.ts | 17 ---------- src/placeholder.ts | 73 ++++++++++++++++++++++++++++++++++++++----- src/string-section.ts | 55 -------------------------------- 4 files changed, 66 insertions(+), 80 deletions(-) delete mode 100644 src/pattern.ts delete mode 100644 src/string-section.ts diff --git a/src/index.ts b/src/index.ts index 41473db..ab3aa33 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,4 @@ export * from "./placeholder.js"; -export * from "./pattern.js"; export * as VariableResolvers from "./resolver/index.js"; export * from "./resolver/types.js"; export * as UnknownVariableHandlers from "./unknown-variable-handler/index.js"; diff --git a/src/pattern.ts b/src/pattern.ts deleted file mode 100644 index e6a3541..0000000 --- a/src/pattern.ts +++ /dev/null @@ -1,17 +0,0 @@ -export class PlaceholderPattern { - readonly #prefix: string; - readonly #suffix: string; - - public constructor(prefix: string, suffix: string) { - this.#prefix = prefix; - this.#suffix = suffix; - } - - public get prefix() { - return this.#prefix; - } - - public get suffix() { - return this.#suffix; - } -} diff --git a/src/placeholder.ts b/src/placeholder.ts index 3b0cba6..ba2c078 100644 --- a/src/placeholder.ts +++ b/src/placeholder.ts @@ -1,11 +1,14 @@ import { AbortSubstitutionException, PlaceholderException } from "./exceptions.js"; -import { PlaceholderPattern } from "./pattern.js"; import { EMPTY as emptyResolver } from "./resolver/index.js"; import { VariableResolver } from "./resolver/types.js"; -import { StringSection } from "./string-section.js"; import { UnknownVariableHandler } from "./unknown-variable-handler/types.js"; import { isDefined, Nullable } from "./utils.js"; +export type PlaceholderPattern = { + prefix: string; + suffix: string; +}; + export type PlaceholderOptions = { pattern?: PlaceholderPattern; escapeChar?: string; @@ -14,13 +17,13 @@ export type PlaceholderOptions = { unknownVariableHandler?: Nullable; }; -const DEFAULT_OPTIONS: Required = { - pattern: new PlaceholderPattern("${", "}"), +const DEFAULT_OPTIONS: Required = Object.freeze({ + pattern: Object.freeze({ prefix: "${", suffix: "}" }), escapeChar: '\\', recursive: false, resolver: emptyResolver, unknownVariableHandler: null -}; +}); export class PlaceholderSubstitutor { readonly #pattern: PlaceholderPattern; @@ -108,7 +111,7 @@ class Substitutor { } const innerSubstitutor = new Substitutor(this.#substitutor, this.#input.subSection(this.#pos), true); - const variable = innerSubstitutor.replace(); + const variable = innerSubstitutor.replace().trim(); if (variable.length === 0) { throw new PlaceholderException(this.#input.getSection(), this.#pos, "Empty variable"); @@ -178,7 +181,7 @@ class Substitutor { return this.#substitutor.unknownVariableHandler(this.#substitutor, name); } catch (e: any) { if (e instanceof AbortSubstitutionException) { - throw new PlaceholderException(this.#input.getSection(), this.#pos, "Substitution aborted due to unknown variable", e); + throw new PlaceholderException(this.#input.getSection(), this.#pos, `Substitution aborted due to unknown variable: ${name}`, e); } throw new PlaceholderException(this.#input.getSection(), this.#pos, "An error occurred while handling unknown variable", e); @@ -188,3 +191,59 @@ class Substitutor { return null; } } + +class StringSection { + readonly #parent: string; + readonly #offset: number; + readonly #length: number; + + constructor(parent: string, offset: number = 0, length: number = parent.length - offset) { + if (offset < 0) { + throw new Error("offset < 0"); + } + + if (length < 0) { + throw new Error("length < 0"); + } + + if (offset + length > parent.length) { + throw new Error("offset + length > parent.length"); + } + + this.#parent = parent; + this.#offset = offset; + this.#length = length; + } + + public get length() { + return this.#length; + } + + public getSection() { + return this.substring(0); + } + + public subSection(offset: number = 0, length: number = this.#length - offset) { + return new StringSection(this.#parent, this.#offset + offset, length); + } + + public charAt(index: number) { + return this.#parent.charAt(this.#offset + index); + } + + public startsWith(prefix: string, offset: number = 0) { + return prefix.length <= (this.#length - offset) && this.#parent.startsWith(prefix, this.#offset + offset); + } + + public substring(beginIndex: number = 0, endIndex: number = this.#length - beginIndex) { + return this.#parent.substring(this.#offset + beginIndex, this.#offset + endIndex); + } + + public toJSON() { + return this.getSection(); + } + + public toString() { + return this.getSection(); + } +} diff --git a/src/string-section.ts b/src/string-section.ts deleted file mode 100644 index a4d24a2..0000000 --- a/src/string-section.ts +++ /dev/null @@ -1,55 +0,0 @@ -export class StringSection { - readonly #parent: string; - readonly #offset: number; - readonly #length: number; - - constructor(parent: string, offset: number = 0, length: number = parent.length - offset) { - if (offset < 0) { - throw new Error("offset < 0"); - } - - if (length < 0) { - throw new Error("length < 0"); - } - - if (offset + length > parent.length) { - throw new Error("offset + length > parent.length"); - } - - this.#parent = parent; - this.#offset = offset; - this.#length = length; - } - - public get length() { - return this.#length; - } - - public getSection() { - return this.substring(0); - } - - public subSection(offset: number = 0, length: number = this.#length - offset) { - return new StringSection(this.#parent, this.#offset + offset, length); - } - - public charAt(index: number) { - return this.#parent.charAt(this.#offset + index); - } - - public startsWith(prefix: string, offset: number = 0) { - return prefix.length <= (this.#length - offset) && this.#parent.startsWith(prefix, this.#offset + offset); - } - - public substring(beginIndex: number = 0, endIndex: number = this.#length - beginIndex) { - return this.#parent.substring(this.#offset + beginIndex, this.#offset + endIndex); - } - - public toJSON() { - return this.getSection(); - } - - public toString() { - return this.getSection(); - } -}