diff options
Diffstat (limited to 'lib/models/summaryArticle.js')
-rw-r--r-- | lib/models/summaryArticle.js | 36 |
1 files changed, 11 insertions, 25 deletions
diff --git a/lib/models/summaryArticle.js b/lib/models/summaryArticle.js index 945a16c..d6cd045 100644 --- a/lib/models/summaryArticle.js +++ b/lib/models/summaryArticle.js @@ -39,16 +39,6 @@ SummaryArticle.prototype.getDepth = function() { }; /** - Return an article by its level - - @param {String} level - @return {Article} -*/ -SummaryArticle.prototype.getByLevel = function(level) { - return SummaryArticle.getByLevel(this, level); -}; - -/** Get path (without anchor) to the pointing file @return {String} @@ -131,31 +121,27 @@ SummaryArticle.create = function(def, level) { /** - Return an article by its level + Find an article from a base one @param {Article|Part} base - @param {String} level + @param {Function(article)} iter @param {String} method @return {Article} */ -SummaryArticle.getByLevel = function(base, level, method) { +SummaryArticle.findArticle = function(base, iter, method) { method = method || 'getArticles'; var articles = base[method](); - var levelParts = level.split('.'); - var baseLevel = levelParts.shift(); - var result = articles.find(function(a) { - return a.getLevel() === baseLevel; - }); + return articles.reduce(function(result, article) { + if (result) return result; - if (!result) { - return undefined; - } - if (levelParts.length === 0) { - return result; - } + if (iter(article)) { + return article; + } - return SummaryArticle.getByLevel(result, levelParts.join('.')); + return SummaryArticle.findArticle(article, iter); + }, null); }; + module.exports = SummaryArticle; |