1+ // @ts -check
12const axios = require ( "axios" ) ;
23const wrap = require ( "word-wrap" ) ;
34const themes = require ( "../../themes" ) ;
45const toEmoji = require ( "emoji-name-map" ) ;
56
7+ /**
8+ * @param {string } message
9+ * @param {string } secondaryMessage
10+ * @returns {string }
11+ */
612const renderError = ( message , secondaryMessage = "" ) => {
713 return `
814 <svg width="495" height="120" viewBox="0 0 495 120" fill="none" xmlns="http://www.w3.org/2000/svg">
@@ -21,7 +27,11 @@ const renderError = (message, secondaryMessage = "") => {
2127 ` ;
2228} ;
2329
24- // https://stackoverflow.com/a/48073476/10629172
30+ /**
31+ * @see https://stackoverflow.com/a/48073476/10629172
32+ * @param {string } str
33+ * @returns {string }
34+ */
2535function encodeHTML ( str ) {
2636 return str
2737 . replace ( / [ \u00A0 - \u9999 < > & ] (? ! # ) / gim, ( i ) => {
@@ -30,18 +40,29 @@ function encodeHTML(str) {
3040 . replace ( / \u0008 / gim, "" ) ;
3141}
3242
43+ /**
44+ * @param {number } num
45+ */
3346function kFormatter ( num ) {
3447 return Math . abs ( num ) > 999
35- ? Math . sign ( num ) * ( Math . abs ( num ) / 1000 ) . toFixed ( 1 ) + "k"
48+ ? Math . sign ( num ) * parseFloat ( ( Math . abs ( num ) / 1000 ) . toFixed ( 1 ) ) + "k"
3649 : Math . sign ( num ) * Math . abs ( num ) ;
3750}
3851
52+ /**
53+ * @param {string } hexColor
54+ * @returns {boolean }
55+ */
3956function isValidHexColor ( hexColor ) {
4057 return new RegExp (
4158 / ^ ( [ A - F a - f 0 - 9 ] { 8 } | [ A - F a - f 0 - 9 ] { 6 } | [ A - F a - f 0 - 9 ] { 3 } | [ A - F a - f 0 - 9 ] { 4 } ) $ / ,
4259 ) . test ( hexColor ) ;
4360}
4461
62+ /**
63+ * @param {string } value
64+ * @returns {boolean | string }
65+ */
4566function parseBoolean ( value ) {
4667 if ( value === "true" ) {
4768 return true ;
@@ -52,19 +73,37 @@ function parseBoolean(value) {
5273 }
5374}
5475
76+ /**
77+ * @param {string } str
78+ */
5579function parseArray ( str ) {
5680 if ( ! str ) return [ ] ;
5781 return str . split ( "," ) ;
5882}
5983
84+ /**
85+ * @param {number } number
86+ * @param {number } min
87+ * @param {number } max
88+ */
6089function clampValue ( number , min , max ) {
90+ // @ts -ignore
91+ if ( Number . isNaN ( parseInt ( number ) ) ) return min ;
6192 return Math . max ( min , Math . min ( number , max ) ) ;
6293}
6394
95+ /**
96+ * @param {string[] } colors
97+ */
6498function isValidGradient ( colors ) {
6599 return isValidHexColor ( colors [ 1 ] ) && isValidHexColor ( colors [ 2 ] ) ;
66100}
67101
102+ /**
103+ * @param {string } color
104+ * @param {string } fallbackColor
105+ * @returns {string | string[] }
106+ */
68107function fallbackColor ( color , fallbackColor ) {
69108 let colors = color . split ( "," ) ;
70109 let gradient = null ;
@@ -79,7 +118,12 @@ function fallbackColor(color, fallbackColor) {
79118 ) ;
80119}
81120
121+ /**
122+ * @param {import('axios').AxiosRequestConfig['data'] } data
123+ * @param {import('axios').AxiosRequestConfig['headers'] } headers
124+ */
82125function request ( data , headers ) {
126+ // @ts -ignore
83127 return axios ( {
84128 url : "https://api.github.com/graphql" ,
85129 method : "post" ,
@@ -92,8 +136,8 @@ function request(data, headers) {
92136 * @param {object } props
93137 * @param {string[] } props.items
94138 * @param {number } props.gap
95- * @param {number[] } props.sizes
96- * @param {"column" | "row" } props.direction
139+ * @param {number[]?= } props.sizes
140+ * @param {"column" | "row"?= } props.direction
97141 *
98142 * @returns {string[] }
99143 *
@@ -115,14 +159,27 @@ function flexLayout({ items, gap, direction, sizes = [] }) {
115159 } ) ;
116160}
117161
118- // returns theme based colors with proper overrides and defaults
162+ /**
163+ * @typedef {object } CardColors
164+ * @prop {string } title_color
165+ * @prop {string } text_color
166+ * @prop {string } icon_color
167+ * @prop {string } bg_color
168+ * @prop {string } border_color
169+ * @prop {keyof typeof import('../../themes')?= } fallbackTheme
170+ * @prop {keyof typeof import('../../themes')?= } theme
171+ */
172+ /**
173+ * returns theme based colors with proper overrides and defaults
174+ * @param {CardColors } options
175+ */
119176function getCardColors ( {
120177 title_color,
121178 text_color,
122179 icon_color,
123180 bg_color,
124- theme,
125181 border_color,
182+ theme,
126183 fallbackTheme = "default" ,
127184} ) {
128185 const defaultTheme = themes [ fallbackTheme ] ;
@@ -157,6 +214,12 @@ function getCardColors({
157214 return { titleColor, iconColor, textColor, bgColor, borderColor } ;
158215}
159216
217+ /**
218+ * @param {string } text
219+ * @param {number } width
220+ * @param {number } maxLines
221+ * @returns {string[] }
222+ */
160223function wrapTextMultiline ( text , width = 60 , maxLines = 3 ) {
161224 const wrapped = wrap ( encodeHTML ( text ) , { width } )
162225 . split ( "\n" ) // Split wrapped lines to get an array of lines
@@ -193,6 +256,10 @@ const SECONDARY_ERROR_MESSAGES = {
193256} ;
194257
195258class CustomError extends Error {
259+ /**
260+ * @param {string } message
261+ * @param {string } type
262+ */
196263 constructor ( message , type ) {
197264 super ( message ) ;
198265 this . type = type ;
@@ -203,7 +270,12 @@ class CustomError extends Error {
203270 static USER_NOT_FOUND = "USER_NOT_FOUND" ;
204271}
205272
206- // https://stackoverflow.com/a/48172630/10629172
273+ /**
274+ * @see https://stackoverflow.com/a/48172630/10629172
275+ * @param {string } str
276+ * @param {number } fontSize
277+ * @returns
278+ */
207279function measureText ( str , fontSize = 10 ) {
208280 // prettier-ignore
209281 const widths = [
@@ -237,6 +309,8 @@ function measureText(str, fontSize = 10) {
237309 . reduce ( ( cur , acc ) => acc + cur ) * fontSize
238310 ) ;
239311}
312+
313+ /** @param {string } name */
240314const lowercaseTrim = ( name ) => name . toLowerCase ( ) . trim ( ) ;
241315
242316/**
0 commit comments