-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
61 lines (51 loc) · 1.83 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
const R = require('ramda')
const chalk = require('chalk')
const utils = require('./lib/utils')
const str2color = require('./lib/str2color')
const getFnArgs = require('./lib/get-fn-args')
const getFnName = require('./lib/get-fn-name')
const stripAnsi = require('strip-ansi')
const inspect = utils.inspect
const print = utils.print
const mapTail = (fn, arr) => R.slice(0, 1, arr).concat(R.map(fn, R.tail(arr)))
const strRepeat = R.compose(R.join(''), R.repeat)
const lines = R.split('\n')
const unlines = R.join('\n')
const indentTailLines = (n, str) =>
unlines(mapTail(R.concat(strRepeat(' ', n)), lines(str)))
const treis = (__print, color) => {
const fnNameGetter = getFnName()
const print = R.pipe(color ? R.identity : stripAnsi, __print)
return (name, fn) => {
if (typeof name === 'function') {
fn = name
name = null
}
return function(/* args */) {
if (name == null) name = fnNameGetter(fn)
name = name.toString()
const prefix = name ? str2color(name) + ' ' : ''
print(prefix + formatArgs(name, fn, arguments))
const res = fn.apply(this, arguments)
print(prefix + '=> ' + indentTailLines(name.length + 4, inspect(res)))
return res
}
}
}
module.exports = treis(print, true)
module.exports.__init = treis
function formatArgs(name, fn, args) {
const fnArgNames = getFnArgs(fn)
const getArgPairs = (i, val) => [fnArgNames[i] || i.toString(), val]
const pairs = R.zipWith(getArgPairs, R.range(0, args.length), args)
const argLines = R.map(R.apply(formatArg), pairs)
const space = strRepeat(' ', name.length + 1)
return unlines(mapTail(R.concat(space), argLines))
function formatArg(argName, val) {
const indentLevel = name.length + argName.length + 3
return R.join(': ', [
chalk.green(argName),
indentTailLines(indentLevel, inspect(val))
])
}
}