Skip to content

Commit

Permalink
refactor(pattern): drop unused functions
Browse files Browse the repository at this point in the history
These functions were previously used in the now-removed NFA matcher. There is no breaking change as these functions were internal and are not exported from the library entrypoint.
  • Loading branch information
jo3-l committed Jul 16, 2024
1 parent 9430168 commit cdd7501
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 243 deletions.
60 changes: 0 additions & 60 deletions src/pattern/Simplifier.ts

This file was deleted.

38 changes: 1 addition & 37 deletions src/pattern/Util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { LiteralNode, Node, ParsedPattern } from './Nodes';
import type { Node, ParsedPattern } from './Nodes';
import { SyntaxKind } from './Nodes';
import type { SimpleNode } from './Simplifier';

export function potentiallyMatchesEmptyString(pattern: ParsedPattern) {
return pattern.nodes.every((node) => node.kind === SyntaxKind.Optional);
Expand Down Expand Up @@ -36,38 +35,3 @@ export function getRegExpStringForNode(node: Node): string {
return `.`;
}
}

export function computePatternMatchLength(nodes: SimpleNode[]) {
return nodes.reduce((total, node) => total + (node.kind === SyntaxKind.Wildcard ? 1 : node.chars.length), 0);
}

export function groupByNodeType(nodes: SimpleNode[]) {
let i = 0;
const groups: NodeGroup[] = [];
while (i < nodes.length) {
const node = nodes[i];
if (node.kind === SyntaxKind.Literal) {
const literals: LiteralNode[] = [];
while (i < nodes.length && nodes[i].kind === SyntaxKind.Literal) literals.push(nodes[i++] as LiteralNode);
groups.push({ literals, isLiteralGroup: true });
} else {
const mark = i;
while (i < nodes.length && nodes[i].kind === SyntaxKind.Wildcard) i++;
groups.push({ wildcardCount: i - mark, isLiteralGroup: false });
}
}

return groups;
}

export type NodeGroup = LiteralGroup | WildcardGroup;

export interface LiteralGroup {
isLiteralGroup: true;
literals: LiteralNode[];
}

export interface WildcardGroup {
isLiteralGroup: false;
wildcardCount: number;
}
78 changes: 0 additions & 78 deletions test/pattern/Simplifier.test.ts

This file was deleted.

69 changes: 1 addition & 68 deletions test/pattern/Util.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import type { LiteralNode, OptionalNode } from '../../src/pattern/Nodes';
import { SyntaxKind } from '../../src/pattern/Nodes';
import type { SimpleNode } from '../../src/pattern/Simplifier';
import {
compilePatternToRegExp,
computePatternMatchLength,
getRegExpStringForNode,
groupByNodeType,
potentiallyMatchesEmptyString,
} from '../../src/pattern/Util';
import { compilePatternToRegExp, getRegExpStringForNode, potentiallyMatchesEmptyString } from '../../src/pattern/Util';
import { CharacterIterator } from '../../src/util/CharacterIterator';

function toLiteralNode(str: string): LiteralNode {
Expand Down Expand Up @@ -123,63 +116,3 @@ describe('getRegExpStringForNode()', () => {
});
});
});

describe('computePatternMatchLength()', () => {
it('should return 0 if given an empty array', () => {
expect(computePatternMatchLength([])).toBe(0);
});

it('should return the total number of chars in literal nodes plus 1 for each wildcard node', () => {
const nodes: SimpleNode[] = [
{ kind: SyntaxKind.Literal, chars: [0, 0] },
{ kind: SyntaxKind.Wildcard },
{ kind: SyntaxKind.Literal, chars: [0, 0, 0] },
];
expect(computePatternMatchLength(nodes)).toBe(6);
});
});

describe('groupByNodeType()', () => {
it('should return an empty array if nodes=[]', () => {
expect(groupByNodeType([])).toStrictEqual([]);
});

it('should return one literal group if there are only literal nodes in the input', () => {
const nodes: SimpleNode[] = [
{ kind: SyntaxKind.Literal, chars: [1, 2, 3] },
{ kind: SyntaxKind.Literal, chars: [2, 3, 4] },
];
expect(groupByNodeType(nodes)).toStrictEqual([{ isLiteralGroup: true, literals: nodes }]);
});

it('should return one wildcard group if there are only wildcards in the input', () => {
const nodes: SimpleNode[] = [
{ kind: SyntaxKind.Wildcard },
{ kind: SyntaxKind.Wildcard },
{ kind: SyntaxKind.Wildcard },
];
expect(groupByNodeType(nodes)).toStrictEqual([{ isLiteralGroup: false, wildcardCount: 3 }]);
});

it('should group literals and wildcards together', () => {
const literal0: LiteralNode = { kind: SyntaxKind.Literal, chars: [1, 2, 3] };
const literal1: LiteralNode = { kind: SyntaxKind.Literal, chars: [2, 3, 4] };
const literal2: LiteralNode = { kind: SyntaxKind.Literal, chars: [3, 4, 5] };
const nodes: SimpleNode[] = [
{ kind: SyntaxKind.Wildcard },
literal0,
literal1,
{ kind: SyntaxKind.Wildcard },
literal2,
{ kind: SyntaxKind.Wildcard },
{ kind: SyntaxKind.Wildcard },
];
expect(groupByNodeType(nodes)).toStrictEqual([
{ isLiteralGroup: false, wildcardCount: 1 },
{ isLiteralGroup: true, literals: [literal0, literal1] },
{ isLiteralGroup: false, wildcardCount: 1 },
{ isLiteralGroup: true, literals: [literal2] },
{ isLiteralGroup: false, wildcardCount: 2 },
]);
});
});

0 comments on commit cdd7501

Please sign in to comment.