-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
108 lines (93 loc) · 2.17 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
(function (factory) {
if (typeof exports == 'object') {
module.exports = factory();
} else if ((typeof define == 'function') && define.amd) {
define(factory);
}
}(function () {
var isBuiltIn = (function () {
var built_ins = [
Object,
Function,
Array,
String,
Boolean,
Number,
Date,
RegExp,
Error
];
var built_ins_length = built_ins.length;
return function (_constructor) {
for (var i = 0; i < built_ins_length; i++) {
if (built_ins[i] === _constructor) {
return true;
}
}
return false;
};
})();
var stringType = (function () {
var _toString = ({}).toString;
return function (obj) {
// [object Blah] -> Blah
var stype = _toString.call(obj).slice(8, -1);
if ((obj === null) || (obj === undefined)) {
return stype.toLowerCase();
}
var ctype = of(obj);
if (ctype && !isBuiltIn(ctype)) {
return ctype.name;
} else {
return stype;
}
};
})();
function of (obj) {
if ((obj === null) || (obj === undefined)) {
return obj;
} else {
return obj.constructor;
}
}
function is (obj, test) {
var typer = (of(test) === String) ? stringType : of;
return (typer(obj) === test);
}
function instance (obj, test) {
return (obj instanceof test);
}
function extension (_Extension, _Base) {
return instance(_Extension.prototype, _Base);
}
function any (obj, tests) {
if (!is(tests, Array)) {
throw ("Second argument to .any() should be array")
}
for (var i = 0; i < tests.length; i++) {
var test = tests[i];
if (is(obj, test)) {
return true;
}
}
return false;
}
var exports = function (obj, type) {
if (arguments.length == 1) {
return of(obj);
} else {
if (is(type, Array)) {
return any(obj, type);
} else {
return is(obj, type);
}
}
}
exports.instance = instance;
exports.string = stringType;
exports.of = of;
exports.is = is;
exports.any = any;
exports.extension = extension;
return exports;
}));