comment-parser 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -0
- package/README.md +15 -0
- package/package.json +1 -1
- package/stringifier.js +17 -17
- package/tests/stringify.spec.js +15 -2
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -160,6 +160,21 @@ This would produce following:
|
|
|
160
160
|
}]
|
|
161
161
|
```
|
|
162
162
|
|
|
163
|
+
## Stringifying
|
|
164
|
+
|
|
165
|
+
One may also convert `comment-parser` JSON structures back into strings using
|
|
166
|
+
the `stringify` method (`stringify(o:(Object|Array) [, opts:Object]):String`).
|
|
167
|
+
|
|
168
|
+
This method accepts the JSON as its first argument and an optional options
|
|
169
|
+
object with an `indent` property set to either a string or a number that
|
|
170
|
+
will be used to determine the number of spaces of indent. The indent of the
|
|
171
|
+
start of the doc block will be one space less than the indent of each line of
|
|
172
|
+
asterisks for the sake of alignment as per usual practice.
|
|
173
|
+
|
|
174
|
+
The `stringify` export delegates to the specialized methods `stringifyBlocks`,
|
|
175
|
+
`stringifyBlock`, and `stringifyTag`, which are available on the `stringify`
|
|
176
|
+
function object.
|
|
177
|
+
|
|
163
178
|
## Packaging
|
|
164
179
|
|
|
165
180
|
`comment-parser` is CommonJS module and was primarely designed to be used with Node. Module `index.js` includes stream and file functionality. Use prser-only module in browser `comment-parser/parse.js`
|
package/package.json
CHANGED
package/stringifier.js
CHANGED
|
@@ -4,13 +4,28 @@ const getIndent = (indent) => {
|
|
|
4
4
|
return typeof indent === 'number' ? ' '.repeat(indent) : indent
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
+
module.exports = exports = function stringify (arg, opts) {
|
|
8
|
+
if (Array.isArray(arg)) {
|
|
9
|
+
return stringifyBlocks(arg, opts)
|
|
10
|
+
}
|
|
11
|
+
if (arg && typeof arg === 'object') {
|
|
12
|
+
if ('tag' in arg) {
|
|
13
|
+
return stringifyTag(arg, opts)
|
|
14
|
+
}
|
|
15
|
+
if ('tags' in arg) {
|
|
16
|
+
return stringifyBlock(arg, opts)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
throw new TypeError('Unexpected argument passed to `stringify`.')
|
|
20
|
+
}
|
|
21
|
+
|
|
7
22
|
const stringifyBlocks = exports.stringifyBlocks = function stringifyBlocks (
|
|
8
23
|
blocks, { indent = '' } = {}
|
|
9
24
|
) {
|
|
10
25
|
const indnt = getIndent(indent)
|
|
11
26
|
return blocks.reduce((s, block) => {
|
|
12
27
|
return s + stringifyBlock(block, { indent })
|
|
13
|
-
}, '/**\n') + indnt + '*/'
|
|
28
|
+
}, (indnt ? indnt.slice(0, -1) : '') + '/**\n') + indnt + '*/'
|
|
14
29
|
}
|
|
15
30
|
|
|
16
31
|
const stringifyBlock = exports.stringifyBlock = function stringifyBlock (
|
|
@@ -18,7 +33,7 @@ const stringifyBlock = exports.stringifyBlock = function stringifyBlock (
|
|
|
18
33
|
) {
|
|
19
34
|
// block.line
|
|
20
35
|
const indnt = getIndent(indent)
|
|
21
|
-
return
|
|
36
|
+
return (block.description ? `${indnt}* ${block.description}\n${indnt}*\n` : '') +
|
|
22
37
|
block.tags.reduce((s, tag) => {
|
|
23
38
|
return s + stringifyTag(tag, { indent })
|
|
24
39
|
}, '')
|
|
@@ -40,18 +55,3 @@ const stringifyTag = exports.stringifyTag = function stringifyTag (
|
|
|
40
55
|
}` : '') +
|
|
41
56
|
(description ? ` ${description.replace(/\n/g, '\n' + indnt + '* ')}` : '') + '\n'
|
|
42
57
|
}
|
|
43
|
-
|
|
44
|
-
module.exports = function stringify (arg, opts) {
|
|
45
|
-
if (Array.isArray(arg)) {
|
|
46
|
-
return stringifyBlocks(arg, opts)
|
|
47
|
-
}
|
|
48
|
-
if (arg && typeof arg === 'object') {
|
|
49
|
-
if ('tag' in arg) {
|
|
50
|
-
return stringifyTag(arg, opts)
|
|
51
|
-
}
|
|
52
|
-
if ('tags' in arg) {
|
|
53
|
-
return stringifyBlock(arg, opts)
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
throw new TypeError('Unexpected argument passed to `stringify`.')
|
|
57
|
-
}
|
package/tests/stringify.spec.js
CHANGED
|
@@ -26,7 +26,7 @@ describe('Comment stringifying', function () {
|
|
|
26
26
|
})
|
|
27
27
|
|
|
28
28
|
it('should stringify indented doc block with description', function () {
|
|
29
|
-
const expected =
|
|
29
|
+
const expected = ` /**
|
|
30
30
|
* Singleline or multiline description text. Line breaks are preserved.
|
|
31
31
|
*
|
|
32
32
|
* @some-tag {Type} name Singleline or multiline description text
|
|
@@ -46,7 +46,7 @@ describe('Comment stringifying', function () {
|
|
|
46
46
|
})
|
|
47
47
|
|
|
48
48
|
it('should stringify numeric indented doc block with description', function () {
|
|
49
|
-
const expected =
|
|
49
|
+
const expected = ` /**
|
|
50
50
|
* Singleline or multiline description text. Line breaks are preserved.
|
|
51
51
|
*
|
|
52
52
|
* @some-tag {Type} name Singleline or multiline description text
|
|
@@ -64,4 +64,17 @@ describe('Comment stringifying', function () {
|
|
|
64
64
|
|
|
65
65
|
expect(stringified).to.eq(expected)
|
|
66
66
|
})
|
|
67
|
+
|
|
68
|
+
it('should stringify numeric indented doc block without description', function () {
|
|
69
|
+
const expected = ` /**
|
|
70
|
+
* @param Foo
|
|
71
|
+
*/`
|
|
72
|
+
const parsed = parser(expected)
|
|
73
|
+
|
|
74
|
+
expect(parsed).to.be.an('array')
|
|
75
|
+
|
|
76
|
+
const stringified = parser.stringify(parsed, { indent: 4 })
|
|
77
|
+
|
|
78
|
+
expect(stringified).to.eq(expected)
|
|
79
|
+
})
|
|
67
80
|
})
|