Skip to content

Commit

Permalink
add fix decimals to total values
Browse files Browse the repository at this point in the history
  • Loading branch information
glitch-txs committed Apr 21, 2024
1 parent cfd4ebe commit 1cde02a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
14 changes: 10 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30459,8 +30459,8 @@ async function exec_vite_size({ branch } = {}) {
const filtered_output = JSON.parse(stdout_output[stdout_output.length - 1]);
const size_values = [['Name', 'Size (kb)', 'Gzip (kb)']];
for (let i = 0; i < filtered_output.length; i++) {
const _size = Number(parseFloat(filtered_output[i].size).toFixed(3));
const _gzip = Number(parseFloat(filtered_output[i].gzip).toFixed(3));
const _size = Number(filtered_output[i].size);
const _gzip = Number(filtered_output[i].gzip);
total_size[1] += _size;
total_size[2] += _gzip;
size_values.push([
Expand All @@ -30469,6 +30469,9 @@ async function exec_vite_size({ branch } = {}) {
_gzip
]);
}
;
total_size[1] = toFixed(total_size[1]);
total_size[2] = toFixed(total_size[2]);
size_values.push(total_size);
return {
status,
Expand All @@ -30484,8 +30487,8 @@ function calcDiff({ current, base }) {
const current_gzip = Number(current[current.length - 1]?.[2]);
const base_size = Number(base[base.length - 1]?.[1]);
const base_gzip = Number(base[base.length - 1]?.[2]);
let size = Math.abs(current_size - base_size);
let gzip = Math.abs(current_gzip - base_gzip);
let size = toFixed(Math.abs(current_size - base_size));
let gzip = toFixed(Math.abs(current_gzip - base_gzip));
if (current_size > base_size) {
size = '🔺+' + size;
}
Expand All @@ -30499,6 +30502,9 @@ function calcDiff({ current, base }) {
]
};
}
function toFixed(value, decimal = 3) {
return Number(parseFloat(value.toString()).toFixed(decimal));
}


/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-size-action",
"version": "1.0.2",
"version": "1.0.3",
"description": "",
"main": "index.js",
"type": "module",
Expand Down
19 changes: 13 additions & 6 deletions utils/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export async function exec_vite_size({ branch }: { branch?: string } = {}){

if(status > 0) return { status, size_values: undefined }

const total_size = ['Total Size', 0, 0]
const total_size: (number | string)[] = ['Total Size', 0, 0]
const filtered_output = JSON.parse(stdout_output[stdout_output.length - 1] as string)

const size_values: SizeValue = [['Name', 'Size (kb)', 'Gzip (kb)']]

for(let i = 0; i < filtered_output.length; i++){
const _size = Number(parseFloat(filtered_output[i].size).toFixed(3));
const _gzip = Number(parseFloat(filtered_output[i].gzip).toFixed(3));
const _size = Number(filtered_output[i].size);
const _gzip = Number(filtered_output[i].gzip);

(total_size[1] as number) += _size;
(total_size[2] as number) += _gzip;
Expand All @@ -48,7 +48,10 @@ export async function exec_vite_size({ branch }: { branch?: string } = {}){
_size,
_gzip
])
}
};

total_size[1] = toFixed(total_size[1] as number);
total_size[2] = toFixed(total_size[2] as number);
size_values.push(total_size)

return {
Expand All @@ -68,8 +71,8 @@ export function calcDiff({ current, base }: { current?: SizeValue, base?: SizeVa
const base_size = Number(base[base.length - 1]?.[1])
const base_gzip = Number(base[base.length - 1]?.[2])

let size: string | number = Math.abs(current_size - base_size)
let gzip: string | number = Math.abs(current_gzip - base_gzip)
let size: string | number = toFixed(Math.abs(current_size - base_size))
let gzip: string | number = toFixed(Math.abs(current_gzip - base_gzip))

if(current_size > base_size){
size = '🔺+' + size
Expand All @@ -85,4 +88,8 @@ export function calcDiff({ current, base }: { current?: SizeValue, base?: SizeVa
['Total Diff.', size, gzip]
]
}
}

function toFixed(value: string | number, decimal: number = 3){
return Number(parseFloat(value.toString()).toFixed(decimal))
}

0 comments on commit 1cde02a

Please sign in to comment.