-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
property_get.js
122 lines (100 loc) · 3.58 KB
/
property_get.js
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
/**
@module @ember/object
*/
import { assert } from 'ember-debug';
import { isPath } from './path_cache';
import { isDescriptor } from './meta';
const ALLOWABLE_TYPES = {
object: true,
function: true,
string: true
};
// ..........................................................
// 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
Ember.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, keyName) {
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, you passed ${keyName}`, typeof keyName === 'string');
assert(`'this' in paths is not supported`, keyName.lastIndexOf('this.', 0) !== 0);
assert('Cannot call `Ember.get` with an empty string', keyName !== '');
// we can't use `descriptorFor` here because we don't want to access the property
// more than once (e.g. side-effectful ES5 getters, etc)
let value = obj[keyName];
if (isDescriptor(value)) {
return value.get(obj, keyName);
} else if (isPath(keyName)) {
return _getPath(obj, keyName);
} else if (value === undefined && 'object' === typeof obj && !(keyName in obj) &&
typeof obj.unknownProperty === 'function') {
return obj.unknownProperty(keyName);
} else {
return value;
}
}
export function _getPath(root, path) {
let obj = root;
let parts = path.split('.');
for (let i = 0; i < parts.length; i++) {
if (!isGettable(obj)) {
return undefined;
}
obj = get(obj, parts[i]);
if (obj && obj.isDestroyed) {
return undefined;
}
}
return obj;
}
function isGettable(obj) {
return obj !== undefined && obj !== null && ALLOWABLE_TYPES[typeof obj];
}
/**
Retrieves the value of a property from an Object, or a default value in the
case that the property returns `undefined`.
```javascript
Ember.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(root, key, defaultValue) {
let value = get(root, key);
if (value === undefined) { return defaultValue; }
return value;
}
export default get;