1 | var placeholderRE = /\{\{\s*([^{}|]+?)(?:\s*\|\s*([^{}|]+))?\s*\}\}/g;
|
2 | var capitalizeRE = /^\w/;
|
3 | var camelizeRE = /-([a-z])/ig;
|
4 |
|
5 | var filterMap = {
|
6 | 'capitalize': capitalize,
|
7 | 'xapitalize': xapitalize,
|
8 | 'camelize': camelize,
|
9 | 'toLowerCase': function(str) {
|
10 | return str.toLowerCase();
|
11 | },
|
12 | 'toUpperCase': function(str) {
|
13 | return str.toUpperCase();
|
14 | }
|
15 | };
|
16 |
|
17 | function format(template, data) {
|
18 | if (!template) {
|
19 | return '';
|
20 | }
|
21 |
|
22 | if (data == null) {
|
23 | return template;
|
24 | }
|
25 |
|
26 | return template.replace(
|
27 | placeholderRE,
|
28 | function(match, key, filter) {
|
29 | var replacer = data[key];
|
30 | if (typeof replacer === 'function') {
|
31 | replacer = replacer(key);
|
32 | }
|
33 |
|
34 | filter = filter && filterMap[filter];
|
35 | return replacer == null ? '' : typeof filter === 'function' ? filter(replacer) : replacer;
|
36 | }
|
37 | );
|
38 | }
|
39 |
|
40 | function camelize(s) {
|
41 | return s.replace(camelizeRE, function(m,char) {
|
42 | return char.toUpperCase();
|
43 | });
|
44 | }
|
45 |
|
46 |
|
47 | function capitalize(str) {
|
48 | return str.replace(capitalizeRE, function(s) {
|
49 | return s.toUpperCase();
|
50 | });
|
51 | }
|
52 |
|
53 | function xapitalize(str) {
|
54 | return str.replace(capitalizeRE, function(s) {
|
55 | return s.toLowerCase();
|
56 | });
|
57 | }
|
58 |
|
59 | module.exports = {
|
60 | format: format,
|
61 | capitalize: capitalize,
|
62 | xapitalize: xapitalize
|
63 | }; |
\ | No newline at end of file |