diff options
Diffstat (limited to 'node_modules/montage-user/core/converter')
9 files changed, 3594 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 | */ | ||
12 | var Montage = require("montage").Montage; | ||
13 | var Converter = require('core/converter/converter').Converter; | ||
14 | var _numericValueToString = require("core/converter/number-converter")._numericValueToString; | ||
15 | var _stringToNumericValue = require("core/converter/number-converter")._stringToNumericValue; | ||
16 | var NUMERIC_SCALES_BINARY_ = require("core/converter/number-converter").NUMERIC_SCALES_BINARY_; | ||
17 | var isDef = require('core/converter/converter').isDef; | ||
18 | |||
19 | /** | ||
20 | @private | ||
21 | */ | ||
22 | var 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 | */ | ||
34 | var 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 | */ | ||
48 | var 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 | */ | ||
64 | var 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 | */ | ||
73 | exports.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 | |||
diff --git a/node_modules/montage-user/core/converter/converter.js b/node_modules/montage-user/core/converter/converter.js new file mode 100755 index 00000000..abce44e2 --- /dev/null +++ b/node_modules/montage-user/core/converter/converter.js | |||
@@ -0,0 +1,144 @@ | |||
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 | Provides common conversion, validation, and formatting functions for different types of values. | ||
8 | @module montage/core/converter/converter | ||
9 | @requires montage/core/core | ||
10 | */ | ||
11 | var Montage = require("montage").Montage; | ||
12 | |||
13 | |||
14 | var FUNCTION_CLASS = '[object Function]', | ||
15 | BOOLEAN_CLASS = '[object Boolean]', | ||
16 | NUMBER_CLASS = '[object Number]', | ||
17 | STRING_CLASS = '[object String]', | ||
18 | ARRAY_CLASS = '[object Array]', | ||
19 | DATE_CLASS = '[object Date]'; | ||
20 | |||
21 | var _toString = Object.prototype.toString; | ||
22 | |||
23 | // TODO should maybe move these into String.isString and Number.isNumber to parallel Array.isArray | ||
24 | |||
25 | /** | ||
26 | @exports module:montage/core/converter#isString | ||
27 | @function | ||
28 | */ | ||
29 | var isString = function(object) { | ||
30 | return _toString.call(object) === STRING_CLASS; | ||
31 | }; | ||
32 | exports.isString = isString; | ||
33 | |||
34 | /** | ||
35 | @exports module:montage/core/converter#isNumber | ||
36 | @function | ||
37 | */ | ||
38 | var isNumber = function(object) { | ||
39 | return _toString.call(object) === NUMBER_CLASS; | ||
40 | }; | ||
41 | exports.isNumber = isNumber; | ||
42 | |||
43 | |||
44 | /** | ||
45 | @exports module:montage/core/converter#isDef | ||
46 | @function | ||
47 | */ | ||
48 | var isDef = function(obj) { | ||
49 | return (obj && typeof obj !== 'undefined'); | ||
50 | }; | ||
51 | exports.isDef = isDef; | ||
52 | |||
53 | |||
54 | var startsWith = exports.startsWith = function(str) { | ||
55 | return str.lastIndexOf(prefix, 0) === 0; | ||
56 | }; | ||
57 | var endsWith = exports.endsWith = function(str) { | ||
58 | var l = str.length - suffix.length; | ||
59 | return l >= 0 && str.indexOf(suffix, l) == l; | ||
60 | }; | ||
61 | |||
62 | /** | ||
63 | Truncates a string to a certain length and adds '...' if necessary.<br> | ||
64 | The length also accounts for the ellipsis, so a maximum length of 10 and a string<br> | ||
65 | 'Hello World!' produces 'Hello W...'. | ||
66 | @function | ||
67 | @param {String} str The string to truncate. | ||
68 | @param {Number} chars Max number of characters. | ||
69 | @param {Boolean} opt_protectEscapedCharacters Whether to protect escaped characters from being cut off in the middle. | ||
70 | @return {String} The truncated {@code str} string. | ||
71 | */ | ||
72 | var truncate = exports.truncate = function(str, chars, opt_protectEscapedCharacters) { | ||
73 | if (opt_protectEscapedCharacters) { | ||