Skip to content

Commit

Permalink
Added row_limit to limit table cards (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcokreeft87 authored Jan 31, 2023
1 parent 7ffc2b1 commit 1d3f00f
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 23 deletions.
12 changes: 6 additions & 6 deletions formulaone-card.js

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions formulaone-card.js.LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,3 @@
* Copyright 2021 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/

/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
Binary file modified formulaone-card.js.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/cards/driver-standings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { html, HTMLTemplateResult } from "lit-html";
import { until } from 'lit-html/directives/until.js';
import FormulaOneCard from "..";
import { Constructor, DriverStanding } from "../api/models";
import { getApiErrorMessage, getApiLoadingMessage, getCountryFlagByNationality, getDriverName, getTeamImageUrl } from "../utils";
import { getApiErrorMessage, getApiLoadingMessage, getCountryFlagByNationality, getDriverName, getTeamImageUrl, reduceArray } from "../utils";
import { BaseCard } from "./base-card";

export default class DriverStandings extends BaseCard {
Expand Down Expand Up @@ -53,7 +53,7 @@ export default class DriverStandings extends BaseCard {
</tr>
</thead>
<tbody>
${response.map(standing => this.renderStandingRow(standing))}
${reduceArray(response, this.config.row_limit).map(standing => this.renderStandingRow(standing))}
</tbody>
</table>
`
Expand Down
4 changes: 2 additions & 2 deletions src/cards/last-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { html, HTMLTemplateResult } from "lit-html";
import { until } from 'lit-html/directives/until.js';
import FormulaOneCard from "..";
import { Result } from "../api/models";
import { getApiErrorMessage, getApiLoadingMessage, getDriverName, renderHeader } from "../utils";
import { getApiErrorMessage, getApiLoadingMessage, getDriverName, reduceArray, renderHeader } from "../utils";
import { BaseCard } from "./base-card";

export default class LastResult extends BaseCard {
Expand Down Expand Up @@ -54,7 +54,7 @@ export default class LastResult extends BaseCard {
</tr>
</thead>
<tbody>
${response.Results.map(result => this.renderResultRow(result))}
${reduceArray(response.Results, this.config.row_limit).map(result => this.renderResultRow(result))}
</tbody>
</table>`
: html`${getApiErrorMessage('last result')}`),
Expand Down
4 changes: 2 additions & 2 deletions src/cards/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { until } from 'lit-html/directives/until.js';
import FormulaOneCard from "..";
import { Race, Result } from "../api/models";
import { CardProperties } from "../types/formulaone-card-types";
import { getApiErrorMessage, getApiLoadingMessage, getDriverName, renderHeader } from "../utils";
import { getApiErrorMessage, getApiLoadingMessage, getDriverName, reduceArray, renderHeader } from "../utils";
import { BaseCard } from "./base-card";

export default class Results extends BaseCard {
Expand Down Expand Up @@ -140,7 +140,7 @@ export default class Results extends BaseCard {
</tr>
</thead>
<tbody>
${selectedRace.Results.map(result => this.renderResultRow(result))}
${reduceArray(selectedRace.Results, this.config.row_limit).map(result => this.renderResultRow(result))}
</tbody>
</table>`
: html`<table>
Expand Down
4 changes: 2 additions & 2 deletions src/cards/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { until } from 'lit-html/directives/until.js';
import FormulaOneCard from "..";
import { Circuit, Race } from "../api/models";
import { formatDate } from "../lib/format_date";
import { getApiErrorMessage, getApiLoadingMessage, getEndOfSeasonMessage } from "../utils";
import { getApiErrorMessage, getApiLoadingMessage, getEndOfSeasonMessage, reduceArray } from "../utils";
import { BaseCard } from "./base-card";

export default class Schedule extends BaseCard {
Expand Down Expand Up @@ -68,7 +68,7 @@ export default class Schedule extends BaseCard {
</tr>
</thead>
<tbody>
${response.map(race => this.renderScheduleRow(race))}
${reduceArray(response, this.config.row_limit).map(race => this.renderScheduleRow(race))}
</tbody>
</table>`;
}),
Expand Down
3 changes: 2 additions & 1 deletion src/types/formulaone-card-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface FormulaOneCardConfig extends LovelaceCardConfig {
show_raceinfo?: boolean;
actions?: ActionOptions;
f1_font?: boolean;
row_limit?: number;
}

export interface ActionOptions {
Expand Down Expand Up @@ -66,4 +67,4 @@ export interface ActionHandler extends HTMLElement {

export interface ActionHandlerElement extends HTMLElement {
actionHandler?: boolean;
}
}
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,8 @@ export const getRefreshTime = (endpoint: string) => {
}

return refreshCacheHours;
}

export const reduceArray = <T>(array: T[], number: number) => {
return number ? array.slice(0, number) : array;
}
27 changes: 27 additions & 0 deletions tests/utils/reduceArray.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { reduceArray } from "../../src/utils";

describe('reduceArray', () => {
it('should reduce the array to the size of the limit', () => {
const array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'];
const limit = 10;
const result = reduceArray(array, limit);

expect(result.length).toBe(limit);
});

it('should not reduce the array if the limit is greater than the array size', () => {
const array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'];
const limit = 20;
const result = reduceArray(array, limit);

expect(result.length).toBe(array.length);
});

it('should return an empty array if the array is empty', () => {
const array = [] as string[];
const limit = 20;
const result = reduceArray(array, limit);

expect(result.length).toBe(0);
});
});

0 comments on commit 1d3f00f

Please sign in to comment.