~ubuntuone-pqm-team/yui/stable-min

« back to all changes in this revision

Viewing changes to build/datatype-number-format/datatype-number-format-debug.js

  • Committer: Ricardo Kirkner
  • Date: 2014-09-23 20:17:06 UTC
  • Revision ID: ricardo.kirkner@canonical.com-20140923201706-17kwxwckw6orp28k
re-added all .js files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
YUI.add('datatype-number-format', function (Y, NAME) {
 
2
 
 
3
/**
 
4
 * The Number Utility provides type-conversion and string-formatting
 
5
 * convenience methods for Numbers.
 
6
 *
 
7
 * @module datatype-number
 
8
 * @main datatype-number
 
9
 */
 
10
 
 
11
/**
 
12
 * Format number submodule.
 
13
 *
 
14
 * @module datatype-number
 
15
 * @submodule datatype-number-format
 
16
 */
 
17
 
 
18
/**
 
19
 * Number provides a set of utility functions to operate against Number objects.
 
20
 *
 
21
 * @class Number
 
22
 * @static
 
23
 */
 
24
var LANG = Y.Lang;
 
25
 
 
26
Y.mix(Y.namespace("Number"), {
 
27
     /**
 
28
     * Takes a Number and formats to string for display to user.
 
29
     *
 
30
     * @method format
 
31
     * @param data {Number} Number.
 
32
     * @param config {Object} (Optional) Optional configuration values:
 
33
     *  <dl>
 
34
     *   <dt>prefix {String}</dd>
 
35
     *   <dd>String prepended before each number, like a currency designator "$"</dd>
 
36
     *   <dt>decimalPlaces {Number}</dd>
 
37
     *   <dd>Number of decimal places to round. Must be a number 0 to 20.</dd>
 
38
     *   <dt>decimalSeparator {String}</dd>
 
39
     *   <dd>Decimal separator</dd>
 
40
     *   <dt>thousandsSeparator {String}</dd>
 
41
     *   <dd>Thousands separator</dd>
 
42
     *   <dt>suffix {String}</dd>
 
43
     *   <dd>String appended after each number, like " items" (note the space)</dd>
 
44
     *  </dl>
 
45
     * @return {String} Formatted number for display. Note, the following values
 
46
     * return as "": null, undefined, NaN, "".
 
47
     */
 
48
    format: function(data, config) {
 
49
        if(LANG.isNumber(data)) {
 
50
            config = config || {};
 
51
 
 
52
            var isNeg = (data < 0),
 
53
                output = data + "",
 
54
                decPlaces = config.decimalPlaces,
 
55
                decSep = config.decimalSeparator || ".",
 
56
                thouSep = config.thousandsSeparator,
 
57
                decIndex,
 
58
                newOutput, count, i;
 
59
 
 
60
            // Decimal precision
 
61
            if(LANG.isNumber(decPlaces) && (decPlaces >= 0) && (decPlaces <= 20)) {
 
62
                // Round to the correct decimal place
 
63
                output = data.toFixed(decPlaces);
 
64
            }
 
65
 
 
66
            // Decimal separator
 
67
            if(decSep !== "."){
 
68
                output = output.replace(".", decSep);
 
69
            }
 
70
 
 
71
            // Add the thousands separator
 
72
            if(thouSep) {
 
73
                // Find the dot or where it would be
 
74
                decIndex = output.lastIndexOf(decSep);
 
75
                decIndex = (decIndex > -1) ? decIndex : output.length;
 
76
                // Start with the dot and everything to the right
 
77
                newOutput = output.substring(decIndex);
 
78
                // Working left, every third time add a separator, every time add a digit
 
79
                for (count = 0, i=decIndex; i>0; i--) {
 
80
                    if ((count%3 === 0) && (i !== decIndex) && (!isNeg || (i > 1))) {
 
81
                        newOutput = thouSep + newOutput;
 
82
                    }
 
83
                    newOutput = output.charAt(i-1) + newOutput;
 
84
                    count++;
 
85
                }
 
86
                output = newOutput;
 
87
            }
 
88
 
 
89
            // Prepend prefix
 
90
            output = (config.prefix) ? config.prefix + output : output;
 
91
 
 
92
            // Append suffix
 
93
            output = (config.suffix) ? output + config.suffix : output;
 
94
 
 
95
            return output;
 
96
        }
 
97
        // Not a Number, just return as string
 
98
        else {
 
99
            Y.log("Could not format data from type Number", "warn", "number");
 
100
            return (LANG.isValue(data) && data.toString) ? data.toString() : "";
 
101
        }
 
102
    }
 
103
});
 
104
 
 
105
Y.namespace("DataType");
 
106
Y.DataType.Number = Y.Number;
 
107
 
 
108
 
 
109
}, '@VERSION@');