export const utf8 = new TextDecoder(); export const looksBinary = (data: Uint8Array) => data.subarray(0, 8192).includes(0); // mirrors excluded_directory / excluded_file in src/languages.rs; keep them in sync const GENERATED_DIRECTORIES = new Set([ "node_modules", "vendor", "third_party", "third-party", "target", "dist", "build", "coverage", ".next", ".nuxt", ".output", ".yarn", ".gradle", ".terraform", ".dart_tool", ".direnv", ".venv", "venv", "__pycache__", "pods", "carthage", "generated", ]); const GENERATED_FILES = new Set([ "cargo.lock", "package-lock.json", "npm-shrinkwrap.json", "yarn.lock", "pnpm-lock.yaml", "bun.lock", "bun.lockb", "deno.lock", "composer.lock", "gemfile.lock", "go.sum", "poetry.lock", "uv.lock", "pipfile.lock", "pdm.lock", "flake.lock", "mix.lock", "pubspec.lock", "package.resolved", "packages.lock.json", "gradle.lockfile", "podfile.lock", "cartfile.resolved", ]); const GENERATED_SUFFIXES = [ ".lock", ".min.js", ".min.css", ".g.dart", ".pb.go", ".pb.rs", ".pb.cc", ".pb.h", "_pb2.py", ".designer.cs", ]; export function looksGenerated(path: string): boolean { const segments = path.toLowerCase().split("/"); const name = segments.pop()!; return segments.some(dir => GENERATED_DIRECTORIES.has(dir)) || GENERATED_FILES.has(name) || GENERATED_SUFFIXES.some(suffix => name.endsWith(suffix)) || name.includes(".generated.") || name.includes("_generated."); } /** mirrors human_size in src/render.rs */ export function humanSize(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1 << 20) return `${(bytes / 1024).toFixed(1)} KiB`; return `${(bytes / (1 << 20)).toFixed(1)} MiB`; } export const plural = (n: number, noun: string) => `${n} ${noun}${n === 1 ? "" : "s"}`; /** the identity's own timezone, like git log */ export function identityDate(id: { time: number; tz: string }): string { const offset = (Number(id.tz.slice(0, 3)) * 60 + Number(id.tz[0] + id.tz.slice(3))) * 60000; const local = new Date(id.time * 1000 + offset).toISOString(); return `${local.slice(0, 10)} ${local.slice(11, 16)} ${id.tz}`; }