import is from "is" ; import removeArticle from "./removeArticle" ; import insertArticle from "./insertArticle" ; param { Summary } summary param { String | SummaryArticle } origin param { String | SummaryArticle } afterTarget return { Summary } function moveArticleAfter ( summary , origin , afterTarget ) { const originLevel = is . string ( origin ) ? origin : origin . getLevel ( ) ; const afterTargetLevel = is . string ( afterTarget ) ? afterTarget : afterTarget . getLevel ( ) ; const article = summary . getByLevel ( originLevel ) ; const targetLevel = increment ( afterTargetLevel ) ; if ( targetLevel < origin ) { const removed = removeArticle ( summary , originLevel ) ; return insertArticle ( removed , article , targetLevel ) ; } else { const inserted = insertArticle ( summary , article , targetLevel ) ; return removeArticle ( inserted , originLevel ) ; } } param { string } return { Array < Number > } function levelToArray ( l ) { return l . split ( "." ) . map ( ( char ) => { return parseInt ( char , 10 ) ; } ) ; } param { Array < Number > } return { string } function arrayToLevel ( a ) { return a . join ( "." ) ; } function increment ( level ) { level = levelToArray ( level ) ; level [ level . length - 1 ] ++ ; return arrayToLevel ( level ) ; } export default moveArticleAfter ;