aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage-user/core/converter/bytes-converter.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/montage-user/core/converter/bytes-converter.js')
-rwxr-xr-xnode_modules/montage-user/core/converter/bytes-converter.js123
1 files changed, 123 insertions, 0 deletions
diff --git a/node_modules/montage-user/core/converter/bytes-converter.js b/node_modules/montage-user/core/converter/bytes-converter.js
new file mode 100755
index 00000000..1626f352
--- /dev/null
+++ b/node_modules/montage-user/core/converter/bytes-converter.js
@@ -0,0 +1,123 @@
1/* <copyright>
2 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4 (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5 </copyright> */
6/**
7 @module montage/core/converter/bytes-converter
8 @requires montage/core/core
9 @requires montage/core/converter/converter
10 @requires montage/core/converter/number-converter
11*/
12var Montage = require("montage").Montage;
13var Converter = require('core/converter/converter').Converter;
14var _numericValueToString = require("core/converter/number-converter")._numericValueToString;
15var _stringToNumericValue = require("core/converter/number-converter")._stringToNumericValue;
16var NUMERIC_SCALES_BINARY_ = require("core/converter/number-converter").NUMERIC_SCALES_BINARY_;
17var isDef = require('core/converter/converter').isDef;
18
19/**
20 @private
21*/
22var NUMERIC_SCALE_PREFIXES_BYTES = [
23 'P', 'T', 'G', 'M', 'K', '', 'm', 'u', 'n'
24];
25
26/**
27 Converts a string to number of bytes, taking into account the units.
28 Binary conversion.
29 @function
30 @param {string} stringValue String to be converted to numeric value.
31 @return {number} Numeric value for string.
32 @private
33 */
34var stringToNumBytes = function(stringValue) {
35 return _stringToNumericValue(stringValue, NUMERIC_SCALES_BINARY_);
36};
37
38/**
39 Converts number of bytes to string representation. Binary conversion.
40 Default is to return the additional 'B' suffix, e.g. '10.5KB' to minimize confusion with counts that are scaled by powers of 1000.
41 @function
42 @param {Number} val Value to be converted.
43 @param {Number} opt_decimals The number of decimals to use. Defaults to 2.
44 @param {Boolean} opt_suffix If true, include trailing 'B' in returned string. Default is true.
45 @return {String} String representation of number of bytes.
46 @private
47 */
48var numBytesToString = function(val, opt_decimals, opt_suffix) {
49 var suffix = '';
50 if (!isDef(opt_suffix) || opt_suffix) {
51 suffix = 'B';
52 }
53 return _numericValueToString(val, NUMERIC_SCALES_BINARY_, opt_decimals, suffix, NUMERIC_SCALE_PREFIXES_BYTES);
54};
55
56/**
57 Formats a number of bytes in human readable form: 54, 450K, 1.3M, 5G etc.
58 @function
59 @param {Number} bytes The number of bytes to show.
60 @param {Number} opt_decimals The number of decimals to use. Defaults to 2.
61 @return {String} The human readable form of the byte size.
62 @private
63 */
64var fileSize = function(bytes, opt_decimals) {
65 return numBytesToString(bytes, opt_decimals, false);
66};
67
68/**
69 @class module:montage/core/converter/bytes-converter.BytesConverter
70 @classdesc Converts a numeric value to byte format (for example, 2048 is converted to 2MB).
71 @extends module:montage/core/converter.Converter
72 */
73exports.BytesConverter = Montage.create(Converter, /** @lends module:montage/core/converter/bytes-converter.BytesConverter# */ {
74
75 /**
76 The number of decimals to include in the formatted value. Default is 2.
77 @type {Property}
78 @default {Number} 2
79 */
80 decimals: {
81 value: 2
82 },
83 /**
84 Converts the specified value to byte format.
85 @function
86 @param {Property} v The value to format.
87 @returns {String} The value converted to byte format.
88 @example
89 var Converter= require("core/converter/converter").Converter;
90 var BytesConverter = require("core/converter/converter").BytesConverter;
91 var bytes = "12341234";
92 var byteconverter = BytesConverter.create();
93 console.log("Converted: " + byteconverter.convert(bytes));
94 console.log("Reverted: " + byteconverter.revert(bytes));
95 // Converted: 11.77MB
96 // Reverted: 12341234
97 */
98 convert: {
99 value: function(v) {
100 return fileSize(v, this.decimals);
101 }
102 },
103 /**
104 Reverts a formatted byte string to a standard number.
105 @function
106 @param {String} v The value to revert.
107 @returns {String} v
108 @see module:montage/converter.BytesConverter#convert
109 @example
110 var Converter= require("core/converter/converter").Converter;
111 var BytesConverter = require("core/converter/converter").BytesConverter;
112 var bytes = "11.77MB";
113 var byteconverter = BytesConverter.create();
114 console.log("Reverted: " + byteconverter.revert(bytes));
115 // Reverted: 12341234
116 */
117 revert: {
118 value: function(v) {
119 return v;
120 }
121 }
122});
123