This repository has been archived by the owner on Jan 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
105 lines (84 loc) · 3.3 KB
/
index.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
var REGIDX = new RegExp('\\$idx', 'g');
var REGKEY = new RegExp('\\$key', 'g');
module.exports = function (dust) {
dust.helpers.pre = dust.helpers.message = function message(chunk, ctx, bodies, params) {
if (params.type && params.type !== 'content') {
return chunk.write('');
}
var before = params.before || '';
var after = params.after || '';
var mode = params.mode || '';
var sep = params.sep || '';
var value = ctx.get('intl.messages' + '.' + params.key) || ctx.get('messages' + '.' + params.key) || '☃' + params.key + '☃';
if (typeof value === 'string') {
value = (mode === 'json') ? JSON.stringify(value) : (before + value + after);
} else if (typeof value === 'object' && value !== null) {
// Object or Array
if (mode === 'json') {
value = JSON.stringify(value, substitute);
} else if (mode === 'paired') {
value = transform(value, asObj(value));
value = JSON.stringify(value);
} else {
value = transform(value, asString(value, before, after));
value = value.join(sep);
}
} else {
// number, bool, date, etc? not likely, but maybe
value = String(value);
}
return chunk.map(function (chunk) {
/* And thus begins the ugly, possibly expensive hack to run dynamically loaded content through Dust */
var cacheKey = ctx.templateName + params.key + encodeURI(value).replace(/%/g, '_');
var tmpl = dust.cache[cacheKey] || dust.loadSource(dust.compile(value, cacheKey));
tmpl(chunk, ctx).end();
/* Here endeth the confusion, on Setting Orange, the 56th day of Bureaucracy in the YOLD 3180 */
});
};
};
module.exports.registerWith = module.exports;
// Replace any $idx or $key values in the element
function substitute(key, value) {
if (typeof value === 'string') {
// Test for numeric value. If non-numeric, use $key, else $idx
var regex = isNaN(parseInt(key, 10)) ? REGKEY : REGIDX;
value = value.replace(regex, key);
}
return value;
}
function asString(obj, before, after) {
var regex = Array.isArray(obj) ? REGIDX : REGKEY;
return function stringPredicate(item) {
var str, b, a, objItem;
objItem = obj[item];
// If mode parameter is missing on nested object, fail soft.
if (typeof objItem !== 'string') {
return '';
}
str = objItem.replace(regex, item);
b = before.replace(regex, item);
a = after.replace(regex, item);
return b + str + a;
};
}
function asObj(obj) {
var regex = Array.isArray(obj) ? REGIDX : REGKEY;
return function objectPredicate(item) {
var id = parseInt(item, 10);
if (typeof obj[item] === 'object') {
var child = transform(obj[item], asObj(obj[item]));
return {
'$id': isNaN(id) ? item : id,
'$elt': child
};
}
return {
'$id': isNaN(id) ? item : id,
'$elt': obj[item].replace(regex, item)
};
};
}
function transform (obj, predicate) {
return Object.keys(obj).map(predicate);
}