Skip to content

Commit d8337c5

Browse files
committed
added borderRadius query param
1 parent 376b155 commit d8337c5

4 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/cards/card.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default class Card {
77
private theme: Theme;
88
private badgeAlign: BadgeAlign;
99
private showBorder: boolean;
10+
private borderRadius: number;
1011

1112
private lines: Map<number, Badge[]>;
1213

@@ -15,6 +16,7 @@ export default class Card {
1516
this.theme = getThemeByName("github");
1617
this.badgeAlign = "left";
1718
this.showBorder = true;
19+
this.borderRadius = 4.5;
1820

1921
this.lineCount = 1;
2022
this.lines = new Map<number, Badge[]>();
@@ -68,4 +70,11 @@ export default class Card {
6870
public setShowBorder = (showBorder: boolean): void => {
6971
this.showBorder = showBorder;
7072
};
73+
74+
public getBorderRadius = (): number => this.borderRadius;
75+
76+
public setBorderRadius = (borderRadius: number): void => {
77+
this.borderRadius = borderRadius;
78+
};
7179
}
80+

src/controllers/cards-controller.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getThemeByName } from "../cards/themes";
44
import SvgGenerator from "../svg/svg-generator";
55
import {
66
validateAlign,
7+
validateBorderRadius,
78
validateLine,
89
validateLineCount,
910
} from "../utils/validator";
@@ -16,12 +17,14 @@ export const getCard = async (req: Request, res: Response) => {
1617
const lineCount = req.query.lineCount?.toString() || "1";
1718
const align = req.query.align?.toString() || "left";
1819
const showBorder = req.query.showBorder?.toString() || "true";
20+
const borderRadius = req.query.borderRadius?.toString() || "4.5";
1921

2022
card.setTitle(title);
2123
card.setTheme(getThemeByName(theme));
2224
card.setLineCount(validateLineCount(lineCount));
2325
card.setBadgeAlign(validateAlign(align));
2426
card.setShowBorder(showBorder !== "false");
27+
card.setBorderRadius(validateBorderRadius(borderRadius));
2528

2629
// run a loop card.getLineCount() times
2730
for (let i = 1; i <= card.getLineCount(); i++) {
@@ -34,3 +37,4 @@ export const getCard = async (req: Request, res: Response) => {
3437
res.setHeader("Content-Type", "image/svg+xml");
3538
res.send(await new SvgGenerator(card).toString());
3639
};
40+

src/svg/svg-generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class SvgGenerator {
4040
<rect
4141
x="${this.card.getShowBorder() ? 0.5 : 0}"
4242
y="${this.card.getShowBorder() ? 0.5 : 0}"
43-
rx="4.5"
43+
rx="${this.card.getBorderRadius()}"
4444
height="${this.height - (this.card.getShowBorder() ? 1 : 0)}"
4545
stroke="${this.card.getTheme().borderColor}"
4646
width="${this.width - (this.card.getShowBorder() ? 1 : 0)}"
@@ -143,3 +143,4 @@ export default class SvgGenerator {
143143
`;
144144
};
145145
}
146+

src/utils/validator.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ export const validateLineCount = (lineCount: string): number => {
2121
return lineNum < 1 ? 1 : lineNum;
2222
};
2323

24+
/**
25+
* Validates the border-radius.
26+
* If the border-radius is not valid, it returns 4.5.
27+
* The border-radius is invalid when it's not a number, less than 0 or greater than 50.
28+
*
29+
* @param {number} borderRadius The raw border radius
30+
* @returns {number} A valid borderRadius.
31+
*/
32+
export const validateBorderRadius = (lineCount: string): number => {
33+
const num = parseInt(lineCount);
34+
35+
// it's not a number
36+
if (isNaN(num)) {
37+
return 4.5;
38+
}
39+
40+
return num > 50 || num < 0 ? 4.5 : num;
41+
};
42+
2443
/**
2544
* Validates the given align.
2645
*
@@ -88,3 +107,4 @@ export const validateLine = (line: string): Badge[] => {
88107

89108
return badges;
90109
};
110+

0 commit comments

Comments
 (0)