diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/models/__tests__/summaryArticle.js | 23 | ||||
-rw-r--r-- | lib/models/summaryArticle.js | 84 |
2 files changed, 71 insertions, 36 deletions
diff --git a/lib/models/__tests__/summaryArticle.js b/lib/models/__tests__/summaryArticle.js new file mode 100644 index 0000000..7c4bc57 --- /dev/null +++ b/lib/models/__tests__/summaryArticle.js @@ -0,0 +1,23 @@ +var SummaryArticle = require('../summaryArticle'); + +describe('SummaryArticle', function() { + describe('createChildLevel', function() { + it('must create the right level', function() { + var article = SummaryArticle.create({}, '1.1'); + expect(article.createChildLevel()).toBe('1.1.1'); + }); + + it('must create the right level when has articles', function() { + var article = SummaryArticle.create({ + articles: [ + { + title: 'Test' + } + ] + }, '1.1'); + expect(article.createChildLevel()).toBe('1.1.2'); + }); + }); +}); + + diff --git a/lib/models/summaryArticle.js b/lib/models/summaryArticle.js index f072e7b..9b5b653 100644 --- a/lib/models/summaryArticle.js +++ b/lib/models/summaryArticle.js @@ -30,20 +30,20 @@ SummaryArticle.prototype.getArticles = function() { }; /** - Return how deep the article is. - The README has a depth of 1 - - @return {Number} -*/ + * Return how deep the article is. + * The README has a depth of 1 + * + * @return {Number} + */ SummaryArticle.prototype.getDepth = function() { return (this.getLevel().split('.').length - 1); }; /** - Get path (without anchor) to the pointing file - - @return {String} -*/ + * Get path (without anchor) to the pointing file + * + * @return {String} + */ SummaryArticle.prototype.getPath = function() { if (this.isExternal()) { return undefined; @@ -63,19 +63,19 @@ SummaryArticle.prototype.getPath = function() { }; /** - Return url if article is external - - @return {String} -*/ + * Return url if article is external + * + * @return {String} + */ SummaryArticle.prototype.getUrl = function() { return this.isExternal()? this.getRef() : undefined; }; /** - Get anchor for this article (or undefined) - - @return {String} -*/ + * Get anchor for this article (or undefined) + * + * @return {String} + */ SummaryArticle.prototype.getAnchor = function() { var ref = this.getRef(); var parts = ref.split('#'); @@ -85,29 +85,42 @@ SummaryArticle.prototype.getAnchor = function() { }; /** - Is article pointing to a page of an absolute url + * Create a new level for a new child article + * + * @return {String} + */ +SummaryArticle.prototype.createChildLevel = function() { + var level = this.getLevel(); + var subArticles = this.getArticles(); + var childLevel = level + '.' + (subArticles.size + 1); + + return childLevel; +}; - @return {Boolean} -*/ +/** + * Is article pointing to a page of an absolute url + * + * @return {Boolean} + */ SummaryArticle.prototype.isPage = function() { return !this.isExternal() && this.getRef(); }; /** - Is article pointing to aan absolute url - - @return {Boolean} -*/ + * Is article pointing to aan absolute url + * + * @return {Boolean} + */ SummaryArticle.prototype.isExternal = function() { return location.isExternal(this.getRef()); }; /** - Create a SummaryArticle - - @param {Object} def - @return {SummaryArticle} -*/ + * Create a SummaryArticle + * + * @param {Object} def + * @return {SummaryArticle} + */ SummaryArticle.create = function(def, level) { var articles = (def.articles || []).map(function(article, i) { if (article instanceof SummaryArticle) { @@ -124,14 +137,13 @@ SummaryArticle.create = function(def, level) { }); }; - /** - Find an article from a base one - - @param {Article|Part} base - @param {Function(article)} iter - @return {Article} -*/ + * Find an article from a base one + * + * @param {Article|Part} base + * @param {Function(article)} iter + * @return {Article} + */ SummaryArticle.findArticle = function(base, iter) { var articles = base.getArticles(); |