Skip to content

Commit b21bafe

Browse files
refactor: extract duplicated reducer (#63)
* refactor: extract duplicated reducer * 🤖 Documentation auto-update --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 3475264 commit b21bafe

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Parameters:
167167
- `params.entries`: The entries of the documentation (functions, constants and classes).
168168
- `params.options`: Optional configuration to render the Markdown content. See `types.ts` for details.
169169

170-
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/lib/markdown.ts#L364)
170+
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/lib/markdown.ts#L360)
171171

172172
### :gear: generateDocumentation
173173

src/lib/markdown.ts

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -209,36 +209,32 @@ const toMarkdown = ({
209209
headingLevel: MarkdownHeadingLevel | '####';
210210
docType: 'Constant' | 'Function' | 'Method' | 'Property' | 'Type' | 'Enum';
211211
} & Pick<MarkdownOptions, 'emoji'>): string => {
212-
const jsDocsToReturnType = (jsDocs: JSDocTagInfo[]): string => {
213-
const returns: JSDocTagInfo[] = jsDocs.filter(({name}: JSDocTagInfo) => name === 'returns');
214-
const texts: Array<SymbolDisplayPart[] | undefined> = returns.map(({text}) => text);
215-
const returnType = texts.reduce(
216-
(acc: SymbolDisplayPart[][], values: SymbolDisplayPart[] | undefined) => {
217-
if (values === undefined) {
218-
return acc;
219-
}
212+
const jsDocsToSymbolDisplayParts = ({
213+
jsDocs = [],
214+
tagInfoName
215+
}: {
216+
jsDocs?: JSDocTagInfo[];
217+
tagInfoName: 'returns' | 'param';
218+
}): SymbolDisplayPart[][] => {
219+
const tags = jsDocs.filter(({name}: JSDocTagInfo) => name === tagInfoName);
220+
const texts = tags.map(({text}) => text);
221+
222+
return texts.reduce<SymbolDisplayPart[][]>((acc, values) => {
223+
if (values === undefined) {
224+
return acc;
225+
}
220226

221-
return [...acc, values];
222-
},
223-
[]
224-
);
227+
return [...acc, values];
228+
}, []);
229+
};
225230

226-
return returnType.map((parts) => parts.map(({text}) => text).join('')).join(' ');
231+
const jsDocsToReturnType = (jsDocs?: JSDocTagInfo[]): string => {
232+
const returns = jsDocsToSymbolDisplayParts({jsDocs, tagInfoName: 'returns'});
233+
return returns.map((parts) => parts.map(({text}) => text).join('')).join(' ');
227234
};
228-
const jsDocsToParams = (jsDocs: JSDocTagInfo[]): Params[] => {
229-
const params: JSDocTagInfo[] = jsDocs.filter(({name}: JSDocTagInfo) => name === 'param');
230-
const texts: Array<SymbolDisplayPart[] | undefined> = params.map(({text}) => text);
231-
232-
const parts: SymbolDisplayPart[][] = texts.reduce(
233-
(acc: SymbolDisplayPart[][], values: SymbolDisplayPart[] | undefined) => {
234-
if (values === undefined) {
235-
return acc;
236-
}
237235

238-
return [...acc, values];
239-
},
240-
[]
241-
);
236+
const jsDocsToParams = (jsDocs?: JSDocTagInfo[]): Params[] => {
237+
const params = jsDocsToSymbolDisplayParts({jsDocs, tagInfoName: 'param'});
242238

243239
const toParam = (parts: SymbolDisplayPart[]): Params | undefined => {
244240
if (parts.find(({kind, text}) => kind === 'parameterName' && text !== '') === undefined) {
@@ -251,7 +247,7 @@ const toMarkdown = ({
251247
return {name, documentation};
252248
};
253249

254-
return parts.map(toParam).filter((param) => param !== undefined) as Params[];
250+
return params.map(toParam).filter((param) => param !== undefined) as Params[];
255251
};
256252
const jsDocsToExamples = (jsDocs: JSDocTagInfo[]): string[] => {
257253
const examples: JSDocTagInfo[] = jsDocs.filter(({name}: JSDocTagInfo) => name === 'example');
@@ -267,8 +263,8 @@ const toMarkdown = ({
267263
name,
268264
type: type ?? '',
269265
documentation: documentation ?? '',
270-
params: [...toParams(parameters), ...jsDocsToParams(jsDocs ?? [])],
271-
returnType: jsDocsToReturnType(jsDocs ?? []),
266+
params: [...toParams(parameters), ...jsDocsToParams(jsDocs)],
267+
returnType: jsDocsToReturnType(jsDocs),
272268
examples: [...jsDocsToExamples(jsDocs ?? [])],
273269
url
274270
})

0 commit comments

Comments
 (0)