import is from "is" ; import SummaryArticle from "../../models/summaryArticle" ; import mergeAtLevel from "./mergeAtLevel" ; import indexArticleLevels from "./indexArticleLevels" ; param { Summary } summary param { Article } article param { String | Article } level return { Summary } function insertArticle ( summary , article , level ) { article = new SummaryArticle ( article ) ; level = is . string ( level ) ? level : level . getLevel ( ) ; let parent = summary . getParent ( level ) ; if ( ! parent ) { return summary ; } let articles = parent . getArticles ( ) ; const index = getLeafIndex ( level ) ; articles = articles . insert ( index , article ) ; parent = parent . set ( "articles" , articles ) ; parent = indexArticleLevels ( parent ) ; return mergeAtLevel ( summary , parent . getLevel ( ) , parent ) ; } param { string } return { number } function getLeafIndex ( level ) { const arr = level . split ( "." ) . map ( ( char ) => { return parseInt ( char , 10 ) ; } ) ; return arr [ arr . length - 1 ] - 1 ; } export default insertArticle ;