Skip to content

Commit e70a5d7

Browse files
committed
Moved multiLinePatternMatch to core utils.
1 parent 899bf56 commit e70a5d7

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

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)