-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
property_get.ts
184 lines (155 loc) · 5.11 KB
/
property_get.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
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
184
/**
@module @ember/object
*/
import { descriptorFor } from '@ember/-internals/meta';
import { HAS_NATIVE_PROXY, symbol } from '@ember/-internals/utils';
import { EMBER_METAL_TRACKED_PROPERTIES } from '@ember/canary-features';
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import { isPath } from './path_cache';
import { tagForProperty } from './tags';
import { getCurrentTracker } from './tracked';
export const PROXY_CONTENT = symbol('PROXY_CONTENT');
export let getPossibleMandatoryProxyValue: (obj: object, keyName: string) => any;
if (DEBUG && HAS_NATIVE_PROXY) {
getPossibleMandatoryProxyValue = function getPossibleMandatoryProxyValue(obj, keyName) {
let content = obj[PROXY_CONTENT];
if (content === undefined) {
return obj[keyName];
} else {
/* global Reflect */
return Reflect.get(content, keyName, obj);
}
};
}
interface MaybeHasUnknownProperty {
unknownProperty?: (keyName: string) => any;
}
interface MaybeHasIsDestroyed {
isDestroyed?: boolean;
}
// ..........................................................
// GET AND SET
//
// If we are on a platform that supports accessors we can use those.
// Otherwise simulate accessors by looking up the property directly on the
// object.
/**
Gets the value of a property on an object. If the property is computed,
the function will be invoked. If the property is not defined but the
object implements the `unknownProperty` method then that will be invoked.
```javascript
import { get } from '@ember/object';
get(obj, "name");
```
If you plan to run on IE8 and older browsers then you should use this
method anytime you want to retrieve a property on an object that you don't
know for sure is private. (Properties beginning with an underscore '_'
are considered private.)
On all newer browsers, you only need to use this method to retrieve
properties if the property might not be defined on the object and you want
to respect the `unknownProperty` handler. Otherwise you can ignore this
method.
Note that if the object itself is `undefined`, this method will throw
an error.
@method get
@for @ember/object
@static
@param {Object} obj The object to retrieve from.
@param {String} keyName The property key to retrieve
@return {Object} the property value or `null`.
@public
*/
export function get(obj: object, keyName: string): any {
assert(
`Get must be called with two arguments; an object and a property key`,
arguments.length === 2
);
assert(
`Cannot call get with '${keyName}' on an undefined object.`,
obj !== undefined && obj !== null
);
assert(
`The key provided to get must be a string or number, you passed ${keyName}`,
typeof keyName === 'string' || (typeof keyName === 'number' && !isNaN(keyName))
);
assert(
`'this' in paths is not supported`,
typeof keyName !== 'string' || keyName.lastIndexOf('this.', 0) !== 0
);
let type = typeof obj;
let isObject = type === 'object';
let isFunction = type === 'function';
let isObjectLike = isObject || isFunction;
if (isPath(keyName)) {
return isObjectLike ? _getPath(obj, keyName) : undefined;
}
let descriptor;
let value: any;
if (isObjectLike) {
if (EMBER_METAL_TRACKED_PROPERTIES) {
let tracker = getCurrentTracker();
if (tracker) tracker.add(tagForProperty(obj, keyName));
}
descriptor = descriptorFor(obj, keyName);
if (descriptor !== undefined) {
return descriptor.get(obj, keyName);
}
if (DEBUG && HAS_NATIVE_PROXY) {
value = getPossibleMandatoryProxyValue(obj, keyName);
} else {
value = obj[keyName];
}
} else {
value = obj[keyName];
}
if (value === undefined) {
if (
isObject &&
!(keyName in obj) &&
typeof (obj as MaybeHasUnknownProperty).unknownProperty === 'function'
) {
return (obj as MaybeHasUnknownProperty).unknownProperty!(keyName);
}
}
return value;
}
export function _getPath<T extends object>(root: T, path: string | string[]): any {
let obj: any = root;
let parts = typeof path === 'string' ? path.split('.') : path;
for (let i = 0; i < parts.length; i++) {
if (obj === undefined || obj === null || (obj as MaybeHasIsDestroyed).isDestroyed) {
return undefined;
}
obj = get(obj, parts[i]);
}
return obj;
}
/**
Retrieves the value of a property from an Object, or a default value in the
case that the property returns `undefined`.
```javascript
import { getWithDefault } from '@ember/object';
getWithDefault(person, 'lastName', 'Doe');
```
@method getWithDefault
@for @ember/object
@static
@param {Object} obj The object to retrieve from.
@param {String} keyName The name of the property to retrieve
@param {Object} defaultValue The value to return if the property value is undefined
@return {Object} The property value or the defaultValue.
@public
*/
export function getWithDefault<T extends object, K extends Extract<keyof T, string>>(
root: T,
key: K,
defaultValue: T[K]
): T[K] {
let value = get(root, key);
if (value === undefined) {
return defaultValue;
}
return value;
}
export default get;