fix(bundle): better size output
@@ -445,15 +445,46 @@ async fn bundle_command(
let output_bytes = output_string.as_bytes();
let output_len = output_bytes.len();
deno_fs::write_file(out_file_, output_bytes, 0o666)?;
- // TODO(bartlomieju): add "humanFileSize" method
- info!("{} bytes emitted.", output_len);
+ info!("{} emitted.", human_size(output_len as f64));
} else {
println!("{}", output_string);
}
-
Ok(())
}
+fn human_size(bytse: f64) -> String {
+ let negative = if bytse.is_sign_positive() { "" } else { "-" };
+ let bytse = bytse.abs();
+ let units = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
+ if bytse < 1_f64 {
+ return format!("{}{} {}", negative, bytse, "Bytes");
+ }
+ let delimiter = 1024_f64;
+ let exponent = std::cmp::min(
+ (bytse.ln() / delimiter.ln()).floor() as i32,
+ (units.len() - 1) as i32,
+ );
+ let pretty_bytes = format!("{:.2}", bytse / delimiter.powi(exponent))
+ .parse::<f64>()
+ .unwrap()
+ * 1_f64;
+ let unit = units[exponent as usize];
+ format!("{}{} {}", negative, pretty_bytes, unit)
+}
+
+#[test]
+fn human_size_test() {
+ assert_eq!(human_size(16_f64), "16 Bytes");
+ assert_eq!(human_size((16 * 1024) as f64), "16 KB");
+ assert_eq!(human_size((16 * 1024 * 1024) as f64), "16 MB");
+ assert_eq!(human_size(16_f64 * 1024_f64.powf(3.0)), "16 GB");
+ assert_eq!(human_size(16_f64 * 1024_f64.powf(4.0)), "16 TB");
+ assert_eq!(human_size(16_f64 * 1024_f64.powf(5.0)), "16 PB");
+ assert_eq!(human_size(16_f64 * 1024_f64.powf(6.0)), "16 EB");
+ assert_eq!(human_size(16_f64 * 1024_f64.powf(7.0)), "16 ZB");
+ assert_eq!(human_size(16_f64 * 1024_f64.powf(8.0)), "16 YB");
+}
+
async fn doc_command(
flags: Flags,
source_file: Option<String>,
