aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/core/converter/number-converter.js
diff options
context:
space:
mode:
authorPierre Frisch2011-12-22 07:25:50 -0800
committerValerio Virgillito2012-01-27 11:18:17 -0800
commitb89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch)
tree0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /node_modules/montage/core/converter/number-converter.js
parent2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff)
downloadninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'node_modules/montage/core/converter/number-converter.js')
-rwxr-xr-xnode_modules/montage/core/converter/number-converter.js390
1 files changed, 390 insertions, 0 deletions
diff --git a/node_modules/montage/core/converter/number-converter.js b/node_modules/montage/core/converter/number-converter.js
new file mode 100755
index 00000000..f440f9a2
--- /dev/null
+++ b/node_modules/montage/core/converter/number-converter.js
@@ -0,0 +1,390 @@
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// Number and String formatting functions from Google Closure Library - http://code.google.com/closure/library/
8// /library/format/format.js
9
10// Copyright 2010 The Closure Library Authors. All Rights Reserved.
11//
12// Licensed under the Apache License, Version 2.0 (the "License");
13// you may not use this file except in compliance with the License.
14// You may obtain a copy of the License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the License is distributed on an "AS-IS" BASIS,
20// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21// See the License for the specific language governing permissions and
22// limitations under the License.
23//
24/**
25 @module montage/core/converter/number-converter
26 @requires montage/core/core
27 @requires montage/core/converter/converter
28*/
29var Montage = require("montage").Montage;
30var Converter = require('core/converter/converter').Converter;
31var Validator = require("core/converter/converter").Validator;
32var isNumber = require('core/converter/converter').isNumber;
33var isDef = require('core/converter/converter').isDef;
34
35/**
36 Regular expression for detecting scaling units, such as K, M, G, etc. for<br>
37 converting a string representation to a numeric value.
38 Also allow 'k' to be aliased to 'K'. These could be used for SI (powers<br>
39 of 1000) or Binary (powers of 1024) conversions.<br>
40 Also allow final 'B' to be interpreted as byte-count, implicitly triggering<br>
41 binary conversion (e.g., '10.2MB').
42 @type {RegExp}
43 @memberof module:montage/core/converter#
44 @private
45 */
46var SCALED_NUMERIC_RE_ = /^([\-]?\d+\.?\d*)([K,M,G,T,P,k,m,u,n]?)[B]?$/;
47
48/**
49 Ordered list of scaling prefixes in decreasing order.
50 @memberof module:montage/converter#
51 @type {Array}
52 @private
53 */
54 // kishore - changed prefix 'G' to 'B' to represent Billion
55var NUMERIC_SCALE_PREFIXES_ = [
56 'P', 'T', 'B', 'M', 'K', '', 'm', 'u', 'n'
57];
58
59
60/**
61 Scaling factors for conversion of numeric value to string. SI conversion.
62 @memberof module:montage/converter#
63 @type {Object}
64 @private
65 */
66var NUMERIC_SCALES_SI_ = exports.NUMERIC_SCALES_SI_ = {
67 '': 1,
68 'n': 1e-9,
69 'u': 1e-6,
70 'm': 1e-3,
71 'k': 1e3,
72 'K': 1e3,
73 'M': 1e6,
74 'B': 1e9,
75 'T': 1e12,
76 'P': 1e15
77};
78
79/**
80 Scaling factors for conversion of numeric value to string. Binary conversion.
81 @memberof module:montage/converter#
82 @type {Object}
83 @private
84 */
85var NUMERIC_SCALES_BINARY_ = exports.NUMERIC_SCALES_BINARY_ = {
86 '': 1,
87 'n': Math.pow(1024, -3),
88 'u': Math.pow(1024, -2),
89 'm': 1.0 / 1024,
90 'k': 1024,
91 'K': 1024,
92 'M': Math.pow(1024, 2),
93 'G': Math.pow(1024, 3),
94 'T': Math.pow(1024, 4),
95 'P': Math.pow(1024, 5)
96};
97
98/**
99 * Converts a numeric value to string, using specified conversion scales.
100 * @memberof module:montage/converter#
101 * @param {Number} val Value to be converted.
102 * @param {Object} conversion Dictionary of scaling factors.
103 * @param {Number} opt_decimals The number of decimals to use. Default is 2.
104 * @param {String} opt_suffix Optional suffix to append.
105 * @return {String} The human readable form of the byte size.
106 * @private
107 */
108var _numericValueToString = exports._numericValueToString = function(val, conversion, opt_decimals, opt_suffix, prefixes) {
109 prefixes = prefixes || NUMERIC_SCALE_PREFIXES_;
110 var orig_val = val;
111 var symbol = '';
112 var scale = 1;
113 if (val < 0) {
114 val = -val;
115 }
116 for (var i = 0; i < prefixes.length; i++) {
117 var unit = prefixes[i];
118 scale = conversion[unit];
119 if (val >= scale || (scale <= 1 && val > 0.1 * scale)) {
120 // Treat values less than 1 differently, allowing 0.5 to be "0.5" rather
121 // than "500m"
122 symbol = unit;
123 break;
124 }
125 }
126 if (!symbol) {
127 scale = 1;
128 } else if (opt_suffix) {
129 symbol += opt_suffix;
130 }
131 var ex = Math.pow(10, isDef(opt_decimals) ? opt_decimals : 2);
132 return Math.round(orig_val / scale * ex) / ex + symbol;
133};
134
135/**
136 Converts a string to numeric value, taking into account the units.
137 @memberof module:montage/converter#
138 @param {string} stringValue String to be converted to numeric value.
139 @param {Object} conversion Dictionary of conversion scales.
140 @return {number} Numeric value for string. If it cannot be converted, returns NaN.
141 @private
142 */
143var _stringToNumericValue = function(stringValue, conversion) {
144 var match = stringValue.match(SCALED_NUMERIC_RE_);
145 if (!match) {
146 return NaN;
147 }
148 return match[1] * conversion[match[2]];
149};
150
151
152/**
153 Checks whether string value containing scaling units (K, M, G, T, P, m, u, n) can be converted to a number.<br>
154 Where there is a decimal, there must be a digit to the left of the decimal point.<br>
155 Negative numbers are valid.<br>
156 @example 0, 1, 1.0, 10.4K, 2.3M, -0.3P, 1.2m
157 @memberof module:montage/core/converter#
158 @function
159 @param {String} val String value to check.
160 @return {Boolean} true If the string could be converted to a numeric value.
161 */
162var isConvertableScaledNumber = function(val) {
163 return SCALED_NUMERIC_RE_.test(val);
164};
165
166
167/**
168 Converts a string to numeric value, taking into account the units.<br>
169 If string ends in 'B', use binary conversion.
170 @memberof module:montage/core/converter#
171 @function
172 @param {String} stringValue String to be converted to numeric value.
173 @return {Number} Numeric value for string.
174 */
175var stringToNumericValue = exports.stringToNumericValue = function(stringValue) {
176 if (stringValue.endsWith('B')) {
177 return _stringToNumericValue(
178 stringValue, NUMERIC_SCALES_BINARY_);
179 }
180 return _stringToNumericValue(
181 stringValue, NUMERIC_SCALES_SI_);
182};
183
184
185
186/**
187 Converts a numeric value to string representation. SI conversion.
188 @memberof module:montage/core/converter#
189 @function
190 @param {Number} val Value to be converted.
191 @param {Number} opt_decimals The number of decimals to use. Defaults to 2.
192 @returns {String} String representation of number.
193 */
194var numericValueToString = exports.numericValueToString = function(val, opt_decimals) {
195 return _numericValueToString(val, NUMERIC_SCALES_SI_, opt_decimals);
196};
197
198
199
200/**
201 @class module:montage/core/converter/number-converter.NumberValidator
202 @classdesc Validates that a string can be represented as a numeric value, and returns the numeric value.
203 @extends module:montage/core/converter.Validator
204 */
205var NumberValidator = exports.NumberValidator = Montage.create(Validator, /** @lends montage/core/converter/number-converter.NumberValidator# */ {
206
207 /**
208 Indicates whether floating point values are allowed.<br>
209 If <code>true</code> (the default) then the validator attempts to parse the string as a float value.<br>
210 If <code>false</code>, it attempts to parse the value as an integer.
211 @type {Property}