ef.js

Declarative DOM helper experiment

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
import { gzipSync } from "bun";
import chalk from "chalk";

export function bytesToString(bytes: number, color: boolean = false): string {
  // I hate common trend of using SI for byte sizes. It's not bit-shiftable.
  const kB = 1_000;

  const num = bytes >= kB ? (bytes / kB).toFixed(2) : bytes.toString(10);
  const unit = bytes >= kB ? "kB" : "B";

  if (!color) {
    return num + unit;
  }

  return chalk.yellow(num) + chalk.cyan(unit);
}

export async function getGzipedSize(
  buf: ArrayBuffer | Uint8Array,
): Promise<number> {
  return gzipSync(buf instanceof Uint8Array ? buf : Buffer.from(buf))
    .byteLength;
}