|
3 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
5 | 5 | */ |
6 | | -import { EMPTY_TAG } from "../const"; |
7 | | -import { DataObject, FileObject, Method, Path } from "../types"; |
8 | | -import { parseDocument } from "./documentParser"; |
| 6 | +import { EMPTY_TAG } from '../const'; |
| 7 | +import { DataObject, FileObject, Method, Path } from '../types'; |
| 8 | +import { parseDocument } from './documentParser'; |
9 | 9 |
|
10 | 10 | export function divideIntoDocumentsByTag(data: DataObject): FileObject[] { |
11 | | - const tags: (string | undefined)[] = getAllTagsInDataObject(data); |
| 11 | + const tags: (string | undefined)[] = getAllTagsInDataObject(data); |
12 | 12 |
|
13 | | - const documentsByTag = tags.map((tag) => { |
14 | | - const foundTagInfo = data.tags?.find( |
15 | | - (item: { name: string; description: string }) => item.name === tag |
16 | | - ); |
17 | | - return { |
18 | | - tagName: tag, |
19 | | - document: generatesDocumentByTag(data, tag), |
20 | | - description: foundTagInfo?.description || undefined, |
21 | | - }; |
22 | | - }); |
| 13 | + const documentsByTag = tags.map((tag) => { |
| 14 | + const foundTagInfo = data.tags?.find((item: { name: string; description: string }) => item.name === tag); |
| 15 | + return { |
| 16 | + tagName: tag, |
| 17 | + document: generatesDocumentByTag(data, tag), |
| 18 | + description: foundTagInfo?.description || undefined, |
| 19 | + }; |
| 20 | + }); |
23 | 21 |
|
24 | | - return documentsByTag; |
| 22 | + return documentsByTag; |
25 | 23 | } |
26 | 24 |
|
27 | 25 | function getAllTagsInDataObject(data: DataObject): (string | undefined)[] { |
28 | | - const tags: (string | undefined)[] = []; |
29 | | - Object.keys(data.paths) |
30 | | - .map((path) => data.paths[path]) |
31 | | - .map((operations) => { |
32 | | - return Object.keys(operations) |
33 | | - .map((operationKey) => operations[operationKey]) |
34 | | - .forEach((method: Method) => { |
35 | | - if (!method.tags) { |
36 | | - if (!tags.includes(EMPTY_TAG)) { |
37 | | - tags.push(EMPTY_TAG); |
38 | | - } |
39 | | - } |
40 | | - method.tags && |
41 | | - method.tags.forEach((tag: string) => { |
42 | | - if (!tags.includes(tag)) { |
43 | | - tags.push(tag); |
44 | | - } |
45 | | - }); |
46 | | - }); |
47 | | - }); |
48 | | - return tags; |
| 26 | + const tags: (string | undefined)[] = []; |
| 27 | + Object.keys(data.paths) |
| 28 | + .map((path) => data.paths[path]) |
| 29 | + .map((operations) => { |
| 30 | + return Object.keys(operations) |
| 31 | + .map((operationKey) => operations[operationKey]) |
| 32 | + .forEach((method: Method) => { |
| 33 | + if (!method.tags) { |
| 34 | + if (!tags.includes(EMPTY_TAG)) { |
| 35 | + tags.push(EMPTY_TAG); |
| 36 | + } |
| 37 | + } |
| 38 | + method.tags && |
| 39 | + method.tags.forEach((tag: string) => { |
| 40 | + if (!tags.includes(tag)) { |
| 41 | + tags.push(tag); |
| 42 | + } |
| 43 | + }); |
| 44 | + }); |
| 45 | + }); |
| 46 | + return tags; |
49 | 47 | } |
50 | 48 |
|
51 | | -function generatesDocumentByTag( |
52 | | - data: DataObject, |
53 | | - tag: string | undefined |
54 | | -): DataObject { |
55 | | - const document = parseDocument(JSON.parse(JSON.stringify(data))); |
56 | | - document.paths = document.paths |
57 | | - .filter((path: Path) => { |
58 | | - if (tag === EMPTY_TAG) { |
59 | | - return path.methods.filter((method: Method) => !method.tags).length > 0; |
60 | | - } |
61 | | - return ( |
62 | | - path.methods.filter( |
63 | | - (method: Method) => method.tags && method.tags.includes(tag) |
64 | | - ).length > 0 |
65 | | - ); |
66 | | - }) |
67 | | - .map((path: Path) => ({ |
68 | | - ...path, |
69 | | - methods: |
70 | | - tag === EMPTY_TAG |
71 | | - ? path.methods.filter((method: Method) => !method.tags) |
72 | | - : path.methods.filter( |
73 | | - (method: Method) => method.tags && method.tags.includes(tag) |
74 | | - ), |
75 | | - })); |
76 | | - return document; |
| 49 | +function generatesDocumentByTag(data: DataObject, tag: string | undefined): DataObject { |
| 50 | + const document = parseDocument(JSON.parse(JSON.stringify(data))); |
| 51 | + document.paths = document.paths |
| 52 | + .filter((path: Path) => { |
| 53 | + if (tag === EMPTY_TAG) { |
| 54 | + return path.methods.filter((method: Method) => !method.tags).length > 0; |
| 55 | + } |
| 56 | + return path.methods.filter((method: Method) => method.tags && method.tags.includes(tag)).length > 0; |
| 57 | + }) |
| 58 | + .map((path: Path) => ({ |
| 59 | + ...path, |
| 60 | + methods: |
| 61 | + tag === EMPTY_TAG |
| 62 | + ? path.methods.filter((method: Method) => !method.tags) |
| 63 | + : path.methods.filter((method: Method) => method.tags && method.tags.includes(tag)), |
| 64 | + })); |
| 65 | + return document; |
77 | 66 | } |
0 commit comments