Skip to content

Commit 6463698

Browse files
authored
Merge pull request #199 from docsbydoxdox/hotfix/move-pattern-match-util-to-core
[hotfix] Move pattern match util to core
2 parents 899bf56 + 51066e4 commit 6463698

File tree

4 files changed

+75
-36
lines changed

4 files changed

+75
-36
lines changed

packages/doxdox-core/src/utils.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getRootDirPath,
1010
isDirectory,
1111
isFile,
12+
multiLinePatternMatch,
1213
parseConfigFromCLI,
1314
parseIgnoreConfig,
1415
slugify
@@ -98,6 +99,36 @@ describe('utils', () => {
9899
});
99100
});
100101

102+
describe('multiLinePatternMatch', () => {
103+
it('find pattern in content', async () => {
104+
expect(
105+
multiLinePatternMatch(
106+
`/**
107+
* Get the current working directory.
108+
*
109+
* @return {string} Directory path.
110+
* @public
111+
*/
112+
113+
function getCurrentWorkingDirectory() {}`,
114+
`/**
115+
* Get the current working directory.
116+
*
117+
* @return {string} Directory path.
118+
* @public
119+
*/`
120+
)
121+
).toEqual(
122+
expect.objectContaining({ matched: true, start: 0, end: 6 })
123+
);
124+
});
125+
it('fail to find pattern', async () => {
126+
expect(multiLinePatternMatch('', '// @ts-ignore')).toEqual(
127+
expect.objectContaining({ matched: false })
128+
);
129+
});
130+
});
131+
101132
describe('parseConfigFromCLI', () => {
102133
it('parse config', () => {
103134
expect(

packages/doxdox-core/src/utils.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,49 @@ export const isFile = async (path: string): Promise<boolean> => {
145145
}
146146
};
147147

148+
/**
149+
* Return information about pattern if found within contents.
150+
*
151+
* @param {string} [content] Full contents to search for pattern in.
152+
* @param {string} [pattern] Pattern to look for in contents.
153+
* @return {{ start?: number; end?: number; matched: boolean }}
154+
* @public
155+
*/
156+
157+
export const multiLinePatternMatch = (
158+
content: string,
159+
pattern: string
160+
): { start?: number; end?: number; matched: boolean } => {
161+
const matched = content.includes(pattern);
162+
163+
if (!matched) {
164+
return { matched };
165+
}
166+
167+
const contentLines = content.split(/\r?\n/);
168+
const patternLines = pattern.split(/\r?\n/);
169+
170+
for (let i = 0; i < contentLines.length; i += 1) {
171+
if (contentLines[i] === patternLines[0]) {
172+
const contentGroup = contentLines
173+
.slice(i, i + patternLines.length)
174+
.join('\n');
175+
176+
const patternGroup = patternLines.join('\n');
177+
178+
if (contentGroup === patternGroup) {
179+
return {
180+
start: i,
181+
end: i + patternLines.length,
182+
matched
183+
};
184+
}
185+
}
186+
}
187+
188+
return { matched: false };
189+
};
190+
148191
/**
149192
* Parse config key/value pairs from raw CLI flags.
150193
*

packages/doxdox-parser-custom/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import { promises as fs } from 'fs';
22

33
import { join } from 'path';
44

5-
import { File, Method, slugify } from 'doxdox-core';
5+
import { File, Method, multiLinePatternMatch, slugify } from 'doxdox-core';
66

77
import { parse as commentParse } from 'comment-parser';
88

9-
import { multiLinePatternMatch } from './utils.js';
10-
119
const JSDOC_PATTERN = /[ \t]*\/\*\*\s*\n?([^*]*(\*[^/])?)*\*\//g;
1210

1311
const IDENTIFIER_PATTERNS = [

packages/doxdox-parser-custom/src/utils.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)