-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
133 lines (104 loc) · 5.6 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { Formatter } from './formatter';
import t from 'should-type';
import { addSpaces, pad0, functionName, constructorName } from './util';
import { typeAdaptorForEachFormat } from './format/type-adaptor-for-each';
import { formatPlainObject, formatPlainObjectKey } from './format/object';
import { formatWrapper1, formatWrapper2 } from './format/primitive-type-wrappers';
import { formatRegExp } from './format/regexp';
import { formatFunction } from './format/function';
import { formatArray } from './format/array';
import { formatArguments } from './format/arguments';
import { formatDate } from './format/date';
import { formatError } from './format/error';
import { generateFormatForNumberArray } from './format/number-array';
import { formatMap } from './format/map';
import { formatSet } from './format/set';
import { genSimdVectorFormat } from './format/simd';
function defaultFormat(value, opts) {
return new Formatter(opts).format(value);
}
defaultFormat.Formatter = Formatter;
defaultFormat.addSpaces = addSpaces;
defaultFormat.pad0 = pad0;
defaultFormat.functionName = functionName;
defaultFormat.constructorName = constructorName;
defaultFormat.formatPlainObjectKey = formatPlainObjectKey;
defaultFormat.typeAdaptorForEachFormat = typeAdaptorForEachFormat;
export default defaultFormat;
// adding primitive types
Formatter.addType(new t.Type(t.UNDEFINED), function() {
return 'undefined';
});
Formatter.addType(new t.Type(t.NULL), function() {
return 'null';
});
Formatter.addType(new t.Type(t.BOOLEAN), function(value) {
return value ? 'true': 'false';
});
Formatter.addType(new t.Type(t.SYMBOL), function(value) {
return value.toString();
});
Formatter.addType(new t.Type(t.NUMBER), function(value) {
if (value === 0 && 1 / value < 0) {
return '-0';
}
return String(value);
});
Formatter.addType(new t.Type(t.STRING), function(value) {
return '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
.replace(/'/g, "\\'")
.replace(/\\"/g, '"') + '\'';
});
Formatter.addType(new t.Type(t.FUNCTION), formatFunction);
// plain object
Formatter.addType(new t.Type(t.OBJECT), formatPlainObject);
// type wrappers
Formatter.addType(new t.Type(t.OBJECT, t.NUMBER), formatWrapper1);
Formatter.addType(new t.Type(t.OBJECT, t.BOOLEAN), formatWrapper1);
Formatter.addType(new t.Type(t.OBJECT, t.STRING), formatWrapper2);
Formatter.addType(new t.Type(t.OBJECT, t.REGEXP), formatRegExp);
Formatter.addType(new t.Type(t.OBJECT, t.ARRAY), formatArray);
Formatter.addType(new t.Type(t.OBJECT, t.ARGUMENTS), formatArguments);
Formatter.addType(new t.Type(t.OBJECT, t.DATE), formatDate);
Formatter.addType(new t.Type(t.OBJECT, t.ERROR), formatError);
Formatter.addType(new t.Type(t.OBJECT, t.SET), formatSet);
Formatter.addType(new t.Type(t.OBJECT, t.MAP), formatMap);
Formatter.addType(new t.Type(t.OBJECT, t.WEAK_MAP), formatMap);
Formatter.addType(new t.Type(t.OBJECT, t.WEAK_SET), formatSet);
Formatter.addType(new t.Type(t.OBJECT, t.BUFFER), generateFormatForNumberArray('length', 'Buffer', 2));
Formatter.addType(new t.Type(t.OBJECT, t.ARRAY_BUFFER), generateFormatForNumberArray('byteLength', 'ArrayBuffer', 2));
Formatter.addType(new t.Type(t.OBJECT, t.TYPED_ARRAY, 'int8'), generateFormatForNumberArray('length', 'Int8Array', 2));
Formatter.addType(new t.Type(t.OBJECT, t.TYPED_ARRAY, 'uint8'), generateFormatForNumberArray('length', 'Uint8Array', 2));
Formatter.addType(new t.Type(t.OBJECT, t.TYPED_ARRAY, 'uint8clamped'), generateFormatForNumberArray('length', 'Uint8ClampedArray', 2));
Formatter.addType(new t.Type(t.OBJECT, t.TYPED_ARRAY, 'int16'), generateFormatForNumberArray('length', 'Int16Array', 4));
Formatter.addType(new t.Type(t.OBJECT, t.TYPED_ARRAY, 'uint16'), generateFormatForNumberArray('length', 'Uint16Array', 4));
Formatter.addType(new t.Type(t.OBJECT, t.TYPED_ARRAY, 'int32'), generateFormatForNumberArray('length', 'Int32Array', 8));
Formatter.addType(new t.Type(t.OBJECT, t.TYPED_ARRAY, 'uint32'), generateFormatForNumberArray('length', 'Uint32Array', 8));
Formatter.addType(new t.Type(t.OBJECT, t.SIMD, 'bool16x8'), genSimdVectorFormat('Bool16x8', 8));
Formatter.addType(new t.Type(t.OBJECT, t.SIMD, 'bool32x4'), genSimdVectorFormat('Bool32x4', 4));
Formatter.addType(new t.Type(t.OBJECT, t.SIMD, 'bool8x16'), genSimdVectorFormat('Bool8x16', 16));
Formatter.addType(new t.Type(t.OBJECT, t.SIMD, 'float32x4'), genSimdVectorFormat('Float32x4', 4));
Formatter.addType(new t.Type(t.OBJECT, t.SIMD, 'int16x8'), genSimdVectorFormat('Int16x8', 8));
Formatter.addType(new t.Type(t.OBJECT, t.SIMD, 'int32x4'), genSimdVectorFormat('Int32x4', 4));
Formatter.addType(new t.Type(t.OBJECT, t.SIMD, 'int8x16'), genSimdVectorFormat('Int8x16', 16));
Formatter.addType(new t.Type(t.OBJECT, t.SIMD, 'uint16x8'), genSimdVectorFormat('Uint16x8', 8));
Formatter.addType(new t.Type(t.OBJECT, t.SIMD, 'uint32x4'), genSimdVectorFormat('Uint32x4', 4));
Formatter.addType(new t.Type(t.OBJECT, t.SIMD, 'uint8x16'), genSimdVectorFormat('Uint8x16', 16));
Formatter.addType(new t.Type(t.OBJECT, t.PROMISE), function() {
return '[Promise]';//TODO it could be nice to inspect its state and value
});
Formatter.addType(new t.Type(t.OBJECT, t.XHR), function() {
return '[XMLHttpRequest]';//TODO it could be nice to inspect its state
});
Formatter.addType(new t.Type(t.OBJECT, t.HTML_ELEMENT), function(value) {
return value.outerHTML;
});
Formatter.addType(new t.Type(t.OBJECT, t.HTML_ELEMENT, '#text'), function(value) {
return value.nodeValue;
});
Formatter.addType(new t.Type(t.OBJECT, t.HTML_ELEMENT, '#document'), function(value) {
return value.documentElement.outerHTML;
});
Formatter.addType(new t.Type(t.OBJECT, t.HOST), function() {
return '[Host]';
});