UNPKG

1.27 kBJavaScriptView Raw
1/* jshint node: true */
2'use strict'
3
4const Context = require('./context')
5
6/**
7 * Class: A documentation extractor.
8 *
9 * e = new Extractor()
10 * e.push('data.js', "...string contents of file")
11 */
12
13class Extractor {
14 constructor (options) {
15 this.output = []
16 this.blocks = []
17 this.options = options || {}
18 }
19
20 push (fname, src) {
21 const self = this
22 const c = new Context(fname, src)
23 c.process()
24 c.blocks.forEach(function (block) {
25 if (self.isBlockAllowed(block)) {
26 self.blocks.push(block)
27 }
28 })
29 }
30
31 isBlockAllowed (block) {
32 if (this.options.excludeTags &&
33 hasIntersection(this.options.excludeTags, block.tags)) {
34 return false
35 }
36
37 return true
38 }
39
40 toJson () {
41 return { blocks: this.blocks }
42 }
43}
44
45/**
46 * Internal: checks two arrays to see if they have common elements.
47 * Used to check if a certain tag (or tags) is in a list.
48 *
49 * hasIntersection(['private', 'internal'], ['private'])
50 * // => true
51 */
52
53function hasIntersection (list, other) {
54 if (!Array.isArray(list) || !Array.isArray(other)) return
55 for (let i = 0, len = list.length; i < len; i++) {
56 let item = list[i]
57 if (~other.indexOf(item)) return true
58 }
59 return false
60}
61
62module.exports = Extractor