All files / apps/mc/src helper.ts

75.35% Statements 107/142
69.23% Branches 36/52
71.42% Functions 10/14
75.75% Lines 100/132

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 2837x   7x                                                         7x 23x 23x 23x   23x     7x 34x 34x   34x     7x 17x   17x   17x   17x 7x 7x 7x   7x   7x     7x         7x   7x   7x                         7x     17x   17x     7x                 12x 12x 12x   12x   12x 21x 21x 21x 21x   21x 21x 21x   21x 21x 21x   21x 21x 21x   21x 4x   4x   4x     21x         21x     12x 12x   12x 12x     7x         172x 172x   172x 25x     172x     172x 406x   406x   406x   406x             30x     406x     172x       7x 457x     7x 12x   12x 2x 10x 1x 9x 1x 8x 1x 7x 1x 6x 1x 5x 1x 4x 1x 3x 1x 2x 1x     12x     7x 4x   4x                                                                       7x                                              
import { by } from "@jga/algorithms";
 
import {
    Deck,
    DifficultyEnum,
    Hero,
    isDeck,
    isHero,
    isModularSet,
    isPack,
    isScenario,
    ModularSet,
    Pack,
    ResultEnum,
    Scenario,
} from "./@models";
 
type SectionData = Hero[] | Scenario[] | ModularSet[] | Pack[] | Deck[];
type SectionItem = Hero | Scenario | ModularSet | Pack | Deck;
type SelectDefaultValue = DifficultyEnum | Hero | ModularSet | ResultEnum | Scenario | null;
type SelectSource = DifficultyEnum[] | ResultEnum[] | string[];
 
export interface SourceItemInterface {
    name: string;
}
 
export interface StatSourceInterface {
    name: string;
    value: number;
}
 
export const createTitle = (title: string, body: HTMLBodyElement): void => {
    const header = document.createElement("h2");
    header.innerText = title;
    header.className = "center";
 
    body.appendChild(header);
};
 
export const createSubTitle = (title: string, body: HTMLBodyElement): void => {
    const header = document.createElement("h3");
    header.innerText = title;
 
    body.appendChild(header);
};
 
export const createSection = (title: string, data: SectionData, body: HTMLBodyElement): void => {
    createSubTitle(title, body);
 
    const section = document.createElement("section");
 
    const list = document.createElement("ul");
 
    data.forEach((item: SectionItem) => {
        const element = document.createElement("li");
        const dataElement = document.createElement("data");
        dataElement.innerText = `${item.name}`;
 
        let dataAttribute = "";
 
        Iif (isHeroOrModularSetOrScenario(item)) {
            dataAttribute = `Pack : ${item.pack.name}`;
        } else {
            Iif (isPack(item)) {
                dataAttribute = item.releaseDate;
            }
        }
 
        dataElement.setAttribute("value", dataAttribute);
 
        element.appendChild(dataElement);
 
        Iif (isDeck(item)) {
            const heroesList = document.createElement("ul");
 
            item.heroes.forEach((hero) => {
                const heroItem = document.createElement("li");
                heroItem.innerText = `${hero.name}`;
 
                heroesList.appendChild(heroItem);
            });
 
            element.appendChild(heroesList);
        }
 
        list.appendChild(element);
    });
 
    section.appendChild(list);
 
    body.appendChild(section);
};
 
export const createStatsTable = (
    data: Map<string, number>,
    source: SourceItemInterface[],
    wins: Map<string, number>,
    title: string,
    body: HTMLBodyElement,
    dates?: Map<string, number>,
    // eslint-disable-next-line max-params
): void => {
    const table = document.createElement("table");
    const leaderboard = document.createElement("jga-ui-leaderboard");
    leaderboard.setAttribute("title", title);
 
    const sourceRate: StatSourceInterface[] = [];
 
    source.forEach((item: SourceItemInterface) => {
        const itemLine = document.createElement("tr");
        const itemName = document.createElement("td");
        const itemCount = document.createElement("td");
        const itemRate = document.createElement("td");
 
        const gameNumber = data.has(item.name) ? data.get(item.name) : 0;
        const winsNumber = wins.has(item.name) ? wins.get(item.name) : 0;
        const winRate = gameNumber !== 0 ? (Number(winsNumber) / Number(gameNumber)) * 100 : 0;
 
        itemName.innerText = item.name;
        itemCount.innerText = Number(gameNumber).toString();
        itemRate.innerText = ` (${Number(winRate).toFixed(0).toString()} %) `;
 
        itemLine.appendChild(itemName);
        itemLine.appendChild(itemCount);
        itemLine.appendChild(itemRate);
 
        if (null != dates) {
            const itemLastDate = document.createElement("td");
 
            itemLastDate.innerText = String(dates.has(item.name) ? `${dates.get(item.name)} day(s) ago` : "");
 
            itemLine.appendChild(itemLastDate);
        }
 
        sourceRate.push({
            name: item.name,
            value: winRate,
        });
 
        table.appendChild(itemLine);
    });
 
    const leaderboardData = by(sourceRate, "value").reverse().slice(0, 5);
    leaderboard.setAttribute("data", JSON.stringify(leaderboardData));
 
    body.appendChild(table);
    body.appendChild(leaderboard);
};
 
