@@ -70,13 +70,12 @@ export async function fetchOpenCollectiveSponsors(
7070
7171 sponsors . push ( ...( nodes || [ ] ) )
7272
73- if ( ( nodes . length ) !== 0 ) {
74- if ( totalCount > offset + nodes . length )
75- offset += nodes . length
76- else
77- offset = undefined
78- }
79- else { offset = undefined }
73+ if ( nodes . length === 0 )
74+ offset = undefined
75+ else if ( totalCount > offset + nodes . length )
76+ offset += nodes . length
77+ else
78+ offset = undefined
8079 } while ( offset )
8180 }
8281
@@ -99,13 +98,12 @@ export async function fetchOpenCollectiveSponsors(
9998 const totalCount = data . data . account . transactions . totalCount
10099
101100 monthlyTransactions . push ( ...( nodes || [ ] ) )
102- if ( ( nodes . length ) !== 0 ) {
103- if ( totalCount > offset + nodes . length )
104- offset += nodes . length
105- else
106- offset = undefined
107- }
108- else { offset = undefined }
101+ if ( nodes . length === 0 )
102+ offset = undefined
103+ else if ( totalCount > offset + nodes . length )
104+ offset += nodes . length
105+ else
106+ offset = undefined
109107 } while ( offset )
110108
111109 const sponsorships : [ string , Sponsorship ] [ ] = sponsors
@@ -139,12 +137,12 @@ export async function fetchOpenCollectiveSponsors(
139137 const transactionsBySponsorId : Map < string , Sponsorship > = monthlySponsorships . reduce ( ( map , [ id , sponsor ] ) => {
140138 const existingSponsor = map . get ( id )
141139 if ( existingSponsor ) {
142- const createdAt = new Date ( sponsor . createdAt ! )
143- const existingSponsorCreatedAt = new Date ( existingSponsor . createdAt ! )
140+ const createdAt = toDate ( sponsor . createdAt )
141+ const existingSponsorCreatedAt = toDate ( existingSponsor . createdAt )
144142 if ( createdAt >= existingSponsorCreatedAt )
145143 map . set ( id , sponsor )
146144
147- else if ( new Date ( existingSponsorCreatedAt . getFullYear ( ) , existingSponsorCreatedAt . getMonth ( ) , 1 ) === new Date ( createdAt . getFullYear ( ) , createdAt . getMonth ( ) , 1 ) )
145+ else if ( isSameMonth ( existingSponsorCreatedAt , createdAt ) )
148146 existingSponsor . monthlyDollars += sponsor . monthlyDollars
149147 }
150148 else { map . set ( id , sponsor ) }
@@ -156,8 +154,8 @@ export async function fetchOpenCollectiveSponsors(
156154 . reduce ( ( map , [ id , sponsor ] ) => {
157155 const existingSponsor = map . get ( id )
158156 if ( existingSponsor ) {
159- const createdAt = new Date ( sponsor . createdAt ! )
160- const existingSponsorCreatedAt = new Date ( existingSponsor . createdAt ! )
157+ const createdAt = toDate ( sponsor . createdAt )
158+ const existingSponsorCreatedAt = toDate ( existingSponsor . createdAt )
161159 if ( createdAt >= existingSponsorCreatedAt )
162160 map . set ( id , sponsor )
163161 }
@@ -195,7 +193,7 @@ function createSponsorFromOrder(order: any): [string, Sponsorship] | undefined {
195193 avatarUrl : order . fromAccount . imageUrl ,
196194 websiteUrl : normalizeUrl ( getBestUrl ( order . fromAccount . socialLinks ) ) ,
197195 linkUrl : `https://opencollective.com/${ slug } ` ,
198- socialLogins : getSocialLogins ( order . fromAccount . socialLinks , slug ) ,
196+ socialLogins : getSocialLogins ( slug , order . fromAccount . socialLinks ) ,
199197 } ,
200198 isOneTime : order . frequency === 'ONETIME' ,
201199 monthlyDollars,
@@ -242,14 +240,14 @@ function createSponsorFromTransaction(transaction: any, excludeOrders: string[],
242240 type : getAccountType ( account . type ) ,
243241 login : slug ,
244242 avatarUrl : account . imageUrl ,
245- websiteUrl : normalizeUrl ( getBestUrl ( account . socialLinks || [ ] ) ) ,
243+ websiteUrl : normalizeUrl ( getBestUrl ( account . socialLinks ?? [ ] ) ) ,
246244 linkUrl : `https://opencollective.com/${ slug } ` ,
247- socialLogins : getSocialLogins ( account . socialLinks || [ ] , slug ) ,
245+ socialLogins : getSocialLogins ( slug , account . socialLinks ) ,
248246 } ,
249247 isOneTime : transaction . order ?. frequency === 'ONETIME' ,
250248 monthlyDollars,
251249 privacyLevel : sponseesMode ? 'PUBLIC' : ( account . isIncognito ? 'PRIVATE' : 'PUBLIC' ) ,
252- tierName : transaction . order ?. tier ?. name || transaction . tier ?. name ,
250+ tierName : transaction . order ?. tier ?. name ?? transaction . tier ?. name ,
253251 createdAt : sponseesMode
254252 ? transaction . createdAt
255253 : transaction . order ?. frequency === 'ONETIME'
@@ -432,13 +430,21 @@ function getBestUrl(socialLinks: SocialLink[]): string | undefined {
432430 return urls [ 0 ]
433431}
434432
433+ function isSameMonth ( a : Date , b : Date ) {
434+ return a . getFullYear ( ) === b . getFullYear ( ) && a . getMonth ( ) === b . getMonth ( )
435+ }
436+
437+ function toDate ( value : string | undefined ) {
438+ return new Date ( value ?? Number . NaN )
439+ }
440+
435441const RE_GITHUB_URL = / g i t h u b \. c o m \/ ( [ ^ / ] * ) /
436442
437- function getSocialLogins ( socialLinks : SocialLink [ ] = [ ] , opencollectiveLogin : string ) : Record < string , string > {
443+ function getSocialLogins ( opencollectiveLogin : string , socialLinks : SocialLink [ ] = [ ] ) : Record < string , string > {
438444 const socialLogins : Record < string , string > = { }
439445 for ( const link of socialLinks ) {
440446 if ( link . type === 'GITHUB' ) {
441- const login = link . url . match ( RE_GITHUB_URL ) ?. [ 1 ]
447+ const login = RE_GITHUB_URL . exec ( link . url ) ?. [ 1 ]
442448 if ( login )
443449 socialLogins . github = login
444450 }
0 commit comments