@stats-organization/github-readme-stats-core 2.1.3 → 2.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/build/api/gist.d.ts.map +1 -1
  2. package/build/api/gist.js +34 -1
  3. package/build/api/index.d.ts.map +1 -1
  4. package/build/api/index.js +18 -1
  5. package/build/api/pin.d.ts.map +1 -1
  6. package/build/api/pin.js +17 -1
  7. package/build/api/top-langs.d.ts.map +1 -1
  8. package/build/api/top-langs.js +36 -5
  9. package/build/api/wakatime.d.ts.map +1 -1
  10. package/build/api/wakatime.js +34 -1
  11. package/build/calculateRank.d.ts +12 -11
  12. package/build/calculateRank.d.ts.map +1 -1
  13. package/build/calculateRank.js +18 -18
  14. package/build/cards/gist.d.ts.map +1 -1
  15. package/build/cards/gist.js +0 -1
  16. package/build/cards/repo.d.ts.map +1 -1
  17. package/build/cards/repo.js +29 -21
  18. package/build/cards/stats.d.ts +4 -2
  19. package/build/cards/stats.d.ts.map +1 -1
  20. package/build/cards/stats.js +21 -9
  21. package/build/cards/top-languages.d.ts.map +1 -1
  22. package/build/cards/top-languages.js +39 -12
  23. package/build/cards/wakatime.d.ts.map +1 -1
  24. package/build/cards/wakatime.js +29 -9
  25. package/build/common/Card.d.ts +54 -60
  26. package/build/common/Card.d.ts.map +1 -1
  27. package/build/common/Card.js +88 -61
  28. package/build/common/I18n.d.ts +13 -14
  29. package/build/common/I18n.d.ts.map +1 -1
  30. package/build/common/I18n.js +13 -12
  31. package/build/common/color.d.ts +56 -32
  32. package/build/common/color.d.ts.map +1 -1
  33. package/build/common/color.js +87 -50
  34. package/build/common/config.d.ts +18 -2
  35. package/build/common/config.d.ts.map +1 -1
  36. package/build/common/config.js +17 -26
  37. package/build/common/constants.d.ts +2 -2
  38. package/build/common/constants.d.ts.map +1 -1
  39. package/build/common/constants.js +5 -3
  40. package/build/common/error.d.ts +36 -29
  41. package/build/common/error.d.ts.map +1 -1
  42. package/build/common/error.js +14 -10
  43. package/build/common/fmt.d.ts +14 -13
  44. package/build/common/fmt.d.ts.map +1 -1
  45. package/build/common/fmt.js +19 -15
  46. package/build/common/html.d.ts +2 -4
  47. package/build/common/html.d.ts.map +1 -1
  48. package/build/common/html.js +2 -6
  49. package/build/common/http.d.ts +13 -4
  50. package/build/common/http.d.ts.map +1 -1
  51. package/build/common/http.js +3 -4
  52. package/build/common/icons.d.ts +22 -21
  53. package/build/common/icons.d.ts.map +1 -1
  54. package/build/common/icons.js +4 -5
  55. package/build/common/languageColors.json +9 -1
  56. package/build/common/ops.d.ts +30 -29
  57. package/build/common/ops.d.ts.map +1 -1
  58. package/build/common/ops.js +32 -38
  59. package/build/common/render.d.ts +115 -111
  60. package/build/common/render.d.ts.map +1 -1
  61. package/build/common/render.js +138 -88
  62. package/build/common/retryer.d.ts +24 -13
  63. package/build/common/retryer.d.ts.map +1 -1
  64. package/build/common/retryer.js +22 -30
  65. package/build/fetchers/gist.d.ts +6 -21
  66. package/build/fetchers/gist.d.ts.map +1 -1
  67. package/build/fetchers/gist.js +30 -36
  68. package/build/fetchers/repo.d.ts.map +1 -1
  69. package/build/fetchers/repo.js +4 -7
  70. package/build/fetchers/stats.d.ts.map +1 -1
  71. package/build/fetchers/stats.js +3 -7
  72. package/build/fetchers/top-languages.d.ts.map +1 -1
  73. package/build/fetchers/top-languages.js +0 -1
  74. package/build/fetchers/types.d.ts +126 -0
  75. package/build/fetchers/types.d.ts.map +1 -0
  76. package/build/fetchers/types.js +1 -0
  77. package/build/fetchers/wakatime.d.ts.map +1 -1
  78. package/build/fetchers/wakatime.js +0 -1
  79. package/build/translations.d.ts +627 -24
  80. package/build/translations.d.ts.map +1 -1
  81. package/build/translations.js +7 -8
  82. package/package.json +4 -4
  83. package/src/_emoji-name-map.d.ts +10 -0
  84. package/src/api/gist.js +36 -2
  85. package/src/api/index.js +19 -2
  86. package/src/api/pin.js +18 -2
  87. package/src/api/top-langs.js +38 -6
  88. package/src/api/wakatime.js +36 -2
  89. package/src/{calculateRank.js → calculateRank.ts} +29 -19
  90. package/src/cards/gist.js +0 -2
  91. package/src/cards/repo.js +30 -22
  92. package/src/cards/stats.js +22 -10
  93. package/src/cards/top-languages.js +49 -13
  94. package/src/cards/wakatime.js +33 -10
  95. package/src/common/Card.ts +338 -0
  96. package/src/common/I18n.ts +49 -0
  97. package/src/common/color.ts +212 -0
  98. package/src/common/config.ts +83 -0
  99. package/src/common/constants.ts +10 -0
  100. package/src/common/{error.js → error.ts} +20 -14
  101. package/src/common/{fmt.js → fmt.ts} +27 -19
  102. package/src/common/{html.js → html.ts} +3 -8
  103. package/src/common/http.ts +31 -0
  104. package/src/common/{icons.js → icons.ts} +9 -7
  105. package/src/common/languageColors.json +9 -1
  106. package/src/common/{ops.js → ops.ts} +48 -52
  107. package/src/common/{render.js → render.ts} +208 -98
  108. package/src/common/retryer.ts +125 -0
  109. package/src/fetchers/gist.ts +136 -0
  110. package/src/fetchers/repo.js +4 -8
  111. package/src/fetchers/stats.js +3 -8
  112. package/src/fetchers/top-languages.js +0 -2
  113. package/src/fetchers/{types.d.ts → types.ts} +1 -1
  114. package/src/fetchers/wakatime.js +0 -2
  115. package/src/{translations.js → translations.ts} +14 -10
  116. package/src/common/Card.js +0 -275
  117. package/src/common/I18n.js +0 -42
  118. package/src/common/color.js +0 -145
  119. package/src/common/config.js +0 -78
  120. package/src/common/constants.js +0 -9
  121. package/src/common/http.js +0 -21
  122. package/src/common/retryer.js +0 -104
  123. package/src/fetchers/gist.js +0 -114
