mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-08 23:15:28 +02:00
refactor: fix method names and variables also split methods to be clear their role
This commit is contained in:
parent
7603f727f0
commit
5a1be35d71
2 changed files with 44 additions and 18 deletions
|
@ -63,16 +63,13 @@ class Pages {
|
|||
}
|
||||
|
||||
/**
|
||||
* Group given pages with descending order of creation time
|
||||
* Sort given pages with descending order of creation time
|
||||
*
|
||||
* @param {pages[]} pages - pages to group
|
||||
* @returns {Page[]}
|
||||
* @param {pages[]} pages - pages to sort
|
||||
* @returns {page[]}
|
||||
*/
|
||||
public static group(pages: Page[]): Page[] {
|
||||
const result: Page[] = [];
|
||||
const obj:Record<string, Array<Page>> = {};
|
||||
|
||||
pages.sort((a, b) => {
|
||||
public static sortByTimeDesc(pages: Page[]): Page[] {
|
||||
return pages.sort((a, b) => {
|
||||
if (a.body.time > b.body.time) {
|
||||
return 1;
|
||||
}
|
||||
|
@ -82,18 +79,36 @@ class Pages {
|
|||
}
|
||||
|
||||
return 0;
|
||||
}).forEach(doc => {
|
||||
if (doc._parent === '0' && typeof doc._id === 'string') {
|
||||
obj[doc._id] = [];
|
||||
obj[doc._id].push(doc);
|
||||
} else if (doc._parent !== '0' && doc._parent && doc._id) {
|
||||
obj[doc._id] = [];
|
||||
obj[doc._parent].push(doc);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Group given pages by their parents
|
||||
*
|
||||
* @param {pages[]} pages - pages to group
|
||||
* @returns {Page[]}
|
||||
*/
|
||||
public static groupByParent(pages: Page[]): Page[] {
|
||||
const result: Page[] = [];
|
||||
const pagesGroupedByParent:Record<string, Array<Page>> = {};
|
||||
|
||||
pages.forEach(page => {
|
||||
pagesGroupedByParent[String(page._id)] = [];
|
||||
|
||||
if (this.isRootPage(page)) {
|
||||
pagesGroupedByParent[String(page._id)].push(page);
|
||||
} else {
|
||||
pagesGroupedByParent[String(page._parent)].push(page);
|
||||
}
|
||||
});
|
||||
|
||||
Object.entries(obj).forEach(([, value]) => {
|
||||
if (value.length <=0) {
|
||||
/**
|
||||
* It converts grouped object to array
|
||||
* Also removes redundant keys when there is no children
|
||||
*/
|
||||
Object.entries(pagesGroupedByParent).forEach(([, value]) => {
|
||||
if (value.length <= 0) {
|
||||
return;
|
||||
} else {
|
||||
result.push(...value);
|
||||
|
@ -244,6 +259,16 @@ class Pages {
|
|||
throw new Error('Please, fill page Header');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given page is root
|
||||
*
|
||||
* @param {page} page - page to check
|
||||
* @returns {boolean}
|
||||
*/
|
||||
private static isRootPage(page:Page):boolean {
|
||||
return page._parent === '0';
|
||||
}
|
||||
}
|
||||
|
||||
export default Pages;
|
||||
|
|
|
@ -12,7 +12,8 @@ const router = express.Router();
|
|||
router.get('/page/new', verifyToken, allowEdit, async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const pagesAvailable = await Pages.getAll();
|
||||
const groupedPagesAvailable = Pages.group(pagesAvailable);
|
||||
const sortedPagesAvailable = Pages.sortByTimeDesc(pagesAvailable);
|
||||
const groupedPagesAvailable = Pages.groupByParent(sortedPagesAvailable);
|
||||
|
||||
res.render('pages/form', {
|
||||
groupedPagesAvailable,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue