2025-05-12 05:38:44 +09:00

48 lines
1.7 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const is_1 = __importDefault(require("is"));
const summaryArticle_1 = __importDefault(require("../../models/summaryArticle"));
const mergeAtLevel_1 = __importDefault(require("./mergeAtLevel"));
const indexArticleLevels_1 = __importDefault(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 = new summaryArticle_1.default(article);
level = is_1.default.string(level) ? level : level.getLevel();
let parent = summary.getParent(level);
if (!parent) {
return summary;
}
// Find the index to insert at
let articles = parent.getArticles();
const 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);
// @ts-expect-error ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
parent = (0, indexArticleLevels_1.default)(parent);
return (0, mergeAtLevel_1.default)(summary, parent.getLevel(), parent);
}
/**
@param {string}
@return {number} The index of this level within its parent's children
*/
function getLeafIndex(level) {
const arr = level.split(".").map((char) => {
return parseInt(char, 10);
});
return arr[arr.length - 1] - 1;
}
exports.default = insertArticle;