This repository has been archived by the owner on Jul 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
90 lines (72 loc) · 2.04 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
(function(f) {
'use strict';
/*istanbul ignore next*/
if(typeof module === 'object' && typeof module.exports === 'object') {
module.exports = f();
}else{
self.inspectf = f();
}
}(function() {
'use strict';
function checkn(n) {
if(typeof n !== 'number') {
throw new TypeError(
'inspectf expects its first argument to be a number'
);
}
}
function checkf(f) {
if(typeof f !== 'function') {
throw new TypeError(
'inspectf expects its second argument to be a function'
);
}
}
var RSPACE = /^ */;
var RCODE = /\s*[^\s]/;
var RTABS = /\t/g;
var REOL = /\n\r?/;
function isCode(line) {
return RCODE.test(line);
}
function getPadding(line) {
return line.match(RSPACE)[0].length;
}
function guessIndentation(lines) {
var filtered = lines.filter(isCode);
var paddings = filtered.map(getPadding);
var depth = paddings.reduce(Math.min, Infinity);
var tabsize = paddings
.map(function(x) { return x - depth; })
.find(function(x) { return x > 1; }) || 2;
return {depth: depth, tabsize: tabsize};
}
function pad(n) {
return (new Array(n + 1)).join(' ');
}
function show(f, indentation) {
return f.toString().replace(RTABS, indentation);
}
function toLines(s) {
return s.split(REOL);
}
function fixIndentation(lines, indentation) {
var info = guessIndentation(lines.slice(1));
var RPAD = new RegExp(pad(info.tabsize), 'g');
return lines.map(function(line) {
return line.slice(Math.min(info.depth, getPadding(line)))
.replace(RPAD, '\t').replace(RTABS, indentation);
}).join('\n');
}
return function inspectf(n, f) {
checkn(n);
if(arguments.length < 2) {
return function inspectf$partial(f) { return inspectf(n, f); };
}
checkf(f);
if(f.toString !== Function.prototype.toString) { return f.toString(); }
var i = pad(n), shown = show(f, i), lines = toLines(shown, i);
if(lines.length < 2) { return shown; }
return fixIndentation(lines, i);
};
}));