aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/pages/documentation/docs_info.ts
blob: 326ebb31c19b2a180c72ad9d1899b303f1a565ee (plain) (blame)
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
import compareVersions = require('compare-versions');
import * as _ from 'lodash';
import {DocsInfoConfig, DocsMenu, SectionsMap} from 'ts/types';

export class DocsInfo {
    public packageName: string;
    public packageUrl: string;
    public websitePath: string;
    public docsJsonRoot: string;
    public menu: DocsMenu;
    public sections: SectionsMap;
    public sectionNameToMarkdown: {[sectionName: string]: string};
    private docsInfo: DocsInfoConfig;
    constructor(config: DocsInfoConfig) {
        this.packageName = config.packageName;
        this.packageUrl = config.packageUrl;
        this.websitePath = config.websitePath;
        this.docsJsonRoot = config.docsJsonRoot;
        this.sections = config.sections;
        this.sectionNameToMarkdown = config.sectionNameToMarkdown;
        this.docsInfo = config;
    }
    public isPublicType(typeName: string): boolean {
        if (_.isUndefined(this.docsInfo.publicTypes)) {
            return false;
        }
        const isPublic = _.includes(this.docsInfo.publicTypes, typeName);
        return isPublic;
    }
    public getModulePathsIfExists(sectionName: string): string[] {
        const modulePathsIfExists = this.docsInfo.sectionNameToModulePath[sectionName];
        return modulePathsIfExists;
    }
    public getMenu(selectedVersion?: string): {[section: string]: string[]} {
        if (_.isUndefined(selectedVersion) || _.isUndefined(this.docsInfo.menuSubsectionToVersionWhenIntroduced)) {
            return this.docsInfo.menu;
        }

        const finalMenu = _.cloneDeep(this.docsInfo.menu);
        finalMenu.contracts = _.filter(finalMenu.contracts, (contractName: string) => {
            const versionIntroducedIfExists = this.docsInfo.menuSubsectionToVersionWhenIntroduced[contractName];
            if (!_.isUndefined(versionIntroducedIfExists)) {
                const existsInSelectedVersion = compareVersions(selectedVersion,
                                                                versionIntroducedIfExists) >= 0;
                return existsInSelectedVersion;
            } else {
                return true;
            }
        });
        return finalMenu;
    }
}