Skip to content

Commit

Permalink
refactor: rename RegexComponent to RegexElement (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski authored Dec 6, 2023
1 parent 3e02b07 commit b583a4f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
38 changes: 19 additions & 19 deletions src/compiler.ts
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 5 additions & 5 deletions src/quantifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/types-internal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Compilation
export type ComponentCompiler = (compiledChildren: string) => string;
export type CompilerMap = Record<string, ComponentCompiler>;
export type ElementCompiler = (compiledChildren: string) => string;
export type CompilerMap = Record<string, ElementCompiler>;
10 changes: 5 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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[];
};

0 comments on commit b583a4f

Please sign in to comment.