From b583a4f7e1dd1cf46b01b9824c30fc2167a3f5b5 Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Wed, 6 Dec 2023 12:11:11 +0100 Subject: [PATCH] refactor: rename `RegexComponent` to `RegexElement` (#12) --- src/compiler.ts | 38 +++++++++++++++++++------------------- src/quantifiers.ts | 10 +++++----- src/types-internal.ts | 4 ++-- src/types.ts | 10 +++++----- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index fba144f..ac3de1f 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -1,42 +1,42 @@ -import type { RegexComponent } from './types'; +import type { RegexElement } from './types'; import { compilers as quantifiers } from './quantifiers'; /** - * Generate RegExp object from components. + * Generate RegExp object for elements. * - * @param components + * @param elements * @returns */ -export function buildRegex(...components: RegexComponent[]): RegExp { - const pattern = compileList(components); +export function buildRegex(...elements: RegexElement[]): RegExp { + const pattern = compileList(elements); return new RegExp(pattern); } /** - * Generate regex pattern from components. - * @param components + * Generate regex pattern for elements. + * @param elements * @returns */ -export function buildPattern(...components: RegexComponent[]): string { - return compileList(components); +export function buildPattern(...elements: RegexElement[]): string { + return compileList(elements); } // Recursive compilation -function compileList(components: RegexComponent[]): string { - return components.map((c) => compileSingle(c)).join(''); +function compileList(elements: RegexElement[]): string { + return elements.map((c) => compileSingle(c)).join(''); } -function compileSingle(component: RegexComponent): string { - if (typeof component === 'string') { - return component; +function compileSingle(elements: RegexElement): string { + if (typeof elements === 'string') { + return elements; } - const componentCompiler = quantifiers[component.type]; - if (!componentCompiler) { - throw new Error(`Unknown component type ${component.type}`); + const elementCompiler = quantifiers[elements.type]; + if (!elementCompiler) { + throw new Error(`Unknown elements type ${elements.type}`); } - const children = compileList(component.children); - return componentCompiler(children); + const children = compileList(elements.children); + return elementCompiler(children); } diff --git a/src/quantifiers.ts b/src/quantifiers.ts index 7e5ed9b..4007990 100644 --- a/src/quantifiers.ts +++ b/src/quantifiers.ts @@ -2,34 +2,34 @@ import type { One, OneOrMore, Optionally, - RegexComponent, + RegexElement, ZeroOrMore, } from './types'; import type { CompilerMap } from './types-internal'; import { wrapGroup } from './utils'; -export function oneOrMore(...children: RegexComponent[]): OneOrMore { +export function oneOrMore(...children: RegexElement[]): OneOrMore { return { type: 'oneOrMore', children, }; } -export function optionally(...children: RegexComponent[]): Optionally { +export function optionally(...children: RegexElement[]): Optionally { return { type: 'optionally', children, }; } -export function one(...children: RegexComponent[]): One { +export function one(...children: RegexElement[]): One { return { type: 'one', children, }; } -export function zeroOrMore(...children: RegexComponent[]): ZeroOrMore { +export function zeroOrMore(...children: RegexElement[]): ZeroOrMore { return { type: 'zeroOrMore', children, diff --git a/src/types-internal.ts b/src/types-internal.ts index 2859963..9d41c14 100644 --- a/src/types-internal.ts +++ b/src/types-internal.ts @@ -1,3 +1,3 @@ // Compilation -export type ComponentCompiler = (compiledChildren: string) => string; -export type CompilerMap = Record; +export type ElementCompiler = (compiledChildren: string) => string; +export type CompilerMap = Record; diff --git a/src/types.ts b/src/types.ts index be008b5..dd6f023 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,24 +1,24 @@ -export type RegexComponent = string | RegexQuantifier; +export type RegexElement = string | RegexQuantifier; export type RegexQuantifier = One | OneOrMore | Optionally | ZeroOrMore; // Quantifiers export type One = { type: 'one'; - children: RegexComponent[]; + children: RegexElement[]; }; export type OneOrMore = { type: 'oneOrMore'; - children: RegexComponent[]; + children: RegexElement[]; }; export type Optionally = { type: 'optionally'; - children: RegexComponent[]; + children: RegexElement[]; }; export type ZeroOrMore = { type: 'zeroOrMore'; - children: RegexComponent[]; + children: RegexElement[]; };