new module to enable editing and deleting of bookmarks
[conkeror/arlinius.git] / modules / pretty-print.js
blob104ef91932b79429aeaf206c89c2d9251cab8389
1 /**
2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
5 * COPYING file.
6 **/
8 function pretty_print_value (value) {
9 if (value === undefined)
10 return "undefined";
11 if (value === null)
12 return "null";
13 if (typeof(value) == "object")
14 return value.toSource();
15 if (typeof(value) == "function")
16 return value.toString();
17 if (typeof(value) == "string") {
18 let s = value.toSource();
19 // toSource returns: (new String("<blah>"))
20 // we want just: "<blah>"
21 return s.substring(12, s.length - 2);
23 return new String(value);
26 function pretty_print_file_size (val) {
27 const GIBI = 1073741824; /* 2^30 */
28 const MEBI = 1048576; /* 2^20 */
29 const KIBI = 1024; /* 2^10 */
30 var suffix, div;
31 if (val < KIBI) {
32 div = 1;
33 suffix = "B";
34 } else if (val < MEBI) {
35 suffix = "KiB";
36 div = KIBI;
37 } else if (val < GIBI) {
38 suffix = "MiB";
39 div = MEBI;
40 } else {
41 suffix = "GiB";
42 div = GIBI;
44 val = val / div;
45 var precision = 2;
46 if (val > 10)
47 precision = 1;
48 if (val > 100)
49 precision = 0;
50 return [val.toFixed(precision), suffix];
53 function pretty_print_time (val) {
54 val = Math.round(val);
55 var seconds = val % 60;
56 val = Math.floor(val / 60);
57 var minutes = val % 60;
58 var hours = Math.floor(val / 60);
59 var parts = [];
61 if (hours > 1)
62 parts.push(hours + " hours");
63 else if (hours == 1)
64 parts.push("1 hour");
66 if (minutes > 1)
67 parts.push(minutes + " minutes");
68 else if (minutes == 1)
69 parts.push("1 minute");
71 if (minutes <= 1 && hours == 0) {
72 if (seconds != 1)
73 parts.push(seconds + " seconds");
74 else
75 parts.push("1 second");
78 return parts.join(", ");
81 provide("pretty-print");