aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/core/converter/currency-converter.js
blob: bba81dc12c2d671f253f02997fba1759169ce27c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */
/**
	@module montage/core/converter/currency-converter
    @requires montage/core/core
    @requires montage/core/converter/converter
    @requires montage/core/converter/number-converter
*/
var Montage = require("montage").Montage;
var Converter = require('core/converter/converter');
var numericValueToString = require("core/converter/number-converter").numericValueToString;
var NumberConverter = require("core/converter/number-converter").NumberConverter;
/**
 Formats a number as a human-readable currency value.
 @function module:montage/core/converter/currency-converter.#formatCurrency
 @param {Property} value
 @param {String} currency
 @param {Number} decimals
 @param {String} useParensForNegative
 @returns stringValue
*/
var formatCurrency = function(value, currency, decimals, useParensForNegative) {
    var stringValue = numericValueToString(value, decimals);
    currency = currency || '$';
    if ((value < 0) && useParensForNegative) {
        stringValue = '(' + stringValue.substring(1, stringValue.length) + ')';
    }

    stringValue = stringValue + ' ' + currency;
    return stringValue;
};
/**
 @class module:montage/core/converter/currency-converter.CurrencyConverter
 @classdesc Formats a value as a currency.
 @extends module:montage/core/converter/number-converter.NumberConverter
 */
exports.CurrencyConverter = Montage.create(NumberConverter, /** @lends module:montage/core/converter.CurrencyConverter# */ {

    /**
        @type {Property}
        @default {String} '$'
    */
    currency: {
        value: '$'
    },

    /**
        @type {Property}
        @default {Number} 2
    */
    decimals: {
        value: 2
    },

    /**
        @type {Property}
        @default {Boolean} false
    */
    useParensForNegative: {
        value: false
    },

    /**
     @function
     @param {String} v
     @returns {string} The formatted currency value.
     */
    convert: {
        value: function(v) {
            return formatCurrency(v, this.currency, this.decimals, this.useParensForNegative);
        }
    }

});