diff options
Diffstat (limited to 'node_modules/montage/core/converter/currency-converter.js')
-rwxr-xr-x | node_modules/montage/core/converter/currency-converter.js | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/node_modules/montage/core/converter/currency-converter.js b/node_modules/montage/core/converter/currency-converter.js new file mode 100755 index 00000000..bba81dc1 --- /dev/null +++ b/node_modules/montage/core/converter/currency-converter.js | |||
@@ -0,0 +1,77 @@ | |||
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/currency-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'); | ||
14 | var numericValueToString = require("core/converter/number-converter").numericValueToString; | ||
15 | var NumberConverter = require("core/converter/number-converter").NumberConverter; | ||
16 | /** | ||
17 | Formats a number as a human-readable currency value. | ||
18 | @function module:montage/core/converter/currency-converter.#formatCurrency | ||
19 | @param {Property} value | ||
20 | @param {String} currency | ||
21 | @param {Number} decimals | ||
22 | @param {String} useParensForNegative | ||
23 | @returns stringValue | ||
24 | */ | ||
25 | var formatCurrency = function(value, currency, decimals, useParensForNegative) { | ||
26 | var stringValue = numericValueToString(value, decimals); | ||
27 | currency = currency || '$'; | ||
28 | if ((value < 0) && useParensForNegative) { | ||
29 | stringValue = '(' + stringValue.substring(1, stringValue.length) + ')'; | ||
30 | } | ||
31 | |||
32 | stringValue = stringValue + ' ' + currency; | ||
33 | return stringValue; | ||
34 | }; | ||
35 | /** | ||
36 | @class module:montage/core/converter/currency-converter.CurrencyConverter | ||
37 | @classdesc Formats a value as a currency. | ||
38 | @extends module:montage/core/converter/number-converter.NumberConverter | ||
39 | */ | ||
40 | exports.CurrencyConverter = Montage.create(NumberConverter, /** @lends module:montage/core/converter.CurrencyConverter# */ { | ||
41 | |||
42 | /** | ||
43 | @type {Property} | ||
44 | @default {String} '$' | ||
45 | */ | ||
46 | currency: { | ||
47 | value: '$' | ||
48 | }, | ||
49 | |||
50 | /** | ||
51 | @type {Property} | ||
52 | @default {Number} 2 | ||
53 | */ | ||
54 | decimals: { | ||
55 | value: 2 | ||
56 | }, | ||
57 | |||
58 | /** | ||
59 | @type {Property} | ||
60 | @default {Boolean} false | ||
61 | */ | ||
62 | useParensForNegative: { | ||
63 | value: false | ||
64 | }, | ||
65 | |||
66 | /** | ||
67 | @function | ||
68 | @param {String} v | ||
69 | @returns {string} The formatted currency value. | ||
70 | */ | ||
71 | convert: { | ||
72 | value: function(v) { | ||
73 | return formatCurrency(v, this.currency, this.decimals, this.useParensForNegative); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | }); | ||