export const createSelectElement = (
    defaultValue: SelectDefaultValue,
    source: SelectSource,
    multiple = false,
): HTMLSelectElement => {
    const selectElement = document.createElement("select");
    const selectOption = document.createElement("option");
 
    if (true === multiple) {
        selectElement.setAttribute("multiple", "true");
    }
 
    selectElement.appendChild(selectOption);
 
    // eslint-disable-next-line no-loops/no-loops
    for (const item in source) {
        const option = document.createElement("option");
        // eslint-disable-next-line security/detect-object-injection
        option.value = source[item];
        // eslint-disable-next-line security/detect-object-injection
        option.innerText = source[item];
 
        if (
            null !== defaultValue &&
            // eslint-disable-next-line security/detect-object-injection
            ((isHeroOrModularSetOrScenario(defaultValue) && defaultValue.name === source[item]) ||
                // eslint-disable-next-line security/detect-object-injection
                (!isHeroOrModularSetOrScenario(defaultValue) && defaultValue === source[item]))
        ) {
            option.selected = true;
        }
 
        selectElement.appendChild(option);
    }
 
    return selectElement;
};
 
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isHeroOrModularSetOrScenario = (value: any): value is Hero | ModularSet | Scenario => {
    return isHero(value) || isModularSet(value) || isScenario(value);
};
 
export const convertRatingToStars = (rating: number): string => {
    let stars = 0;
 
    if (28 <= rating) {
        stars = 5;
    } else if (24 <= rating) {
        stars = 4.5;
    } else if (20 <= rating) {
        stars = 4;
    } else if (16 <= rating) {
        stars = 3.5;
    } else if (12 <= rating) {
        stars = 3;
    } else if (9 <= rating) {
        stars = 2.5;
    } else if (5 <= rating) {
        stars = 2;
    } else if (1 <= rating) {
        stars = 1.5;
    } else if (-4 <= rating) {
        stars = 1;
    } else if (-7 <= rating) {
        stars = 0.5;
    }
 
    return `⭐`.repeat(stars * 2);
};
 
export const paginateTable = (selector: string, rowsPerPage = 10): void => {
    const table = document.querySelector(selector) as HTMLTableElement;
 
    Iif (null != table) {
        const tRows = table.querySelectorAll("tbody tr");
        const numPages = Math.ceil(tRows.length / rowsPerPage);
 
        const colCount = [].slice
            .call(table.querySelector("tr")?.cells)
            .reduce((a: number, b: HTMLTableCellElement) => a + b.colSpan, 0);
 
        // eslint-disable-next-line no-unsanitized/property
        table.createTFoot().insertRow().innerHTML = `<td colspan=${colCount}><div class="nav"></div></td>`;
 
        Iif (numPages === 1) {
            return;
        }
 
        // eslint-disable-next-line no-loops/no-loops
        for (let i = 0; i < numPages; i++) {
            const pageNum = i + 1;
 
            // eslint-disable-next-line no-unsanitized/method
            table
                .querySelector(".nav")
                ?.insertAdjacentHTML("beforeend", `<a href="#${selector}" rel="${i}">${pageNum}</a> `);
        }
 
        changeToPage(table, 1, rowsPerPage);
 
        // eslint-disable-next-line no-loops/no-loops
        for (const navA of Array.from(table.querySelectorAll(".nav a"))) {
            navA.addEventListener("click", (e: Event) =>
                changeToPage(table, parseInt((e.target as HTMLElement)?.innerHTML), rowsPerPage),
            );
        }
    }
};
 
const changeToPage = (table: HTMLTableElement, page: number, rowsPerPage: number): void => {
    const startItem = (page - 1) * rowsPerPage;
    const endItem = startItem + rowsPerPage;
    const navAs = table.querySelectorAll(".nav a");
    const tRows = table.querySelectorAll("tbody tr");
 
    // eslint-disable-next-line no-loops/no-loops
    for (let nix = 0; nix < navAs.length; nix++) {
        if (nix === page - 1) {
            // eslint-disable-next-line security/detect-object-injection
            navAs[nix].classList.add("active");
        } else {
            // eslint-disable-next-line security/detect-object-injection
            navAs[nix].classList.remove("active");
        }
 
        // eslint-disable-next-line no-loops/no-loops
        for (let trix = 0; trix < tRows.length; trix++) {
            // eslint-disable-next-line security/detect-object-injection
            (tRows[trix] as HTMLElement).style.display = trix >= startItem && trix < endItem ? "table-row" : "none";
        }
    }
};