@@ -8,7 +8,7 @@ function formatBytes(numBytes) {
8
8
unitIndex ++ ;
9
9
}
10
10
11
- // Format the output to 3 decimal places
11
+ // Format the output to 0 decimal places
12
12
return `${ numBytes . toFixed ( 0 ) } ${ units [ unitIndex ] } ` ;
13
13
}
14
14
@@ -27,11 +27,10 @@ async function fetchGistData() {
27
27
// Print total bytes downloaded
28
28
const formattedNumber = total_bytes . toLocaleString ( ) ;
29
29
const wrappedNumber = Array . from ( formattedNumber ) . map ( char => ( / \d / . test ( char ) ? `<span>${ char } </span>` :`<em>${ char } </em>` ) ) . join ( '' ) ;
30
- document . querySelectorAll ( "# kaggle_stat") [ 0 ] . innerHTML = wrappedNumber + " bytes downloaded" ;
30
+ document . getElementById ( " kaggle_stat") . innerHTML = wrappedNumber + " bytes downloaded" ;
31
31
32
32
// Print total dataset size
33
33
const formattedSize = formatBytes ( total_size ) ;
34
- //const wrappedSize = Array.from(formattedSize).map(char => (/\s/.test(char) ? `${char}`:`<span>${char}</span>`)).join('');
35
34
const wrappedSize = formattedSize . split ( / ( \s + ) / ) . map ( chunk => {
36
35
if ( / ^ \d + $ / . test ( chunk ) )
37
36
return Array . from ( chunk ) . map ( char => `<span>${ char } </span>` ) . join ( '' ) ;
@@ -40,8 +39,24 @@ async function fetchGistData() {
40
39
else
41
40
return chunk ;
42
41
} ) . join ( '' ) ;
43
- document . querySelectorAll ( "#kaggle_size" ) [ 0 ] . innerHTML = wrappedSize ;
44
- document . querySelectorAll ( "#data_size" ) [ 0 ] . innerHTML = formattedSize ;
42
+ document . getElementById ( "kaggle_size" ) . innerHTML = wrappedSize ;
43
+ document . getElementById ( "data_size" ) . innerHTML = formattedSize ;
44
+
45
+ // Extra formatting thanks matthias
46
+ let children = Array . from ( document . getElementById ( "kaggle_stat" ) . children ) ;
47
+ let firstEmIndex = children . findIndex ( child => child . tagName . toLowerCase ( ) === 'em' ) ;
48
+ let byteCount = firstEmIndex === - 1
49
+ ? children . filter ( child => child . tagName . toLowerCase ( ) === 'span' ) . length
50
+ : children . slice ( 0 , firstEmIndex ) . filter ( child => child . tagName . toLowerCase ( ) === 'span' ) . length ;
51
+ let sizeCount = document . querySelectorAll ( "#kaggle_size span" ) . length - 1 ;
52
+ if ( sizeCount < byteCount ) {
53
+ let prependZeros = Array ( byteCount - sizeCount ) . fill ( '<span class="zero">0</span>' ) . join ( '' ) ;
54
+ document . getElementById ( "kaggle_size" ) . innerHTML = prependZeros + wrappedSize ;
55
+ } else if ( sizeCount > byteCount ) {
56
+ let prependZeros = Array ( sizeCount - byteCount ) . fill ( '<span class="zero">0</span>' ) . join ( '' ) ;
57
+ document . getElementById ( "kaggle_stat" ) . innerHTML = prependZeros + wrappedNumber + " bytes downloaded" ;
58
+ }
59
+
45
60
} catch ( error ) {
46
61
console . log ( "Failed to fetch Gist data:" , error ) ;
47
62
}
0 commit comments