~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/common/util.js

  • Committer: mattgiuca
  • Date: 2008-01-11 05:11:08 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:180
util: Added function nice_filesize.

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
    return elem;
65
65
}
66
66
 
 
67
/** Given a number of bytes, returns a string representing the file size in a
 
68
 * human-readable format.
 
69
 * eg. nice_filesize(6) -> "6 bytes"
 
70
 *     nice_filesize(81275) -> "79.4 kB"
 
71
 *     nice_filesize(13498346) -> "12.9 MB"
 
72
 * \param bytes Number of bytes. Must be an integer.
 
73
 * \return String.
 
74
 */
 
75
function nice_filesize(bytes)
 
76
{
 
77
    var size;
 
78
    if (bytes < 1024)
 
79
        return bytes.toString() + " bytes";
 
80
    size = bytes / 1024;
 
81
    if (size < 1024)
 
82
        return size.toFixed(1) + " kB";
 
83
    size = size / 1024;
 
84
    if (size < 1024)
 
85
        return size.toFixed(1) + " MB";
 
86
    size = size / 1024;
 
87
    return size.toFixed(1) + " GB";
 
88
}
 
89
 
67
90
/** Given a URL, returns an object containing a number of attributes
68
91
 * describing the components of the URL, similar to CGI request variables.
69
92
 * The object has the following attributes: