Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/reflect/reflect.js
4136 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Useful compiler idioms.
9
*/
10
11
goog.provide('goog.reflect');
12
13
14
/**
15
* Syntax for object literal casts.
16
* @see http://go/jscompiler-renaming
17
* @see https://goo.gl/CRs09P
18
*
19
* Use this if you have an object literal whose keys need to have the same names
20
* as the properties of some class even after they are renamed by the compiler.
21
*
22
* @param {!Function} type Type to cast to.
23
* @param {Object} object Object literal to cast.
24
* @return {Object} The object literal.
25
*/
26
goog.reflect.object = function(type, object) {
27
'use strict';
28
return object;
29
};
30
31
/**
32
* Syntax for renaming property strings.
33
* @see http://go/jscompiler-renaming
34
* @see https://goo.gl/CRs09P
35
*
36
* Use this if you have an need to access a property as a string, but want
37
* to also have the property renamed by the compiler. In contrast to
38
* goog.reflect.object, this method takes an instance of an object.
39
*
40
* Properties must be simple names (not qualified names).
41
*
42
* @param {string} prop Name of the property
43
* @param {!Object} object Instance of the object whose type will be used
44
* for renaming
45
* @return {string} The renamed property.
46
*/
47
goog.reflect.objectProperty = function(prop, object) {
48
'use strict';
49
return prop;
50
};
51
52
/**
53
* To assert to the compiler that an operation is needed when it would
54
* otherwise be stripped. For example:
55
* <code>
56
* // Force a layout
57
* goog.reflect.sinkValue(dialog.offsetHeight);
58
* </code>
59
* @param {T} x
60
* @return {T}
61
* @template T
62
*/
63
goog.reflect.sinkValue = function(x) {
64
'use strict';
65
goog.reflect.sinkValue[' '](x);
66
return x;
67
};
68
69
70
/**
71
* The compiler should optimize this function away iff no one ever uses
72
* goog.reflect.sinkValue.
73
*/
74
goog.reflect.sinkValue[' '] = function() {};
75
76
77
/**
78
* Check if a property can be accessed without throwing an exception.
79
* @param {Object} obj The owner of the property.
80
* @param {string} prop The property name.
81
* @return {boolean} Whether the property is accessible. Will also return true
82
* if obj is null.
83
*/
84
goog.reflect.canAccessProperty = function(obj, prop) {
85
'use strict';
86
try {
87
goog.reflect.sinkValue(obj[prop]);
88
return true;
89
} catch (e) {
90
}
91
return false;
92
};
93
94
95
/**
96
* Retrieves a value from a cache given a key. The compiler provides special
97
* consideration for this call such that it is generally considered side-effect
98
* free. However, if the `opt_keyFn` or `valueFn` have side-effects
99
* then the entire call is considered to have side-effects.
100
*
101
* Conventionally storing the value on the cache would be considered a
102
* side-effect and preclude unused calls from being pruned, ie. even if
103
* the value was never used, it would still always be stored in the cache.
104
*
105
* Providing a side-effect free `valueFn` and `opt_keyFn`
106
* allows unused calls to `goog.reflect.cache` to be pruned.
107
*
108
* @param {!Object<K, V>} cacheObj The object that contains the cached values.
109
* @param {?} key The key to lookup in the cache. If it is not string or number
110
* then a `opt_keyFn` should be provided. The key is also used as the
111
* parameter to the `valueFn`.
112
* @param {function(?):V} valueFn The value provider to use to calculate the
113
* value to store in the cache. This function should be side-effect free
114
* to take advantage of the optimization.
115
* @param {function(?):K=} opt_keyFn The key provider to determine the cache
116
* map key. This should be used if the given key is not a string or number.
117
* If not provided then the given key is used. This function should be
118
* side-effect free to take advantage of the optimization.
119
* @return {V} The cached or calculated value.
120
* @template K
121
* @template V
122
*/
123
goog.reflect.cache = function(cacheObj, key, valueFn, opt_keyFn) {
124
'use strict';
125
const storedKey = opt_keyFn ? opt_keyFn(key) : key;
126
127
if (Object.prototype.hasOwnProperty.call(cacheObj, storedKey)) {
128
return cacheObj[storedKey];
129
}
130
131
return (cacheObj[storedKey] = valueFn(key));
132
};
133
134