@@ -562,7 +562,6 @@ function storeConvert(size, idx=0) {
562562 if ( size <= 0 ) {
563563 return "0" ;
564564 }
565-
566565 while ( idx < ( store_units . length - 1 ) && size > 1024 ) {
567566 size /= 1024 ;
568567 idx ++ ;
@@ -577,6 +576,17 @@ function decConvert(n, allowed_pre=true) {
577576 return idx === 0 ? n : n . toFixed ( 1 ) + dec_units [ idx ] ;
578577}
579578
579+ function sum ( arr ) {
580+ switch ( arr . length ) {
581+ case 0 :
582+ return 0 ;
583+ case 1 :
584+ return arr [ 0 ] ;
585+ default :
586+ return arr . reduce ( ( a , b ) => a + b ) ;
587+ }
588+ }
589+
580590async function getLanguage ( user , repo ) {
581591 return await cache . requestWithCache ( `/repos/${ user } /${ repo } /languages` ) ;
582592}
@@ -598,16 +608,15 @@ async function langStatistics(queue) {
598608}
599609
600610function langHandler ( langs ) {
601- langs = { ...langs } ; // 这里有一个很神奇的关于指针的Bug的回忆
602- // (缓存与操作的对象指向一个Array, 导致了奇妙的事情发生, 有意者可以亲自拉下代码复刻这一[特性])
603- const val_arr = sort ( Object . values ( langs ) ) . reverse ( ) ;
604- const total = val_arr . length ? val_arr . reduce ( ( before , after ) => before + after ) : 0 ;
611+ langs = { ...langs } ; // deep copy
612+ const arr = sort ( Object . values ( langs ) ) . reverse ( ) ;
613+ const total = sum ( arr ) ;
605614 for ( let k in langs ) {
606615 langs [ langs [ k ] ] = k ;
607616 delete langs [ k ] ;
608617 }
609618 let cursor = 0 ;
610- return val_arr . map ( key => {
619+ return arr . map ( key => {
611620 const lang = langs [ key ] ;
612621 const ratio = key / total ;
613622 cursor += ratio ;
@@ -626,10 +635,10 @@ async function getAccount(username, dark=false) {
626635 return {
627636 dark : dark ,
628637 locs : response [ 'location' ] ,
629- stars : repos . map ( repo => repo [ 'stargazers_count' ] ) . reduce ( ( a , b ) => a + b ) ,
630- forks : repos . map ( repo => repo [ 'forks_count' ] ) . reduce ( ( a , b ) => a + b ) ,
631- issues : repos . map ( repo => repo [ 'open_issues_count' ] ) . reduce ( ( a , b ) => a + b ) ,
632- watchers : repos . map ( repo => repo [ 'watchers_count' ] ) . reduce ( ( a , b ) => a + b ) ,
638+ stars : decConvert ( sum ( repos . map ( repo => repo [ 'stargazers_count' ] ) ) ) ,
639+ forks : decConvert ( sum ( repos . map ( repo => repo [ 'forks_count' ] ) ) ) ,
640+ issues : decConvert ( sum ( repos . map ( repo => repo [ 'open_issues_count' ] ) ) ) ,
641+ watchers : decConvert ( sum ( repos . map ( repo => repo [ 'watchers_count' ] ) ) ) ,
633642 username : username ,
634643 followers : decConvert ( response [ 'followers' ] ) ,
635644 repos : response [ 'public_repos' ] ,
@@ -641,18 +650,18 @@ async function getAccount(username, dark=false) {
641650}
642651
643652async function getRepository ( username , repo , dark = false ) {
644- // get releases (700ms): ( await cache.requestWithCache(`/repos/${username}/${repo}/releases`)).length
645- const info = await cache . requestWithCache ( `/repos/ ${ username } / ${ repo } ` ) ;
653+ const res = await cache . requestWithCache ( `/repos/${ username } /${ repo } ` ) ;
654+ const license = res [ 'license' ] ;
646655 return {
647656 dark : dark ,
648657 username : username ,
649658 repo : repo ,
650- size : storeConvert ( info [ 'size' ] , 1 ) ,
651- forks : decConvert ( info [ 'forks' ] ) ,
652- stars : decConvert ( info [ 'stargazers_count' ] ) ,
653- watchers : decConvert ( info [ 'watchers_count' ] ) ,
654- license : info [ ' license' ] [ 'spdx_id' ] ,
655- color : lang_colors [ info [ 'language' ] ] ,
659+ size : storeConvert ( res [ 'size' ] , 1 ) ,
660+ forks : decConvert ( res [ 'forks' ] ) ,
661+ stars : decConvert ( res [ 'stargazers_count' ] ) ,
662+ watchers : decConvert ( res [ 'watchers_count' ] ) ,
663+ license : license ? license [ 'spdx_id' ] : "Empty" ,
664+ color : lang_colors [ res [ 'language' ] ] ,
656665 langs : langHandler ( await getLanguage ( username , repo ) ) ,
657666 } ;
658667}
0 commit comments