-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathutil.ts
31 lines (27 loc) · 1.08 KB
/
util.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
import { gte } from 'ember-compatibility-helpers';
export type DecoratorPropertyDescriptor = PropertyDescriptor & { initializer?: any } | undefined;
export function isElementDescriptor(args: any[]): args is [object, string, DecoratorPropertyDescriptor] {
let [maybeTarget, maybeKey, maybeDesc] = args;
return (
// Ensure we have the right number of args
args.length === 3 &&
// Make sure the target is a class or object (prototype)
(typeof maybeTarget === 'function' || (typeof maybeTarget === 'object' && maybeTarget !== null)) &&
// Make sure the key is a string
typeof maybeKey === 'string' &&
// Make sure the descriptor is the right shape
((typeof maybeDesc === 'object' &&
maybeDesc !== null &&
'enumerable' in maybeDesc &&
'configurable' in maybeDesc) ||
// TS compatibility
maybeDesc === undefined)
);
}
export function computedMacroWithOptionalParams(fn) {
if (gte('3.10.0')) {
return (...maybeDesc: any[]) => (isElementDescriptor(maybeDesc) ? fn()(...maybeDesc) : fn(...maybeDesc));
} else {
return fn;
}
}