rjformat.js
1.74 KB
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
var RJFormat = (function() {
/*********************
* PRIVATE
*********************/
/** The string to use if the raw value is not valid. */
var invalidValueStandin = "--";
/** Returns true if the given value is not null or undefined. */
var isValue = function(val) {
return val !== undefined && val !== null;
};
/** Generates a function that calls a formatter function if the given value is valid, otherwise returns the invalidValueStandin. */
var validOr = function(f) {
return function(val) {
return isValue(val) ? f(val) : invalidValueStandin;
};
};
/*********************
* PUBLIC
*********************/
/** Mark thousands with commas. 4000000 -> 4,000,000. */
var commatize = function(val) {
var valString = val.toString();
var output = [];
for(var i=valString.length-1; i>=0; i--) {
var fromRight = valString.length - i - 1;
if(fromRight !== 0 && fromRight%3 === 0) {
output.unshift(",");
}
output.unshift(valString[i]);
}
return output.join("");
};
/** Formats the given value as a percentage. Returns the invalidValueStandin if the given raw value is null or undefined. */
var percentage = validOr(function(val) {
return val * 100 + "%";
});
var currency = validOr(function(val) {
var valString = val.toString();
var decimelLoc = valString.indexOf(".");
var decimelPlaces = decimelLoc === -1 ? 0 :
valString.length - valString.indexOf(".") - 1;
var mixed = valString.split(".");
var whole = mixed[0];
var fraction = mixed.length > 1 ? mixed[1].substring(0,2) : "";
return "${0}.{1}".supplant(
commatize(whole),
fraction + "0".repeat(2 - fraction.length)
);
});
// public interface
return {
commatize: commatize,
currency: currency,
percentage: percentage
};
})();