-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis.js
297 lines (296 loc) · 9.23 KB
/
is.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*! is.js v0.5.3-1510231133
* by @bbuecherl
* License: MIT
*/
"use strict";
(function(factory) {
if (typeof module === "object") {
module.exports = factory();
} else if (typeof define === "function" && define.amd) {
define(["is"], factory);
} else {
window.is = factory();
}
}(function() {
"use strict";
var _global = this || (function() {
try {
return (typeof global !== _undef ? global : false);
} catch (e) {
return false;
}
}()) || window,
_number = "number",
_bool = "boolean",
_funct = "function",
_object = "object",
_string = "string",
_undef = "undefined",
_not = "!",
_iterator = "@@iterator",
_obj = {},
_array = [],
_regexp = new RegExp(_not),
_isRegExpObject = (typeof _regexp === _object),
_toString = (function(o) {
return _obj.toString.call(o);
}),
_arr = _toString(_array),
_regex = _toString(_regexp),
_args = _toString(arguments),
_err = _toString(new Error()),
_errorTypes = ["TypeError", "ReferenceError", "SyntaxError", "URIError", "EvalError", "RangeError"],
_removeFirst = (function(str) {
return str.substring(1);
});
String.prototype.startsWith = String.prototype.startsWith || function(str, offset) {
offset = offset || 0;
return this.substring(offset, offset + str.length) === str;
};
if (typeof _global.Symbol === _undef) {
_global.Symbol = {};
}
if (typeof Symbol.iterator === _undef) {
Symbol.iterator = _iterator;
}
if (typeof Array.prototype[Symbol.iterator] === _undef) {
Array.prototype[Symbol.iterator] = function() {
var index = 0,
array = this;
return {next: (function() {
return (index < array.length ? {
value: array[index++],
done: false
} : {
value: undefined,
done: true
});
})};
};
}
var assertionError = (function(expected, actual) {
var tmp = new Error("expected " + actual + " to be " + expected);
tmp.name = "AssertionError";
tmp.actual = actual;
tmp.expected = expected;
tmp.showDiff = true;
return tmp;
});
var is = {
Number: (function(val) {
return typeof val === _number;
}),
Integer: (function(val) {
return is.Number(val) && is.Finite(val) && Math.floor(val) === val;
}),
Float: (function(val) {
return is.Number(val) && is.Finite(val) && Math.floor(val) !== val;
}),
Negative: (function(val) {
return val < 0;
}),
Positive: (function(val) {
return val > 0;
}),
NaN: (function(val) {
return isNaN(val);
}),
Infinity: (function(val) {
return val === Infinity || val === -Infinity;
}),
Finite: (function(val) {
return !is.NaN(val) && !is.Infinity(val);
}),
Boolean: (function(val) {
return typeof val === _bool;
}),
True: (function(val) {
return is.Boolean(val) && true === val;
}),
False: (function(val) {
return is.Boolean(val) && true !== val;
}),
String: (function(val) {
return typeof val === _string;
}),
EmptyString: (function(val) {
return is.String(val) && val.length === 0;
}),
Undefined: (function(val) {
return typeof val === _undef;
}),
Defined: (function(val) {
return !is.Undefined(val);
}),
Null: (function(val) {
return val === null;
}),
Object: (function(val) {
return typeof val === _object || (_isRegExpObject ? false : is.RegExp(val));
}),
Function: (function(val) {
return typeof val === _funct && (_isRegExpObject ? true : !is.RegExp(val));
}),
Array: (function(val) {
return _toString(val) === _arr;
}),
EmptyArray: (function(val) {
return is.Array(val) && val.length === 0;
}),
RegExp: (function(val) {
return _toString(val) === _regex;
}),
RealObject: (function(val) {
return is.Object(val) && !is.Function(val) && !is.Null(val) && !is.Array(val) && !is.RegExp(val) && !is.Error(val);
}),
Arguments: (function(val) {
return _toString(val) == _args;
}),
Error: (function(val) {
return _toString(val) === _err;
}),
RealError: (function(val) {
for (var $__0 = _errorTypes[Symbol.iterator](),
$__1; !($__1 = $__0.next()).done; ) {
var e = $__1.value;
{
if (is[e](val)) {
return false;
}
}
}
return is.Error(val);
}),
type: {of: (function() {
for (var values = [],
$__4 = 0; $__4 < arguments.length; $__4++)
values[$__4] = arguments[$__4];
return {
equal: (function() {
for (var args = [],
$__5 = 0; $__5 < arguments.length; $__5++)
args[$__5] = arguments[$__5];
for (var $__2 = args[Symbol.iterator](),
$__3; !($__3 = $__2.next()).done; ) {
var test = $__3.value;
{
for (var $__0 = values[Symbol.iterator](),
$__1; !($__1 = $__0.next()).done; ) {
var val = $__1.value;
{
if (is.Function(test) && !test(val)) {
return false;
} else if (is.String(test) && (test[0] === _not ? is[test.substring(1, test.length)](val) : !is[test](val))) {
return false;
} else if (!is.Function(test) && !is.String(test)) {
return false;
}
}
}
}
}
return true;
}),
either: (function() {
for (var args = [],
$__6 = 0; $__6 < arguments.length; $__6++)
args[$__6] = arguments[$__6];
for (var $__2 = args[Symbol.iterator](),
$__3; !($__3 = $__2.next()).done; ) {
var test = $__3.value;
{
for (var $__0 = values[Symbol.iterator](),
$__1; !($__1 = $__0.next()).done; ) {
var val = $__1.value;
{
if (is.Function(test) && test(val)) {
return true;
} else if (is.String(test) && (test[0] === _not ? !is[test.substring(1, test.length)](val) : is[test](val))) {
return true;
}
}
}
}
}
return false;
})
};
})},
expect: (function() {
var toArray = (function(args) {
return _array.slice.call(args, 0);
}),
listExpected = (function(args, lastSeperator) {
var str = "";
for (var ownFunct = 0,
sep = "",
i = 0,
arg; i < args.length; ++i) {
arg = args[i];
if (i !== 0 && i === args.length - 1) {
str += " " + lastSeperator + " ";
} else if (i !== 0) {
str += ", ";
}
if (is.Function(arg)) {
str += is.Defined(arg.name) ? arg.name : ("custom$" + ownFunct++);
} else if (is.String(arg) && is.hasOwnProperty(arg) || is.String(arg) && is.hasOwnProperty(_removeFirst(arg))) {
str += arg;
} else {
str += "<unknown test statement '" + arg + "'>";
}
}
return str;
});
return {type: {of: (function() {
for (var values = [],
$__5 = 0; $__5 < arguments.length; $__5++)
values[$__5] = arguments[$__5];
var ref = is.type.of.apply(_array, values);
return {to: {be: {
equal: (function() {
for (var args = [],
$__6 = 0; $__6 < arguments.length; $__6++)
args[$__6] = arguments[$__6];
if (!ref.equal.apply(_array, args)) {
var exp = listExpected(args, "and"),
arr = toArray(values);
throw assertionError(exp, arr);
}
return true;
}),
either: (function() {
for (var args = [],
$__7 = 0; $__7 < arguments.length; $__7++)
args[$__7] = arguments[$__7];
if (!ref.either.apply(_array, args)) {
var exp = listExpected(args, "or"),
arr = toArray(values);
throw assertionError(exp, arr);
}
return true;
})
}}};
})}};
}()),
extend: (function(name, fn) {
if (is.String(name) && is.Function(fn) && !is.hasOwnProperty(name)) {
is[name] = fn;
}
})
};
var errorTest = (function(err) {
return (function(val) {
return val.toString().startsWith(err);
});
});
for (var $__0 = _errorTypes[Symbol.iterator](),
$__1; !($__1 = $__0.next()).done; ) {
var e = $__1.value;
{
is[e] = errorTest(e);
}
}
return is;
}));
//# sourceMappingURL=is.js.map