Code coverage report for lib/flatten.js

Statements: 100% (42 / 42)      Branches: 93.75% (15 / 16)      Functions: 100% (21 / 21)      Lines: 100% (42 / 42)      Ignored: none     

All files » lib/ » flatten.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167    36   36 44     36 40     36 46       46 5       36                                               2     14 7   14     162 88   162     9 6   9     96 96   96     7 4   7     16 11   16     1     3     5     1     3     2     1     9                                                                                                     36 331   331 463     331    
'use strict';
 
var extend = require('extend');
 
function flattenName(result, tag) {
  result[tag.title] = tag.name;
}
 
function flattenDescription(result, tag) {
  result[tag.title] = tag.description;
}
 
function flattenTypedName(result, tag) {
  result[tag.title] = {
    name: tag.name
  };
 
  if (tag.type) {
    result[tag.title].type = tag.type;
  }
}
 
var flatteners = {
  'name': flattenName,
  'function': flattenName,
  'mixin': flattenName,
  'alias': flattenName,
  'memberof': flattenDescription,
  'version': flattenDescription,
  'since': flattenDescription,
  'copyright': flattenDescription,
  'author': flattenDescription,
  'license': flattenDescription,
  'classdesc': flattenDescription,
  'lends': flattenDescription,
  'event': flattenDescription,
  'external': flattenDescription,
  'file': flattenDescription,
  'callback': flattenDescription,
  'class': flattenTypedName,
  'constant': flattenTypedName,
  'member': flattenTypedName,
  'module': flattenTypedName,
  'namespace': flattenTypedName,
  'typedef': flattenTypedName,
  'kind': function (result, tag) {
    result.kind = tag.kind;
  },
  'property': function (result, tag) {
    if (!result.properties) {
      result.properties = [];
    }
    result.properties.push(tag);
  },
  'param': function (result, tag) {
    if (!result.params) {
      result.params = [];
    }
    result.params.push(tag);
  },
  'throws': function (result, tag) {
    if (!result.throws) {
      result.throws = [];
    }
    result.throws.push(tag);
  },
  'returns': function (result, tag) {
    Eif (!result.returns) {
      result.returns = [];
    }
    result.returns.push(tag);
  },
  'augments': function (result, tag) {
    if (!result.augments) {
      result.augments = [];
    }
    result.augments.push(tag);
  },
  'example': function (result, tag) {
    if (!result.examples) {
      result.examples = [];
    }
    result.examples.push(tag.description);
  },
  'global': function (result) {
    result.scope = 'global';
  },
  'static': function (result) {
    result.scope = 'static';
  },
  'instance': function (result) {
    result.scope = 'instance';
  },
  'inner': function (result) {
    result.scope = 'inner';
  },
  'access': function (result, tag) {
    result.access = tag.access;
  },
  'public': function (result) {
    result.access = 'public';
  },
  'protected': function (result) {
    result.access = 'protected';
  },
  'private': function (result) {
    result.access = 'private';
  }
};
 
/**
 * Flattens tags in an opinionated way.
 *
 * The following tags are assumed to be singletons, and are flattened
 * to a top-level property on the result whose value is extracted from
 * the tag:
 *
 *  * `@name`
 *  * `@memberof`
 *  * `@classdesc`
 *  * `@kind`
 *  * `@class`
 *  * `@constant`
 *  * `@event`
 *  * `@external`
 *  * `@file`
 *  * `@function`
 *  * `@member`
 *  * `@mixin`
 *  * `@module`
 *  * `@namespace`
 *  * `@typedef`
 *  * `@access`
 *  * `@lends`
 *
 * The following tags are flattened to a top-level array-valued property:
 *
 *  * `@param` (to `params` property)
 *  * `@property` (to `properties` property)
 *  * `@returns` (to `returns` property)
 *  * `@augments` (to `augments` property)
 *  * `@example` (to `examples` property)
 *  * `@throws` (to `throws` property)
 *
 * The `@global`, `@static`, `@instance`, and `@inner` tags are flattened
 * to a `scope` property whose value is `"global"`, `"static"`, `"instance"`,
 * or `"inner"`.
 *
 * The `@access`, `@public`, `@protected`, and `@private` tags are flattened
 * to an `access` property whose value is `"protected"` or `"private"`.
 * The assumed default value is `"public"`, so `@access public` or `@public`
 * tags result in no `access` property.
 *
 * @name flatten
 * @param {Object} comment a parsed comment
 * @return {Object} comment with tags flattened
 */
module.exports = function (comment) {
  var result = extend({}, comment);
 
  comment.tags.forEach(function (tag) {
    (flatteners[tag.title] || function () {})(result, tag);
  });
 
  return result;
};