-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathinvoke.ts
76 lines (65 loc) · 2.02 KB
/
invoke.ts
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
import { deprecate } from '@ember/debug';
/**
Checks to see if the `methodName` exists on the `obj`.
```javascript
let foo = { bar: function() { return 'bar'; }, baz: null };
Ember.canInvoke(foo, 'bar'); // true
Ember.canInvoke(foo, 'baz'); // false
Ember.canInvoke(foo, 'bat'); // false
```
@method canInvoke
@for Ember
@param {Object} obj The object to check for the method
@param {String} methodName The method name to check for
@return {Boolean}
@private
*/
export function canInvoke(obj: any | null | undefined, methodName: string): obj is Function {
return obj !== null && obj !== undefined && typeof obj[methodName] === 'function';
}
/**
@module @ember/utils
*/
/**
Checks to see if the `methodName` exists on the `obj`,
and if it does, invokes it with the arguments passed.
```javascript
import { tryInvoke } from '@ember/utils';
let d = new Date('03/15/2013');
tryInvoke(d, 'getTime'); // 1363320000000
tryInvoke(d, 'setFullYear', [2014]); // 1394856000000
tryInvoke(d, 'noSuchMethod', [2014]); // undefined
```
@method tryInvoke
@for @ember/utils
@static
@param {Object} obj The object to check for the method
@param {String} methodName The method name to check for
@param {Array} [args] The arguments to pass to the method
@return {*} the return value of the invoked method or undefined if it cannot be invoked
@public
@deprecated Use Javascript's optional chaining instead.
*/
export function tryInvoke(
obj: any | undefined | null,
methodName: string,
args: Array<any | undefined | null>
) {
deprecate(
`Use of tryInvoke is deprecated. Instead, consider using JavaScript's optional chaining.`,
false,
{
id: 'ember-utils.try-invoke',
until: '4.0.0',
for: 'ember-source',
since: {
enabled: '3.24.0',
},
url: 'https://deprecations.emberjs.com/v3.x#toc_ember-utils-try-invoke',
}
);
if (canInvoke(obj, methodName)) {
let method = obj[methodName];
return method.apply(obj, args);
}
}