|
1 | 1 | var Globalize = require("globalize"); |
2 | 2 | var React = require("react"); |
| 3 | + |
| 4 | +var fns = { |
| 5 | + formatCurrency: ["value", "currency", "options"], |
| 6 | + formatDate: ["value", "options"], |
| 7 | + formatMessage: ["key", "variables"], |
| 8 | + formatNumber: ["value", "options"], |
| 9 | + formatRelativeTime: ["value", "unit", "options"] |
| 10 | +}; |
3 | 11 | var ReactGlobalize = {}; |
4 | 12 |
|
5 | 13 | function capitalizeFirstLetter(string) { |
6 | 14 | return string.charAt(0).toUpperCase() + string.slice(1); |
7 | 15 | } |
8 | 16 |
|
9 | | -Object.getOwnPropertyNames(Globalize).forEach(function(fn) { |
10 | | - if (fn.indexOf("format") === 0) { |
11 | | - var Fn = capitalizeFirstLetter(fn); |
12 | | - var fnString = Globalize[fn].toString(); |
13 | | - var argString = fnString.substr(fnString.indexOf("(")+1, fnString.indexOf(")")-(fnString.indexOf("(")+1)).trim(); |
14 | | - var argArray = argString.split(", "); |
15 | | - |
16 | | - ReactGlobalize[Fn] = React.createClass({ |
17 | | - displayName: Fn, |
18 | | - render: function() { |
19 | | - var formatted; |
20 | | - var componentProps = this.props; |
21 | | - var instance = Globalize; |
22 | | - var propArgs = argArray.map(function(element) { |
23 | | - return componentProps[element.replace(/(\s\/\*|\*\/)/,"").trim()]; |
24 | | - }); |
25 | | - |
26 | | - if (componentProps["locale"]) { |
27 | | - instance = Globalize(componentProps["locale"]); |
28 | | - } |
29 | | - |
30 | | - formatted = instance[fn].apply(instance, propArgs); |
31 | | - |
32 | | - return React.DOM.span(null, formatted); |
| 17 | +Object.keys(fns).forEach(function(fn) { |
| 18 | + var Fn = capitalizeFirstLetter(fn); |
| 19 | + var argArray = fns[fn]; |
| 20 | + |
| 21 | + ReactGlobalize[Fn] = React.createClass({ |
| 22 | + displayName: Fn, |
| 23 | + render: function() { |
| 24 | + var formatted; |
| 25 | + var componentProps = this.props; |
| 26 | + var instance = Globalize; |
| 27 | + var propArgs = argArray.map(function(element) { |
| 28 | + return componentProps[element]; |
| 29 | + }); |
| 30 | + |
| 31 | + if (componentProps["locale"]) { |
| 32 | + instance = Globalize(componentProps["locale"]); |
33 | 33 | } |
34 | | - }); |
35 | | - } |
| 34 | + |
| 35 | + formatted = instance[fn].apply(instance, propArgs); |
| 36 | + |
| 37 | + return React.DOM.span(null, formatted); |
| 38 | + } |
| 39 | + }); |
36 | 40 | }); |
37 | 41 |
|
38 | 42 | module.exports = ReactGlobalize; |
0 commit comments