Skip to content

Commit

Permalink
feat: 优化_getChaptersWithCode() 方法中生成文章章节 code 的算法,调整 groupBy() 方法,直接生…
Browse files Browse the repository at this point in the history
…成章节 code,移除 _getChaptersWithCode() 中多余的逻辑。理论上性能提升1倍
  • Loading branch information
yaohaixiao committed Aug 2, 2023
1 parent d4baf16 commit 91fbdff
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 49 deletions.
2 changes: 1 addition & 1 deletion anchors.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion anchors.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/js/outline.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/js/outline.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion outline.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion outline.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yaohaixiao/outline.js",
"version": "3.7.0",
"version": "3.8.0",
"description": "outline.js - 自动生成文章导读(Table of Contents)导航的 JavaScript 工具。",
"main": "outline.min.js",
"scripts": {
Expand Down
34 changes: 19 additions & 15 deletions src/_getChaptersWithCode.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import groupBy from './utils/lang/groupBy'

const _getChaptersWithCode = (chapters) => {
const groups = groupBy(chapters, 'pid')
const groups = {}
const cb = (o) => {
return [o.pid]
}

groups.forEach((group) => {
group.forEach((chapter, i) => {
chapter.index = i + 1
chapters.forEach((o) => {
const group = JSON.stringify(cb(o))

if (chapter.pid === -1) {
chapter.code = String(chapter.index)
}
})
groups[group] = groups[group] || []
groups[group].push(o)

o.index = groups[group].length
if (o.pid === -1) {
o.code = String(o.index)
}
})

groups.forEach((group) => {
group.forEach((paragraph) => {
chapters.forEach((chapter) => {
if (chapter.pid === paragraph.id) {
chapter.code = paragraph.code + '.' + chapter.index
Object.keys(groups).forEach((group) => {
groups[group].forEach((c, i) => {
c.index = i + 1
chapters.forEach((o) => {
if (o.pid === c.id) {
o.code = c.code + '.' + o.index
}
})
})
Expand Down
27 changes: 0 additions & 27 deletions src/utils/lang/groupBy.js

This file was deleted.

0 comments on commit 91fbdff

Please sign in to comment.