forked from millermedeiros/esformatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugins.js
80 lines (59 loc) · 1.5 KB
/
plugins.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
"use strict";
var partial = require('mout/function/partial');
var remove = require('mout/array/remove');
var _plugins = [];
exports.register = register;
function register(plugin) {
if (_plugins.indexOf(plugin) === -1) {
_plugins.push(plugin);
}
}
exports.unregister = partial(remove, _plugins);
exports.unregisterAll = unregisterAll;
function unregisterAll() {
_plugins = [];
}
exports.setOptions = function(opts) {
loadAndRegister(opts && opts.plugins);
exec('setOptions', opts);
};
exports.loadAndRegister = loadAndRegister;
function loadAndRegister(ids) {
ids = ids || [];
ids.forEach(function(id) {
register(require(id));
});
}
exportMethods([
'tokenBefore',
'tokenAfter',
'nodeBefore',
'nodeAfter',
// "transform" is an alias to "transformAfter" but we do not recommend using
// it going forward. it might be deprecated in the future.
'transform',
'transformAfter',
'transformBefore'
], exec);
exportMethods([
'stringBefore',
'stringAfter'
], pipe);
function exportMethods(arr, fn) {
arr.forEach(function(methodName) {
exports[methodName] = partial(fn, methodName);
});
}
function exec(methodName) {
var args = Array.prototype.slice.call(arguments, 1);
_plugins.forEach(function(plugin){
if (methodName in plugin) {
plugin[methodName].apply(plugin, args);
}
});
}
function pipe(methodName, input) {
return _plugins.reduce(function(output, plugin) {
return methodName in plugin ? plugin[methodName](output) : output;
}, input);
}