@@ -1,5 +1,3 @@
1
- // @ts-check
2
-
3
1
  import toEmoji from "emoji-name-map";
4
2
 
5
3
  import { OWNER_AFFILIATIONS } from "./constants.js";
@@ -8,10 +6,10 @@ import { CustomError } from "./error.js";
8
6
  /**
9
7
  * Returns boolean if value is either "true" or "false" else the value as it is.
10
8
  *
11
- * @param {string | boolean} value The value to parse.
12
- * @returns {boolean | undefined } The parsed value.
9
+ * @param value The value to parse.
10
+ * @returns The parsed value.
13
11
  */
14
- const parseBoolean = (value) => {
12
+ const parseBoolean = (value: string | boolean): boolean | undefined => {
15
13
  if (typeof value === "boolean") {
16
14
  return value;
17
15
  }
@@ -29,10 +27,10 @@ const parseBoolean = (value) => {
29
27
  /**
30
28
  * Parse string to array of strings.
31
29
  *
32
- * @param {string} str The string to parse.
33
- * @returns {string[]} The array of strings.
30
+ * @param str The string to parse.
31
+ * @returns The array of strings.
34
32
  */
35
- const parseArray = (str) => {
33
+ const parseArray = (str: string): Array<string> => {
36
34
  if (!str) {
37
35
  return [];
38
36
  }
@@ -42,47 +40,44 @@ const parseArray = (str) => {
42
40
  /**
43
41
  * Clamp the given number between the given range.
44
42
  *
45
- * @param {number} number The number to clamp.
46
- * @param {number} min The minimum value.
47
- * @param {number} max The maximum value.
48
- * @returns {number} The clamped number.
43
+ * @param number The number to clamp.
44
+ * @param min The minimum value.
45
+ * @param max The maximum value.
46
+ * @returns The clamped number.
49
47
  */
50
- const clampValue = (number, min, max) => {
51
- // @ts-ignore
52
- if (Number.isNaN(parseInt(number, 10))) {
48
+ const clampValue = (
49
+ number: string | number,
50
+ min: number,
51
+ max: number,
52
+ ): number => {
53
+ if (Number.isNaN(parseInt(String(number), 10))) {
53
54
  return min;
54
55
  }
55
- return Math.max(min, Math.min(number, max));
56
+ return Math.max(min, Math.min(Number(number), max));
56
57
  };
57
58
 
58
59
  /**
59
60
  * Lowercase and trim string.
60
61
  *
61
- * @param {string} name String to lowercase and trim.
62
- * @returns {string} Lowercased and trimmed string.
62
+ * @param name String to lowercase and trim.
63
+ * @returns Lowercased and trimmed string.
63
64
  */
64
- const lowercaseTrim = (name) => name.toLowerCase().trim();
65
+ const lowercaseTrim = (name: string): string => name.toLowerCase().trim();
65
66
 
66
67
  /**
67
68
  * Split array of languages in two columns.
68
69
  *
69
70
  * @template T Language object.
70
- * @param {Array<T>} arr Array of languages.
71
- * @param {number} perChunk Number of languages per column.
72
- * @returns {Array<T>} Array of languages split in two columns.
71
+ * @param arr Array of languages.
72
+ * @param perChunk Number of languages per column.
73
+ * @returns Array of languages split in two columns.
73
74
  */
74
- const chunkArray = (arr, perChunk) => {
75
- return arr.reduce((resultArray, item, index) => {
75
+ const chunkArray = <T>(arr: Array<T>, perChunk: number): Array<Array<T>> => {
76
+ return arr.reduce<Array<Array<T>>>((resultArray, item, index) => {
76
77
  const chunkIndex = Math.floor(index / perChunk);
77
-
78
- if (!resultArray[chunkIndex]) {
79
- // @ts-ignore
80
- resultArray[chunkIndex] = []; // start a new chunk
81
- }
82
-
83
- // @ts-ignore
84
- resultArray[chunkIndex].push(item);
85
-
78
+ const chunk = resultArray[chunkIndex] ?? [];
79
+ chunk.push(item);
80
+ resultArray[chunkIndex] = chunk;
86
81
  return resultArray;
87
82
  }, []);
88
83
  };
@@ -90,10 +85,10 @@ const chunkArray = (arr, perChunk) => {
90
85
  /**
91
86
  * Parse emoji from string.
92
87
  *
93
- * @param {string} str String to parse emoji from.
94
- * @returns {string} String with emoji parsed.
88
+ * @param str String to parse emoji from.
89
+ * @returns String with emoji parsed.
95
90
  */
96
- const parseEmojis = (str) => {
91
+ const parseEmojis = (str: string): string => {
97
92
  if (!str) {
98
93
  throw new Error("[parseEmoji]: str argument not provided");
99
94
  }
@@ -105,11 +100,11 @@ const parseEmojis = (str) => {
105
100
  /**
106
101
  * Get diff in minutes between two dates.
107
102
  *
108
- * @param {Date} d1 First date.
109
- * @param {Date} d2 Second date.
110
- * @returns {number} Number of minutes between the two dates.
103
+ * @param d1 First date.
104
+ * @param d2 Second date.
105
+ * @returns Number of minutes between the two dates.
111
106
  */
112
- const dateDiff = (d1, d2) => {
107
+ const dateDiff = (d1: Date, d2: Date): number => {
113
108
  const date1 = new Date(d1);
114
109
  const date2 = new Date(d2);
115
110
  const diff = date1.getTime() - date2.getTime();
@@ -119,40 +114,41 @@ const dateDiff = (d1, d2) => {
119
114
  /**
120
115
  * Parse owner affiliations.
121
116
  *
122
- * @param {string[]} affiliations input affiliations to be parsed.
123
- * @returns {string[]} Parsed affiliations.
117
+ * @param affiliations input affiliations to be parsed.
118
+ * @returns Parsed affiliations.
124
119
  *
125
120
  * @throws {CustomError} If affiliations contains invalid values.
126
121
  */
127
- const parseOwnerAffiliations = (affiliations) => {
122
+ const parseOwnerAffiliations = (affiliations: Array<string>): Array<string> => {
128
123
  // Set default value for ownerAffiliations.
129
124
  // NOTE: Done here since parseArray() will always return an empty array even nothing
130
125
  //was specified.
131
- affiliations =
132
- affiliations && affiliations.length > 0
126
+ const normalized =
127
+ affiliations.length > 0
133
128
  ? affiliations.map((affiliation) => affiliation.toUpperCase())
134
129
  : ["OWNER"];
135
130
 
136
131
  // Check if ownerAffiliations contains valid values.
137
132
  if (
138
- affiliations.some(
139
- (affiliation) => !OWNER_AFFILIATIONS.includes(affiliation),
140
- )
133
+ normalized.some((affiliation) => !OWNER_AFFILIATIONS.includes(affiliation))
141
134
  ) {
142
135
  throw new CustomError(
143
136
  "Invalid query parameter",
144
137
  CustomError.INVALID_AFFILIATION,
145
138
  );
146
139
  }
147
- return affiliations;
140
+ return normalized;
148
141
  };
149
142
 
150
- const buildSearchFilter = (repos = [], owners = []) => {
151
- let repoFilter =
143
+ const buildSearchFilter = (
144
+ repos: Array<string> | string = [],
145
+ owners: Array<string> | string = [],
146
+ ): string => {
147
+ const repoFilter =
152
148
  Array.isArray(repos) && repos.length > 0
153
149
  ? repos.map((r) => `repo:${r} `).join("")
154
150
  : "";
155
- let orgFilter =
151
+ const orgFilter =
156
152
  Array.isArray(owners) && owners.length > 0
157
153
  ? owners.map((o) => `owner:${o} `).join("")
158
154
  : "";
@@ -1,6 +1,4 @@
1
- // @ts-check
2
-
3
- import { getCardColors } from "./color.js";
1
+ import { getCardColors, isPrefixedHexColor } from "./color.js";
4
2
  import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from "./error.js";
5
3
  import { encodeHTML } from "./html.js";
6
4
  import { clampValue } from "./ops.js";
@@ -9,14 +7,30 @@ import { clampValue } from "./ops.js";
9
7
  * Auto layout utility, allows us to layout things vertically or horizontally with
10
8
  * proper gaping.
11
9
  *
12
- * @param {object} props Function properties.
13
- * @param {string[]} props.items Array of items to layout.
14
- * @param {number} props.gap Gap between items.
15
- * @param {"column" | "row"=} props.direction Direction to layout items.
16
- * @param {number[]=} props.sizes Array of sizes for each item.
17
- * @returns {string[]} Array of items with proper layout.
10
+ * The caller must ensure that the passed `items` are properly sanitized!
11
+ *
12
+ * @param props Function properties.
13
+ * @param props.items Array of sanitized items to layout.
14
+ * @param props.gap Gap between items.
15
+ * @param props.direction Direction to layout items.
16
+ * @param props.sizes Array of sizes for each item.
17
+ * @returns Array of items with proper layout.
18
18
  */
19
- const flexLayout = ({ items, gap, direction, sizes = [] }) => {
19
+ const flexLayout = ({
20
+ items,
21
+ gap,
22
+ direction,
23
+ sizes = [],
24
+ }: {
25
+ items: Array<string>;
26
+ gap: number;
27
+ direction?: "column" | "row";
28
+ sizes?: Array<number>;
29
+ }): Array<string> => {
30
+ if (sizes.some((size) => !Number.isFinite(size))) {
31
+ throw new Error("flexLayout: `sizes` must contain only numbers");
32
+ }
33
+
20
34
  let lastSize = 0;
21
35
  // filter() for filtering out empty strings
22
36
  return items.filter(Boolean).map((item, i) => {
@@ -33,15 +47,19 @@ const flexLayout = ({ items, gap, direction, sizes = [] }) => {
33
47
  /**
34
48
  * Creates a node to display the primary programming language of the repository/gist.
35
49
  *
36
- * @param {string} langName Language name.
37
- * @param {string} langColor Language color.
38
- * @returns {string} Language display SVG object.
50
+ * @param langName Language name.
51
+ * @param langColor Language color.
52
+ * @returns Language display SVG object.
39
53
  */
40
- const createLanguageNode = (langName, langColor) => {
54
+ const createLanguageNode = (langName: string, langColor: string): string => {
55
+ if (!isPrefixedHexColor(langColor)) {
56
+ throw new Error(`Invalid language color: "${langColor}"`);
57
+ }
58
+
41
59
  return `
42
60
  <g data-testid="primary-lang">
43
61
  <circle data-testid="lang-color" cx="0" cy="-5" r="6" fill="${langColor}" />
44
- <text data-testid="lang-name" class="gray" x="15">${langName}</text>
62
+ <text data-testid="lang-name" class="gray" x="15">${encodeHTML(langName)}</text>
45
63
  </g>
46
64
  `;
47
65
  };
@@ -49,15 +67,15 @@ const createLanguageNode = (langName, langColor) => {
49
67
  /**
50
68
  * Create a node to indicate progress in percentage along a horizontal line.
51
69
  *
52
- * @param {Object} params Object that contains the createProgressNode parameters.
53
- * @param {number} params.x X-axis position.
54
- * @param {number} params.y Y-axis position.
55
- * @param {number} params.width Width of progress bar.
56
- * @param {string} params.color Progress color.
57
- * @param {number} params.progress Progress value.
58
- * @param {string} params.progressBarBackgroundColor Progress bar bg color.
59
- * @param {number} params.delay Delay before animation starts.
60
- * @returns {string} Progress node.
70
+ * @param params Object that contains the createProgressNode parameters.
71
+ * @param params.x X-axis position.
72
+ * @param params.y Y-axis position.
73
+ * @param params.width Width of progress bar.
74
+ * @param params.color Progress color.
75
+ * @param params.progress Progress value.
76
+ * @param params.progressBarBackgroundColor Progress bar bg color.
77
+ * @param params.delay Delay before animation starts.
78
+ * @returns Progress node.
61
79
  */
62
80
  const createProgressNode = ({
63
81
  x,
@@ -67,7 +85,36 @@ const createProgressNode = ({
67
85
  progress,
68
86
  progressBarBackgroundColor,
69
87
  delay,
70
- }) => {
88
+ }: {
89
+ x: number;
90
+ y: number;
91
+ width: number;
92
+ color: string;
93
+ progress: number;
94
+ progressBarBackgroundColor: string;
95
+ delay: number;
96
+ }): string => {
97
+ if (!isPrefixedHexColor(color)) {
98
+ throw new Error(`Invalid progress color: "${color}"`);
99
+ }
100
+ if (!isPrefixedHexColor(progressBarBackgroundColor)) {
101
+ throw new Error(
102
+ `Invalid progress bar background color: "${progressBarBackgroundColor}"`,
103
+ );
104
+ }
105
+ if (!Number.isFinite(width)) {
106
+ throw new Error(`Invalid width: "${width}"`);
107
+ }
108
+ if (!Number.isFinite(x)) {
109
+ throw new Error(`Invalid x: "${x}"`);
110
+ }
111
+ if (!Number.isFinite(y)) {
112
+ throw new Error(`Invalid y: "${y}"`);
113
+ }
114
+ if (!Number.isFinite(delay)) {
115
+ throw new Error(`Invalid delay: "${delay}"`);
116
+ }
117
+
71
118
  const progressPercentage = clampValue(progress, 2, 100);
72
119
 
73
120
  return `
@@ -91,16 +138,16 @@ const createProgressNode = ({
91
138
  * native, font-aware wrapping. Content overflowing `lineCount` lines is
92
139
  * clipped (with an ellipsis on the last visible line) by CSS line-clamp.
93
140
  *
94
- * @param {object} props Function properties.
95
- * @param {string} props.text Text to render (will be HTML-encoded).
96
- * @param {number} props.x X position of the foreignObject.
97
- * @param {number} props.y Y position of the foreignObject.
98
- * @param {number} props.width Width of the wrap box.
99
- * @param {number} props.height Height of the wrap box.
100
- * @param {number} props.lineCount Maximum number of lines to display.
101
- * @param {string} props.className CSS class applied to the inner element.
102
- * @param {string=} props.testId Optional test id for the inner element.
103
- * @returns {string} foreignObject SVG node.
141
+ * @param props Function properties.
142
+ * @param props.text Text to render (will be HTML-encoded).
143
+ * @param props.x X position of the foreignObject.
144
+ * @param props.y Y position of the foreignObject.
145
+ * @param props.width Width of the wrap box.
146
+ * @param props.height Height of the wrap box.
147
+ * @param props.lineCount Maximum number of lines to display.
148
+ * @param props.className CSS class applied to the inner element.
149
+ * @param props.testId Optional test id for the inner element.
150
+ * @returns foreignObject SVG node.
104
151
  */
105
152
  const wrappedTextNode = ({
106
153
  text,
@@ -111,11 +158,36 @@ const wrappedTextNode = ({
111
158
  lineCount,
112
159
  className,
113
160
  testId,
114
- }) => {
115
- const testIdAttr = testId ? ` data-testid="${testId}"` : "";
161
+ }: {
162
+ text: string;
163
+ x: number;
164
+ y: number;
165
+ width: number;
166
+ height: number;
167
+ lineCount: number;
168
+ className: string;
169
+ testId?: string;
170
+ }): string => {
171
+ if (!Number.isFinite(x)) {
172
+ throw new Error(`Invalid x: "${x}"`);
173
+ }
174
+ if (!Number.isFinite(y)) {
175
+ throw new Error(`Invalid y: "${y}"`);
176
+ }
177
+ if (!Number.isFinite(width)) {
178
+ throw new Error(`Invalid width: "${width}"`);
179
+ }
180
+ if (!Number.isFinite(height)) {
181
+ throw new Error(`Invalid height: "${height}"`);
182
+ }
183
+ if (!Number.isFinite(lineCount)) {
184
+ throw new Error(`Invalid lineCount: "${lineCount}"`);
185
+ }
186
+
187
+ const testIdAttr = testId ? ` data-testid="${encodeHTML(testId)}"` : "";
116
188
  return `
117
189
  <foreignObject x="${x}" y="${y}" width="${width}" height="${height}">
118
- <div xmlns="http://www.w3.org/1999/xhtml" class="${className}" style="--lines: ${lineCount};"${testIdAttr}>${encodeHTML(
190
+ <div xmlns="http://www.w3.org/1999/xhtml" class="${encodeHTML(className)}" style="--lines: ${lineCount};"${testIdAttr}>${encodeHTML(
119
191
  text,
120
192
  )}</div>
121
193
  </foreignObject>
@@ -128,37 +200,55 @@ const wrappedTextNode = ({
128
200
  * browser handles wrapping and the line count is taken from the `--lines`
129
201
  * custom property set on the element.
130
202
  *
131
- * @param {string} color Text color (CSS `color` property).
132
- * @returns {string} CSS rules block (without the surrounding selector).
203
+ * @param color Text color (CSS `color` property).
204
+ * @returns CSS rules block (without the surrounding selector).
133
205
  */
134
- const wrappedTextStyles = (color) => `
135
- color: ${color};
136
- margin: 0;
137
- line-height: 1.2;
138
- overflow-wrap: anywhere;
139
- word-break: break-word;
140
- display: -webkit-box;
141
- -webkit-box-orient: vertical;
142
- -webkit-line-clamp: var(--lines);
143
- line-clamp: var(--lines);
144
- overflow: hidden;
145
- text-overflow: ellipsis;
146
- padding-bottom: 0.15em;
147
- `;
206
+ const wrappedTextStyles = (color: string): string => {
207
+ if (!isPrefixedHexColor(color)) {
208
+ throw new Error(`Invalid text color: "${color}"`);
209
+ }
210
+
211
+ return `
212
+ color: ${color};
213
+ margin: 0;
214
+ line-height: 1.2;
215
+ overflow-wrap: anywhere;
216
+ word-break: break-word;
217
+ display: -webkit-box;
218
+ -webkit-box-orient: vertical;
219
+ -webkit-line-clamp: var(--lines);
220
+ line-clamp: var(--lines);
221
+ overflow: hidden;
222
+ text-overflow: ellipsis;
223
+ padding-bottom: 0.15em;
224
+ `;
225
+ };
148
226
 
149
227
  /**
150
228
  * Creates an icon with label to display repository/gist stats like forks, stars, etc.
151
229
  *
152
- * @param {string} icon The icon to display.
153
- * @param {number|string} label The label to display.
154
- * @param {string} testid The testid to assign to the label.
155
- * @param {number} iconSize The size of the icon.
156
- * @returns {string} Icon with label SVG object.
230
+ * The caller must ensure that the passed `icon` is properly sanitized!
231
+ *
232
+ * @param icon The sanitized icon to display.
233
+ * @param label The label to display.
234
+ * @param testid The testid to assign to the label.
235
+ * @param iconSize The size of the icon.
236
+ * @returns Icon with label SVG object.
157
237
  */
158
- const iconWithLabel = (icon, label, testid, iconSize) => {
238
+ const iconWithLabel = (
239
+ icon: string,
240
+ label: number | string,
241
+ testid: string,
242
+ iconSize: number,
243
+ ): string => {
159
244
  if (typeof label === "number" && label <= 0) {
160
245
  return "";
161
246
  }
247
+
248
+ if (!Number.isFinite(iconSize)) {
249
+ throw new Error(`Invalid iconSize: "${iconSize}"`);
250
+ }
251
+
162
252
  const iconSvg = `
163
253
  <svg
164
254
  class="icon"
@@ -171,7 +261,7 @@ const iconWithLabel = (icon, label, testid, iconSize) => {
171
261
  ${icon}
172
262
  </svg>
173
263
  `;
174
- const text = `<text data-testid="${testid}" class="gray">${label}</text>`;
264
+ const text = `<text data-testid="${encodeHTML(testid)}" class="gray">${encodeHTML(String(label))}</text>`;
175
265
  return flexLayout({ items: [iconSvg, text], gap: 20 }).join("");
176
266
  };
177
267
 
@@ -186,23 +276,34 @@ const UPSTREAM_API_ERRORS = [
186
276
  /**
187
277
  * Renders error message on the card.
188
278
  *
189
- * @param {object} args Function arguments.
190
- * @param {string} args.message Main error message.
191
- * @param {string} [args.secondaryMessage=""] The secondary error message.
192
- * @param {object} [args.renderOptions={}] Render options.
193
- * @param {string=} args.renderOptions.title_color Card title color.
194
- * @param {string=} args.renderOptions.text_color Card text color.
195
- * @param {string=} args.renderOptions.bg_color Card background color.
196
- * @param {string=} args.renderOptions.border_color Card border color.
197
- * @param {Parameters<typeof getCardColors>[0]["theme"]=} args.renderOptions.theme Card theme.
198
- * @param {boolean=} args.renderOptions.show_repo_link Whether to show repo link or not.
199
- * @returns {string} The SVG markup.
279
+ * @param args Function arguments.
280
+ * @param args.message Main error message.
281
+ * @param args.secondaryMessage The secondary error message.
282
+ * @param args.renderOptions Render options.
283
+ * @param args.renderOptions.title_color Card title color.
284
+ * @param args.renderOptions.text_color Card text color.
285
+ * @param args.renderOptions.bg_color Card background color.
286
+ * @param args.renderOptions.border_color Card border color.
287
+ * @param args.renderOptions.theme Card theme.
288
+ * @param args.renderOptions.show_repo_link Whether to show repo link or not.
289
+ * @returns The SVG markup.
200
290
  */
201
291
  const renderError = ({
202
292
  message,
203
293
  secondaryMessage = "",
204
294
  renderOptions = {},
205
- }) => {
295
+ }: {
296
+ message: string;
297
+ secondaryMessage?: string;
298
+ renderOptions?: {
299
+ title_color?: string;
300
+ text_color?: string;
301
+ bg_color?: string;
302
+ border_color?: string;
303
+ theme?: string;
304
+ show_repo_link?: boolean;
305
+ };
306
+ }): string => {
206
307
  const {
207
308
  title_color,
208
309
  text_color,
@@ -224,7 +325,7 @@ const renderError = ({
224
325
  });
225
326
 
226
327
  return `
227
- <svg width="${ERROR_CARD_LENGTH}" height="120" viewBox="0 0 ${ERROR_CARD_LENGTH} 120" fill="${bgColor}" xmlns="http://www.w3.org/2000/svg">
328
+ <svg width="${ERROR_CARD_LENGTH}" height="120" viewBox="0 0 ${ERROR_CARD_LENGTH} 120" fill="${String(bgColor)}" xmlns="http://www.w3.org/2000/svg">
228
329
  <style>
229
330
  .text { font: 600 16px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${titleColor} }
230
331
  .small { font: 600 12px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor} }
@@ -232,7 +333,7 @@ const renderError = ({
232
333
  </style>
233
334
  <rect x="0.5" y="0.5" width="${
234
335
  ERROR_CARD_LENGTH - 1
235
- }" height="99%" rx="4.5" fill="${bgColor}" stroke="${borderColor}"/>
336
+ }" height="99%" rx="4.5" fill="${String(bgColor)}" stroke="${borderColor}"/>
236
337
  <text x="25" y="45" class="text">Something went wrong!${
237
338
  UPSTREAM_API_ERRORS.includes(secondaryMessage) || !show_repo_link
238
339
  ? ""
@@ -240,7 +341,7 @@ const renderError = ({
240
341
  }</text>
241
342
  <text data-testid="message" x="25" y="55" class="text small">
242
343
  <tspan x="25" dy="18">${encodeHTML(message)}</tspan>
243
- <tspan x="25" dy="18" class="gray">${secondaryMessage}</tspan>
344
+ <tspan x="25" dy="18" class="gray">${encodeHTML(secondaryMessage)}</tspan>
244
345
  </text>
245
346
  </svg>
246
347
  `;
@@ -250,11 +351,11 @@ const renderError = ({
250
351
  * Retrieve text length based on Segoe UI font.
251
352
  *
252
353
  * @see https://stackoverflow.com/a/48172630/10629172
253
- * @param {string} str String to measure.
254
- * @param {number} fontSize Font size.
255
- * @returns {number} Text length.
354
+ * @param str String to measure.
355
+ * @param fontSize Font size.
356
+ * @returns Text length.
256
357
  */
257
- const measureText = (str, fontSize = 10) => {
358
+ const measureText = (str: string, fontSize = 10): number => {
258
359
  // prettier-ignore
259
360
  const widths = [
260
361
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -306,7 +407,7 @@ const measureText = (str, fontSize = 10) => {
306
407
  return 1;
307
408
  }
308
409
  if (c.charCodeAt(0) < widths.length) {
309
- return widths[c.charCodeAt(0)];
410
+ return widths[c.charCodeAt(0)] ?? avg;
310
411
  } else {
311
412
  return avg;
312
413
  }
@@ -322,12 +423,16 @@ const measureText = (str, fontSize = 10) => {
322
423
  * The browser still does the real wrap inside the foreignObject; this is only
323
424
  * used to size the SVG.
324
425
  *
325
- * @param {string} text Text to split.
326
- * @param {number} fontSize Font size in px (matches `measureText`).
327
- * @param {number} maxWidth Available wrap width in px.
328
- * @returns {string[]} Estimated wrapped lines.
426
+ * @param text Text to split.
427
+ * @param fontSize Font size in px (matches `measureText`).
428
+ * @param maxWidth Available wrap width in px.
429
+ * @returns Estimated wrapped lines.
329
430
  */
330
- const splitWrappedText = (text, fontSize, maxWidth) => {
431
+ const splitWrappedText = (
432
+ text: string,
433
+ fontSize: number,
434
+ maxWidth: number,
435
+ ): Array<string> => {
331
436
  if (!text) {
332
437
  return [];
333
438
  }
@@ -342,15 +447,15 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
342
447
  // Korean Hangul (U+AC00–U+D7AF) is intentionally NOT in the CJK range
343
448
  // because Korean wraps at word boundaries by default in HTML.
344
449
  // ASCII whitespace is collapsed to a single space per CSS `white-space: normal;`
345
- text = text.replace(/[\t\n\r ]+/g, " ");
346
- const tokens = text.match(
450
+ const normalizedText = text.replace(/[\t\n\r ]+/g, " ");
451
+ const tokens = normalizedText.match(
347
452
  /\s|[\u3000-\u9FFF\uFF00-\uFFEF]|[^\s\u3000-\u9FFF\uFF00-\uFFEF]+/g,
348
453
  );
349
454
  if (!tokens) {
350
455
  return [];
351
456
  }
352
457
 
353
- const takeFittingSegment = (token, availableWidth) => {
458
+ const takeFittingSegment = (token: string, availableWidth: number) => {
354
459
  const characters = token.split("");
355
460
  let segment = "";
356
461
  let width = 0;
@@ -379,7 +484,7 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
379
484
  if (currentWidth === 0) {
380
485
  continue;
381
486
  }
382
- lines[lines.length - 1] += token;
487
+ lines[lines.length - 1] = (lines[lines.length - 1] ?? "") + token;
383
488
  currentWidth += measureText(token, fontSize);
384
489
  continue;
385
490
  }
@@ -389,7 +494,7 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
389
494
  while (remaining) {
390
495
  const w = measureText(remaining, fontSize);
391
496
  if (currentWidth + w <= maxWidth) {
392
- lines[lines.length - 1] += remaining;
497
+ lines[lines.length - 1] = (lines[lines.length - 1] ?? "") + remaining;
393
498
  currentWidth += w;
394
499
  break;
395
500
  }
@@ -402,7 +507,7 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
402
507
 
403
508
  // An atom wider than the box wraps mid-glyph (overflow-wrap: anywhere).
404
509
  const { segment, width } = takeFittingSegment(remaining, maxWidth);
405
- lines[lines.length - 1] += segment;
510
+ lines[lines.length - 1] = (lines[lines.length - 1] ?? "") + segment;
406
511
  currentWidth = width;
407
512
  remaining = remaining.slice(segment.length);
408
513
  }
@@ -415,13 +520,18 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
415
520
  * Estimate how many lines a string will wrap to when laid out greedily at the
416
521
  * given font size inside a box of width `maxWidth`, capped at `maxLines`.
417
522
  *
418
- * @param {string} text Text to estimate.
419
- * @param {number} fontSize Font size in px (matches `measureText`).
420
- * @param {number} maxWidth Available wrap width in px.
421
- * @param {number} maxLines Cap on the returned line count.
422
- * @returns {number} Estimated line count, at least 1, at most `maxLines`.
523
+ * @param text Text to estimate.
524
+ * @param fontSize Font size in px (matches `measureText`).
525
+ * @param maxWidth Available wrap width in px.
526
+ * @param maxLines Cap on the returned line count.
527
+ * @returns Estimated line count, at least 1, at most `maxLines`.
423
528
  */
424
- const countWrappedLines = (text, fontSize, maxWidth, maxLines) => {
529
+ const countWrappedLines = (
530
+ text: string,
531
+ fontSize: number,
532
+ maxWidth: number,
533
+ maxLines: number,
534
+ ): number => {
425
535
  return Math.min(
426
536
  Math.max(1, splitWrappedText(text, fontSize, maxWidth).length),
427
537
  maxLines,