-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtypes.js
More file actions
183 lines (151 loc) · 4.54 KB
/
types.js
File metadata and controls
183 lines (151 loc) · 4.54 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
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
/**
* @fileoverview A set of utilities to work with EE types.
* @suppress {missingRequire} TODO(b/152540451): this shouldn't be needed
*/
goog.provide('ee.Types');
goog.require('ee.ComputedObject');
/**
* A dictionary of the ee classes. Not technically needed in the JavaScript
* library, but it matches what we have to do in the Python library. The keys
* are the names of the ee classes. The values the class objects.
*
* @type {Object}
* @private
*/
ee.Types.registeredClasses_ = {};
/**
* Register the classes available in the ee object for lookup.
*
* @param {Object} classes The classes available in the ee object for lookup.
*/
ee.Types.registerClasses = function(classes) {
ee.Types.registeredClasses_ = classes;
};
/**
* Converts a type name to a class constructor.
*
* @param {string} name The class name.
* @return {Function|null} The constructor for the named class or null if it's
* not an ee class.
*/
ee.Types.nameToClass = function(name) {
if (name in ee.Types.registeredClasses_) {
return ee.Types.registeredClasses_[name];
}
return null;
};
/**
* Converts a class constructor to the API-friendly type name.
*
* @param {Function} klass The class constructor.
* @return {string} The name of the class, or "Object" if not recognized.
*/
ee.Types.classToName = function(klass) {
if (klass.prototype instanceof ee.ComputedObject) {
// Assume that name() does not care about the instance.
return klass.prototype.name.call(null);
}
if ([Number, String, Array, Date].includes(klass)) {
return klass.name
}
return 'Object';
};
/**
* Checks whether a type is a subtype of another.
*
* @param {string} firstType The first type name.
* @param {string} secondType The second type name.
* @return {boolean} Whether secondType is a subtype of firstType.
*/
ee.Types.isSubtype = function(firstType, secondType) {
if (secondType === firstType) {
return true;
}
switch (firstType) {
case 'Element':
return [
'Element',
'Image',
'Feature',
'Collection',
'ImageCollection',
'FeatureCollection'
].includes(secondType);
case 'Collection':
case 'FeatureCollection':
return [
'Collection',
'ImageCollection',
'FeatureCollection'
].includes(secondType);
case 'Object':
return true;
default:
return false;
}
};
/**
* Returns true if this object is a number or number variable.
*
* @param {*} obj The object to check.
* @return {boolean} Whether the object is a number or number variable.
*/
ee.Types.isNumber = function(obj) {
return typeof obj === 'number' ||
(obj instanceof ee.ComputedObject && obj.name() === 'Number');
};
/**
* Returns true if this object is a string or string variable.
*
* @param {*} obj The object to check.
* @return {boolean} Whether the object is a string or string variable.
*/
ee.Types.isString = function(obj) {
return typeof obj === 'string' ||
(obj instanceof ee.ComputedObject && obj.name() === 'String');
};
/**
* Returns true if this object is an array or array variable.
*
* @param {*} obj The object to check.
* @return {boolean} Whether the object is an array or array variable.
*/
ee.Types.isArray = function(obj) {
return Array.isArray(obj) ||
(obj instanceof ee.ComputedObject && obj.name() === 'List');
};
/**
* Returns true if this the object is a regular, non-prototyped, non-function
* Object.
*
* @param {*} obj The object to check.
* @return {boolean} Whether the object is a regular, non-prototyped,
* non-function Object.
*/
ee.Types.isRegularObject = function(obj) {
if (goog.isObject(obj) && typeof obj !== 'function') {
const proto = Object.getPrototypeOf(obj);
return proto !== null && Object.getPrototypeOf(proto) === null;
}
return false;
};
/**
* Assume keyword arguments if we get a single dictionary.
*
* @param {!Array<?>} args actual arguments to function call.
* @param {!ee.Function.Signature} signature.
* @param {boolean=} isInstance true if function is invoked on existing instance.
* @return {boolean} true if the first element of args should be treated as a
* dictionary of keyword arguments.
*/
ee.Types.useKeywordArgs = function(args, signature, isInstance = false) {
if (args.length === 1 && ee.Types.isRegularObject(args[0])) {
const formalArgs = isInstance ? formalArgs.slice(1) : signature.args;
if (formalArgs.length) {
return formalArgs.length === 1
? formalArgs[0].type !== 'Dictionary'
: formalArgs[1].optional
}
}
return false;
};