index.js
1.51 KB
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
'use strict';
var object = require('./object');
var style = require('./style');
var template = require('./template');
module.exports = {
applyAnsiStyles: style.applyAnsiStyles,
concatFirstStringElements: template.concatFirstStringElements,
customFormatterFactory: customFormatterFactory,
maxDepthFactory: object.maxDepthFactory,
removeStyles: style.removeStyles,
toJSON: object.toJSON,
toStringFactory: object.toStringFactory,
transform: transform,
};
function customFormatterFactory(customFormat, concatFirst, scopeOptions) {
if (typeof customFormat === 'string') {
return function customStringFormatter(data, message) {
return transform(message, [
template.templateVariables,
template.templateScopeFactory(scopeOptions),
template.templateDate,
template.templateText,
concatFirst && template.concatFirstStringElements,
], [customFormat].concat(data));
};
}
if (typeof customFormat === 'function') {
return function customFunctionFormatter(data, message) {
var modifiedMessage = Object.assign({}, message, { data: data });
var texts = customFormat(modifiedMessage, data);
return [].concat(texts);
};
}
return function (data) {
return [].concat(data);
};
}
function transform(message, transformers, initialData) {
return transformers.reduce(function (data, transformer) {
if (typeof transformer === 'function') {
return transformer(data, message);
}
return data;
}, initialData || message.data);
}