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