|
| 1 | +const dox = require('dox'); |
| 2 | + |
| 3 | +const formatStringForName = content => |
| 4 | + content.toString() |
| 5 | + .replace(/\.prototype|\(\)/g, ''); |
| 6 | + |
| 7 | +const formatStringForUID = content => |
| 8 | + content.toString() |
| 9 | + .toLowerCase() |
| 10 | + .replace(/[^\w\.]+/g, '-') |
| 11 | + .replace(/^-|-$/g, ''); |
| 12 | + |
| 13 | +const parser = (content, filename) => dox.parseComments(content, {'raw': true}).reduce((prev, curr) => { |
| 14 | + |
| 15 | + if (!curr.ignore && curr.ctx) { |
| 16 | + |
| 17 | + prev.push({ |
| 18 | + 'uid': formatStringForUID(`${filename}-${curr.ctx.string}`), |
| 19 | + 'isPrivate': curr.isPrivate, |
| 20 | + 'type': curr.ctx.type, |
| 21 | + 'name': formatStringForName(curr.ctx.string), |
| 22 | + 'description': curr.description.full, |
| 23 | + 'params': curr.tags.filter(tag => tag.type === 'param' && !tag.name.match(/\./)) |
| 24 | + .reduce((prev, curr) => { |
| 25 | + |
| 26 | + if (prev) { |
| 27 | + |
| 28 | + return `${prev}, ${curr.name.replace(/\[|\]/g, '')}`; |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + return `${curr.name.replace(/\[|\]/g, '')}`; |
| 33 | + |
| 34 | + }, ''), |
| 35 | + 'tags': { |
| 36 | + 'example': curr.tags.filter(tag => tag.type === 'example') |
| 37 | + .reduce((prev, curr) => { |
| 38 | + |
| 39 | + prev.push(curr.string); |
| 40 | + |
| 41 | + return prev; |
| 42 | + |
| 43 | + }, []), |
| 44 | + 'param': curr.tags.filter(tag => tag.type === 'param') |
| 45 | + .reduce((prev, curr) => { |
| 46 | + |
| 47 | + prev.push({ |
| 48 | + 'name': curr.name.replace(/\[|\]/g, ''), |
| 49 | + 'isOptional': curr.optional, |
| 50 | + 'types': curr.types, |
| 51 | + 'description': curr.description |
| 52 | + }); |
| 53 | + |
| 54 | + return prev; |
| 55 | + |
| 56 | + }, []), |
| 57 | + 'property': curr.tags.filter(tag => tag.type === 'property') |
| 58 | + .reduce((prev, curr) => { |
| 59 | + |
| 60 | + prev.push({ |
| 61 | + 'name': curr.name, |
| 62 | + 'types': curr.types, |
| 63 | + 'description': curr.description |
| 64 | + }); |
| 65 | + |
| 66 | + return prev; |
| 67 | + |
| 68 | + }, []), |
| 69 | + 'return': curr.tags.filter(tag => tag.type === 'return' || tag.type === 'returns') |
| 70 | + .reduce((prev, curr) => { |
| 71 | + |
| 72 | + prev.push({ |
| 73 | + 'types': curr.types, |
| 74 | + 'description': curr.description |
| 75 | + }); |
| 76 | + |
| 77 | + return prev; |
| 78 | + |
| 79 | + }, []) |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + return prev; |
| 86 | + |
| 87 | +}, []); |
| 88 | + |
| 89 | +module.exports = parser; |
0 commit comments