All files / app/codeCharta/ui/dialogs/changelogDialog changelogDialog.component.ts

100% Statements 40/40
100% Branches 4/4
100% Functions 8/8
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 742x 2x 2x 2x 2x 2x 2x 2x               2x     2x 2x       2x 2x   2x 2x     2x 2x 2x 2x 10x 10x   10x 10x   6x 6x   6x 20x       10x 6x     2x       10x 215x 6x   215x         4x 89x       38x      
import markdownFile from "../../../../../CHANGELOG.md"
import { Component, Inject } from "@angular/core"
import { MAT_DIALOG_DATA, MatDialogContent, MatDialogActions, MatDialogClose } from "@angular/material/dialog"
import { marked } from "marked"
import { MatToolbar } from "@angular/material/toolbar"
import { CdkScrollable } from "@angular/cdk/scrolling"
import { MatButton } from "@angular/material/button"
import { KeyValuePipe } from "@angular/common"
 
@Component({
    selector: "cc-change-log-dialog",
    templateUrl: "./changelogDialog.component.html",
    standalone: true,
    imports: [MatToolbar, CdkScrollable, MatDialogContent, MatDialogActions, MatButton, MatDialogClose, KeyValuePipe]
})
export class ChangelogDialogComponent {
    changes: Record<string, string>
 
    constructor(@Inject(MAT_DIALOG_DATA) public data: { previousVersion: string; currentVersion: string }) {
        this.changes = this.getChangelogChanges()
    }
 
    private getChangelogChanges() {
        const parsedMarkdownFile = marked.parse(markdownFile, { headerIds: false })
        let changelogLines = parsedMarkdownFile.split("\n")
 
        const currentVersionFirstLine = this.findVersionLine(changelogLines, this.data.currentVersion)
        const lastOpenedVersionFirstLine = this.findVersionLine(changelogLines, this.data.previousVersion)
 
        //Add 1 to keep the version line so that it detects the end of the last set of changes
        changelogLines = changelogLines.slice(currentVersionFirstLine, lastOpenedVersionFirstLine + 1)
        const titles = ["Added 🚀", "Fixed 🐞", "Changed", "Removed 🗑", "Chore 👨‍💻 👩‍💻"]
        const changes = {}
        for (const title of titles) {
            const titlePattern = new RegExp(`<h3>${title}</h3>`)
            const titleLinesIndexes = this.getAllIndexes(changelogLines, titlePattern)
 
            const changelogTypes: string[] = []
            for (const lineIndex of titleLinesIndexes) {
                // Add 2 to remove the headline and the <ul> tag
                const start = lineIndex + 2
                const end = this.findEndChangesLine(changelogLines, lineIndex)
 
                for (const changeLine of changelogLines.slice(start, end)) {
                    changelogTypes.push(`${changeLine}<br>`)
                }
            }
 
            if (changelogTypes.length > 0) {
                changes[title] = changelogTypes.join("\n")
            }
        }
        return changes
    }
 
    private getAllIndexes(titles: string[], pattern: RegExp) {
        return titles.reduce((matchingTitleIndexes: number[], title, index) => {
            if (pattern.test(title)) {
                matchingTitleIndexes.push(index)
            }
            return matchingTitleIndexes
        }, [])
    }
 
    private findVersionLine(lines: string[], version: string): number {
        const versionPattern = new RegExp(`\\[${version}]`)
        return lines.findIndex(element => versionPattern.test(element))
    }
 
    private findEndChangesLine(lines: string[], startLine: number): number {
        return startLine + lines.slice(startLine + 1).findIndex(element => /<h3>/.test(element) || /<h2>/.test(element))
    }
}