Complete method getByPath of Summary

This commit is contained in:
Samy Pesse
2016-04-26 13:03:53 +02:00
parent ea5e8891a8
commit 1057bb15d3
3 changed files with 17 additions and 39 deletions
+6 -4
View File
@@ -24,7 +24,9 @@ Summary.prototype.getParts = function() {
@return {Part|Article}
*/
Summary.prototype.getByLevel = function(level) {
return SummaryArticle.getByLevel(this, level, 'getParts');
return SummaryArticle.findArticle(this, function(article) {
return (article.getLevel() === level);
}, 'getParts');
};
/**
@@ -34,9 +36,9 @@ Summary.prototype.getByLevel = function(level) {
@return {Part|Article}
*/
Summary.prototype.getByPath = function(filePath) {
// todo
return undefined;
return SummaryArticle.findArticle(this, function(article) {
return (article.getPath() === filePath);
}, 'getParts');
};
/**
+11 -25
View File
@@ -38,16 +38,6 @@ SummaryArticle.prototype.getDepth = function() {
return this.getLevel().split('.').length;
};
/**
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
@@ -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;
-10
View File
@@ -24,16 +24,6 @@ SummaryPart.prototype.getArticles = function() {
return this.get('articles');
};
/**
Return an article by its level
@param {String} level
@return {Article}
*/
SummaryPart.prototype.getByLevel = function(level) {
return SummaryArticle.getByLevel(this, level);
};
/**
Create a SummaryPart