-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
89 lines (75 loc) · 2.18 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
'use strict';
var isObject = require('isobject');
var AsyncHelpers = require('async-helpers');
var asyncHelpers;
/**
* Compile a helper function to add convenience methods for
* working with [async-helpers][].
*
* ```js
* // ensures that asyncHelpers.resolveIds is called on the
* // rendered content from `options.fn` before returning
* var helper = compile(function(options, cb) {
* return options.fn(this, cb);
* });
* ```
* @param {Function} `helper` Helper function to be called with a modified `options.fn` and `options.inverse` function if available.
* @return {Function} Compiled helper function suitable to be registered with a template engine.
* @api public
*/
module.exports = function compile(helper) {
if (typeof helper !== 'function') {
throw new TypeError('expected a function');
}
if (!asyncHelpers) {
asyncHelpers = new AsyncHelpers();
}
function compiled(/* args */) {
var args = [].slice.call(arguments);
var cb, options;
if (typeof args[args.length - 1] === 'function') {
cb = args.pop();
}
options = args[args.length - 1];
if (!isObject(options) || !isObject(options.hash)) {
options = {};
}
if (typeof cb === 'function') {
args.push(cb);
}
if (typeof options.fn === 'function') {
var fn = options.fn;
options.fn = function(context, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
var str = fn(context, options);
if (typeof cb === 'function') {
return asyncHelpers.resolveIds(str, cb);
}
return str;
};
}
if (typeof options.inverse === 'function') {
var inverse = options.inverse;
options.inverse = function(context, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
var str = inverse(context, options);
if (typeof cb === 'function') {
asyncHelpers.resolveIds(str, cb);
return;
}
return str;
};
}
return helper.apply(this, args);
}
if (helper.async === true) {
compiled.async = true;
}
return compiled;
};