aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-10-02 21:35:21 +0200
committermetamuffin <metamuffin@disroot.org>2023-10-02 21:35:21 +0200
commit78fe4b0459dd34737303ec97647a83cecfbd1dc1 (patch)
tree3f2d995629fe690b86630459f494ffdaa06f8d31
parent1a42804b2df0c443588863e77c1c4c619a33533b (diff)
downloadjshelper-78fe4b0459dd34737303ec97647a83cecfbd1dc1.tar
jshelper-78fe4b0459dd34737303ec97647a83cecfbd1dc1.tar.bz2
jshelper-78fe4b0459dd34737303ec97647a83cecfbd1dc1.tar.zst
formatting function
-rw-r--r--mod.ts3
-rw-r--r--src/show.ts17
2 files changed, 19 insertions, 1 deletions
diff --git a/mod.ts b/mod.ts
index d61e0e7..0657b94 100644
--- a/mod.ts
+++ b/mod.ts
@@ -5,6 +5,7 @@
*/
import { e } from "./src/element.ts";
import { OVar } from "./src/observable.ts";
+import * as show from "./src/show.ts";
-export { OVar, e };
+export { OVar, e, show };
diff --git a/src/show.ts b/src/show.ts
new file mode 100644
index 0000000..193a93b
--- /dev/null
+++ b/src/show.ts
@@ -0,0 +1,17 @@
+
+export function byte_size(x: number): string {
+ if (x > 1000000000) return (x / 1000000000).toFixed(1) + "G"
+ if (x > 1000000) return (x / 1000000).toFixed(1) + "M"
+ if (x > 1000) return (x / 1000).toFixed(1) + "k"
+ return x.toString()
+}
+
+export function duration(t: number): string {
+ if (t < 0) return "-" + duration(-t)
+ let h = 0, m = 0, s = 0;
+ while (t > 3600) t -= 3600, h++;
+ while (t > 60) t -= 60, m++;
+ while (t > 1) t -= 1, s++;
+ return (h ? h + "h" : "") + (m ? m + "m" : "") + (s ? s + "s" : "")
+}
+