-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathalgorithmia.data.js
More file actions
105 lines (96 loc) · 2.82 KB
/
Copy pathalgorithmia.data.js
File metadata and controls
105 lines (96 loc) · 2.82 KB
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
(function() {
var autoType, isArray, isMap, printType, unifyArray, unifyTypes;
autoType = function(data) {
var arrayType, key, keyType, types, value, valueType;
switch (Object.prototype.toString.call(data)) {
case "[object Array]":
types = data.map(autoType);
arrayType = unifyArray(types);
return [arrayType];
case "[object Object]":
keyType = unifyArray((function() {
var _results;
_results = [];
for (key in data) {
value = data[key];
_results.push(autoType(key));
}
return _results;
})());
valueType = unifyArray((function() {
var _results;
_results = [];
for (key in data) {
value = data[key];
_results.push(autoType(value));
}
return _results;
})());
return {
key: keyType,
value: valueType
};
case "[object Boolean]":
return "Boolean";
case "[object String]":
return "String";
case "[object Number]":
return "Number";
case "[object Undefined]":
return "Unit";
case "[object Null]":
return "Unit";
}
};
unifyArray = function(array) {
return array.reduce(unifyTypes, "Unit");
};
unifyTypes = function(typeA, typeB) {
var arrayType, keyType, valueType;
if (typeA === typeB) {
return typeA;
} else if (typeA === null || typeA === "Unit") {
return typeB;
} else if (typeB === null || typeB === "Unit") {
return typeA;
} else if (typeA === "Any" || typeB === "Any") {
return "Any";
} else if (typeA === "String" && (typeB === "Number" || typeB === "Boolean")) {
return "String";
} else if (typeB === "String" && (typeA === "Number" || typeA === "Boolean")) {
return "String";
} else if (isArray(typeA) && isArray(typeB)) {
arrayType = unifyTypes(typeA[0], typeB[0]);
return [arrayType];
} else if (isMap(typeA) && isMap(typeB)) {
keyType = unifyTypes(typeA.key, typeB.key);
valueType = unifyTypes(typeA.value, typeB.value);
return {
key: keyType,
value: valueType
};
} else {
return "Any";
}
};
isArray = function(obj) {
return Object.prototype.toString.call(obj) === "[object Array]";
};
isMap = function(obj) {
return Object.prototype.toString.call(obj) === "[object Object]";
};
printType = function(type) {
if (isArray(type)) {
return "List[" + printType(type[0]) + "]";
} else if (isMap(type)) {
return "Map[" + printType(type.key) + "," + printType(type.value) + "]";
} else {
return type;
}
};
window.Types = {
autoType: autoType,
printType: printType
};
}).call(this);
//# sourceMappingURL=algorithmia.data.js.map