-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.js
89 lines (74 loc) · 3.61 KB
/
example.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
var Type = require('./');
// Type.of(arg) and Type(one_argument) return constructor of type
console.log(Type.of('hi there ok')); // [Function: String]
console.log(Type.of(342)); // [Function: Number]
console.log(Type.of({})); // [Function: Object]
console.log(Type.of([1, 2, 3])); // [Function: Array]
console.log(Type.of(null)); // null
console.log(Type.of(undefined)); // undefined
console.log(Type(true)); // [Function: Boolean]
console.log(Type(function () {})); // [Function: Function]
console.log(Type(/abcd/)); // [Function: RegExp]
console.log(Type(new Date())); // [Function: Date]
console.log(Type(new Error())); // { [Function: Error] stackTraceLimit: 10 }
// Type.string(arg) returns the string name of constructor
console.log(Type.string('hi there ok')); // "String"
console.log(Type.string(342)); // "Number"
console.log(Type.string({})); // "Object"
console.log(Type.string([1, 2, 3])); // "Array"
console.log(Type.string(null)); // "null"
console.log(Type.string(undefined)); // "undefined"
console.log(Type.string(true)); // "Boolean"
console.log(Type.string(function () {})); // "Function"
console.log(Type.string(/abcd/)); // "RegExp"
console.log(Type.string(new Date())); // "Date"
console.log(Type.string(new Error())); // "Error"
// Type.is(object, type) and Type(object, type) tests object type
console.log(Type.is(true, Boolean)); // true
console.log(Type.is("1231", Number)); // false
console.log(Type.is("1231", String)); // true
console.log(Type.is("1231", "String")); // true
console.log(Type.is("1231", Object)); // false
console.log(Type([], Object)); // false
console.log(Type({}, Object)); // true
console.log(Type([], Array)); // true
console.log(Type(new Date(), Date)); // true
console.log(Type(new Date(), Object)); // false
var s = "hihihi";
var Stringy = Type.of(s);
var t = new Stringy("hihihi");
console.log((s == t)); // true
console.log((s === t)); // false
// User defined objects should be instances of Objects but also can get actual constructor type
function Person (name) {
this.name = name;
}
Person.prototype.barf = function () {
return this.name + " just barfed!";
};
var ralph = new Person('Ralph');
console.log(Type.of(ralph)); // [Function: Person]
console.log(Type.is(ralph, Person)); // true
console.log(Type.is(ralph, Object)); // false
console.log(Type.instance(ralph, Person)); // true
console.log(Type.instance(ralph, Object)); // true
// arguments is weird edge case, there's no Arguments global but typeof arguments is "arguments"
// type returned is Object, but not sure what would be preferable
(function () {
console.log(Type.of(arguments)); // [Function: Object]
})();
// other built-ins
console.log(Type.of(Infinity)); // [Function: Number]
console.log(Type.of(-Infinity)); // [Function: Number]
console.log(Type.of(NaN)); // [Function: Number]
console.log(Type.of(Math)); // [Function: Object]
console.log(Type.of(JSON)); // [Function: Object]
// Returning constructor as type allows it to be used to create new objects i.e.
var s = "s";
var t = new Type.of(s)("t");
console.log(t.toUpperCase()); // "T"
// Type.any(obj, [Array, Of, Types]) and Type(obj, [Array, Of, Types]) should test whether
// the object is any of the passed in types
var str = 'hihihi';
console.log(Type.any(str, [String, Number, Array])); // true
console.log(Type(str, [Array, RegExp])); // false