Code coverage report for lib/nest.js

Statements: 100% (22 / 22)      Branches: 100% (10 / 10)      Functions: 100% (4 / 4)      Lines: 100% (22 / 22)      Ignored: none     

All files » lib/ » nest.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    26 330 222     108     108 222 222   332   222 52 52 1       1 1   51 51   170       108   108                             26 165       26  
'use strict';
 
function nestTag(comment, tagName, target) {
  if (!comment[target]) {
    return comment;
  }
 
  var result = [],
    index = {};
 
  comment[target].forEach(function (tag) {
    index[tag.name] = tag;
    var parts = tag.name.split(/(\[\])?\./)
      .filter(function (part) {
        return part && part !== '[]';
      });
    if (parts.length > 1) {
      var parent = index[parts.slice(0, -1).join('.')];
      if (parent === undefined) {
        comment.errors.push({
          message: '@' + tagName + ' ' + tag.name + '\'s parent ' + parts[0] + ' not found',
          commentLineNumber: tag.lineNumber
        });
        result.push(tag);
        return;
      }
      parent.properties = parent.properties || [];
      parent.properties.push(tag);
    } else {
      result.push(tag);
    }
  });
 
  comment[target] = result;
 
  return comment;
}
 
/**
 * Nests
 * [parameters with properties](http://usejsdoc.org/tags-param.html#parameters-with-properties).
 *
 * A parameter `employee.name` will be attached to the parent parameter `employee` in
 * a `properties` array.
 *
 * This assumes that incoming comments have been flattened.
 *
 * @param {Object} comment input comment
 * @return {Object} nested comment
 */
function nest(comment) {
  return nestTag(nestTag(comment, 'param', 'params'),
    'property', 'properties');
}
 
module.exports = nest;