-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
66 lines (55 loc) · 1.3 KB
/
util.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
var EOL = '\n';
export function indent(v, indentation) {
return v
.split(EOL)
.map(function(vv) {
return indentation + vv;
})
.join(EOL);
}
export function pad(str, value, filler) {
str = String(str);
var isRight = false;
if (value < 0) {
isRight = true;
value = -value;
}
if (str.length < value) {
var padding = new Array(value - str.length + 1).join(filler);
return isRight ? str + padding : padding + str;
} else {
return str;
}
}
export function pad0(str, value) {
return pad(str, value, '0');
}
var functionNameRE = /^\s*function\s*(\S*)\s*\(/;
export function functionName(f) {
if (f.name) {
return f.name;
}
var matches = f.toString().match(functionNameRE);
if (matches === null) {
// `functionNameRE` doesn't match arrow functions.
return '';
}
var name = matches[1];
return name;
}
export function constructorName(obj) {
while (obj) {
var descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor');
if (descriptor !== undefined && typeof descriptor.value === 'function') {
var name = functionName(descriptor.value);
if (name !== '') {
return name;
}
}
obj = Object.getPrototypeOf(obj);
}
}
var INDENT = ' ';
export function addSpaces(str) {
return indent(str, INDENT);
}