Skip to content

Commit

Permalink
feat(ProjectParser): add search method (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
RealShadowNova committed Sep 2, 2022
1 parent af2a828 commit 3d87c32
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 2 deletions.
117 changes: 116 additions & 1 deletion src/lib/structures/NamespaceParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { JSONOutput } from 'typedoc';
import { ReflectionKind } from '../types';
import { ReflectionKind, SearchResult } from '../types';
import { ClassParser } from './class-parser/';
import { ConstantParser } from './ConstantParser';
import { EnumParser } from './enum-parser';
Expand Down Expand Up @@ -78,6 +78,121 @@ export class NamespaceParser extends Parser {
this.typeAliases = typeAliases;
}

/**
* Search for a parser with a given query.
* @since 3.0.0
* @param query The query to search with.
* @returns An array of search results.
*/
public search(query: string): SearchResult[] {
const results: SearchResult[] = [];
const words = query.toLowerCase().split(/(#|.)/g);

for (const classParser of this.classes) {
if (classParser.name.toLowerCase().includes(words[0])) {
if (words.length === 1) {
results.push(classParser);

continue;
}

for (const methodParser of classParser.methods) {
if (methodParser.name.toLowerCase().includes(words[1])) {
if (words.length === 2) {
results.push(methodParser);

continue;
}
}
}

for (const propertyParser of classParser.properties) {
if (propertyParser.name.toLowerCase().includes(words[1])) {
results.push(propertyParser);

continue;
}
}
}
}

for (const constantParser of this.constants) {
if (constantParser.name.toLowerCase().includes(words[0])) {
results.push(constantParser);

continue;
}
}

for (const enumParser of this.enums) {
if (enumParser.name.toLowerCase().includes(words[0])) {
if (words.length === 1) {
results.push(enumParser);

continue;
}

for (const enumMemberParser of enumParser.properties) {
if (enumMemberParser.name.toLowerCase().includes(words[1])) {
results.push(enumMemberParser);

continue;
}
}
}
}

for (const functionParser of this.functions) {
if (functionParser.name.toLowerCase().includes(words[0])) {
results.push(functionParser);

continue;
}
}

for (const interfaceParser of this.interfaces) {
if (interfaceParser.name.toLowerCase().includes(words[0])) {
if (words.length === 1) {
results.push(interfaceParser);

continue;
}

for (const propertyParser of interfaceParser.properties) {
if (propertyParser.name.toLowerCase().includes(words[1])) {
results.push(propertyParser);

continue;
}
}
}
}

for (const namespaceParser of this.namespaces) {
if (namespaceParser.name.toLowerCase().includes(words[0])) {
if (words.length === 1) {
results.push(namespaceParser);

continue;
}

const subResults = namespaceParser.search(query.substring(words[0].length));

for (const subResult of subResults) results.push(subResult);
}
}

for (const typeAliasParser of this.typeAliases) {
if (typeAliasParser.name.toLowerCase().includes(words[0])) {
results.push(typeAliasParser);

continue;
}
}

return results;
}

/**
* Converts this parser to a JSON compatible format.
* @since 1.0.0
Expand Down
117 changes: 116 additions & 1 deletion src/lib/structures/ProjectParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { JSONOutput } from 'typedoc';
import { ReflectionKind } from '../types';
import { ReflectionKind, SearchResult } from '../types';
import { ClassParser } from './class-parser/';
import { ConstantParser } from './ConstantParser';
import { EnumParser } from './enum-parser';
Expand Down Expand Up @@ -131,6 +131,121 @@ export class ProjectParser {
}
}

/**
* Search for a parser with a given query.
* @since 3.0.0
* @param query The query to search with.
* @returns An array of search results.
*/
public search(query: string): SearchResult[] {
const results: SearchResult[] = [];
const words = query.toLowerCase().split(/(#|.)/g);

for (const classParser of this.classes) {
if (classParser.name.toLowerCase().includes(words[0])) {
if (words.length === 1) {
results.push(classParser);

continue;
}

for (const methodParser of classParser.methods) {
if (methodParser.name.toLowerCase().includes(words[1])) {
if (words.length === 2) {
results.push(methodParser);

continue;
}
}
}

for (const propertyParser of classParser.properties) {
if (propertyParser.name.toLowerCase().includes(words[1])) {
results.push(propertyParser);

continue;
}
}
}
}

for (const constantParser of this.constants) {
if (constantParser.name.toLowerCase().includes(words[0])) {
results.push(constantParser);

continue;
}
}

for (const enumParser of this.enums) {
if (enumParser.name.toLowerCase().includes(words[0])) {
if (words.length === 1) {
results.push(enumParser);

continue;
}

for (const enumMemberParser of enumParser.properties) {
if (enumMemberParser.name.toLowerCase().includes(words[1])) {
results.push(enumMemberParser);

continue;
}
}
}
}

for (const functionParser of this.functions) {
if (functionParser.name.toLowerCase().includes(words[0])) {
results.push(functionParser);

continue;
}
}

for (const interfaceParser of this.interfaces) {
if (interfaceParser.name.toLowerCase().includes(words[0])) {
if (words.length === 1) {
results.push(interfaceParser);

continue;
}

for (const propertyParser of interfaceParser.properties) {
if (propertyParser.name.toLowerCase().includes(words[1])) {
results.push(propertyParser);

continue;
}
}
}
}

for (const namespaceParser of this.namespaces) {
if (namespaceParser.name.toLowerCase().includes(words[0])) {
if (words.length === 1) {
results.push(namespaceParser);

continue;
}

const subResults = namespaceParser.search(query.substring(words[0].length));

for (const subResult of subResults) results.push(subResult);
}
}

for (const typeAliasParser of this.typeAliases) {
if (typeAliasParser.name.toLowerCase().includes(words[0])) {
results.push(typeAliasParser);

continue;
}
}

return results;
}

/**
* Converts this project to a JSON compatible format.
* @since 1.0.0
Expand Down
25 changes: 25 additions & 0 deletions src/lib/types/SearchResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { ClassConstructorParser, ClassMethodParser, ClassParser, ClassPropertyParser } from '../structures/class-parser';
import type { ConstantParser } from '../structures/ConstantParser';
import type { EnumParser, EnumPropertyParser } from '../structures/enum-parser';
import type { FunctionParser } from '../structures/FunctionParser';
import type { InterfaceParser, InterfacePropertyParser } from '../structures/interface-parser';
import type { ParameterParser, SignatureParser, TypeParameterParser } from '../structures/misc';
import type { NamespaceParser } from '../structures/NamespaceParser';
import type { TypeAliasParser } from '../structures/TypeAliasParser';

export type SearchResult =
| ClassParser
| ClassConstructorParser
| ClassMethodParser
| SignatureParser
| TypeParameterParser
| ParameterParser
| ClassPropertyParser
| ConstantParser
| EnumParser
| EnumPropertyParser
| FunctionParser
| InterfaceParser
| InterfacePropertyParser
| NamespaceParser
| TypeAliasParser;
1 change: 1 addition & 0 deletions src/lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './ReflectionKind';
export * from './SearchResult';

0 comments on commit 3d87c32

Please sign in to comment.