diff options
Diffstat (limited to 'lib/modifiers/summary/insertArticle.js')
-rw-r--r-- | lib/modifiers/summary/insertArticle.js | 49 |
1 files changed, 0 insertions, 49 deletions
diff --git a/lib/modifiers/summary/insertArticle.js b/lib/modifiers/summary/insertArticle.js deleted file mode 100644 index 3a084b3..0000000 --- a/lib/modifiers/summary/insertArticle.js +++ /dev/null @@ -1,49 +0,0 @@ -var is = require('is'); -var SummaryArticle = require('../../models/summaryArticle'); -var mergeAtLevel = require('./mergeAtLevel'); -var indexArticleLevels = require('./indexArticleLevels'); - -/** - Returns a new Summary with the article at the given level, with - subsequent article shifted. - - @param {Summary} summary - @param {Article} article - @param {String|Article} level: level to insert at - @return {Summary} -*/ -function insertArticle(summary, article, level) { - article = SummaryArticle(article); - level = is.string(level)? level : level.getLevel(); - - var parent = summary.getParent(level); - if (!parent) { - return summary; - } - - // Find the index to insert at - var articles = parent.getArticles(); - var index = getLeafIndex(level); - - // Insert the article at the right index - articles = articles.insert(index, article); - - // Reindex the level from here - parent = parent.set('articles', articles); - parent = indexArticleLevels(parent); - - return mergeAtLevel(summary, parent.getLevel(), parent); -} - -/** - @param {String} - @return {Number} The index of this level within its parent's children - */ -function getLeafIndex(level) { - var arr = level.split('.').map(function (char) { - return parseInt(char, 10); - }); - return arr[arr.length - 1] - 1; -} - -module.exports = insertArticle; |