-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
utils.js
138 lines (122 loc) · 4.84 KB
/
utils.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import EmberArray from './mixins/array';
import EmberObject from './system/object';
// ........................................
// TYPING & ARRAY MESSAGING
//
const TYPE_MAP = {
'[object Boolean]': 'boolean',
'[object Number]': 'number',
'[object String]': 'string',
'[object Function]': 'function',
'[object Array]': 'array',
'[object Date]': 'date',
'[object RegExp]': 'regexp',
'[object Object]': 'object',
'[object FileList]': 'filelist'
};
const { toString } = Object.prototype;
/**
@module @ember/array
*/
/**
Returns true if the passed object is an array or Array-like.
Objects are considered Array-like if any of the following are true:
- the object is a native Array
- the object has an objectAt property
- the object is an Object, and has a length property
Unlike `Ember.typeOf` this method returns true even if the passed object is
not formally an array but appears to be array-like (i.e. implements `Ember.Array`)
```javascript
Ember.isArray(); // false
Ember.isArray([]); // true
Ember.isArray(Ember.ArrayProxy.create({ content: [] })); // true
```
@method isArray
@static
@for @ember/array
@param {Object} obj The object to test
@return {Boolean} true if the passed object is an array or Array-like
@public
*/
export function isArray(obj) {
if (!obj || obj.setInterval) { return false; }
if (Array.isArray(obj)) { return true; }
if (EmberArray.detect(obj)) { return true; }
let type = typeOf(obj);
if ('array' === type) { return true; }
let length = obj.length;
if (typeof length === 'number' && length === length && 'object' === type) { return true; }
return false;
}
/**
@module @ember/utils
*/
/**
Returns a consistent type for the passed object.
Use this instead of the built-in `typeof` to get the type of an item.
It will return the same result across all browsers and includes a bit
more detail. Here is what will be returned:
| Return Value | Meaning |
|---------------|------------------------------------------------------|
| 'string' | String primitive or String object. |
| 'number' | Number primitive or Number object. |
| 'boolean' | Boolean primitive or Boolean object. |
| 'null' | Null value |
| 'undefined' | Undefined value |
| 'function' | A function |
| 'array' | An instance of Array |
| 'regexp' | An instance of RegExp |
| 'date' | An instance of Date |
| 'filelist' | An instance of FileList |
| 'class' | An Ember class (created using Ember.Object.extend()) |
| 'instance' | An Ember object instance |
| 'error' | An instance of the Error object |
| 'object' | A JavaScript object not inheriting from Ember.Object |
Examples:
```javascript
Ember.typeOf(); // 'undefined'
Ember.typeOf(null); // 'null'
Ember.typeOf(undefined); // 'undefined'
Ember.typeOf('michael'); // 'string'
Ember.typeOf(new String('michael')); // 'string'
Ember.typeOf(101); // 'number'
Ember.typeOf(new Number(101)); // 'number'
Ember.typeOf(true); // 'boolean'
Ember.typeOf(new Boolean(true)); // 'boolean'
Ember.typeOf(Ember.A); // 'function'
Ember.typeOf([1, 2, 90]); // 'array'
Ember.typeOf(/abc/); // 'regexp'
Ember.typeOf(new Date()); // 'date'
Ember.typeOf(event.target.files); // 'filelist'
Ember.typeOf(Ember.Object.extend()); // 'class'
Ember.typeOf(Ember.Object.create()); // 'instance'
Ember.typeOf(new Error('teamocil')); // 'error'
// 'normal' JavaScript object
Ember.typeOf({ a: 'b' }); // 'object'
```
@method typeOf
@for @ember/utils
@param {Object} item the item to check
@return {String} the type
@public
@static
*/
export function typeOf(item) {
if (item === null) { return 'null'; }
if (item === undefined) { return 'undefined'; }
let ret = TYPE_MAP[toString.call(item)] || 'object';
if (ret === 'function') {
if (EmberObject.detect(item)) {
ret = 'class';
}
} else if (ret === 'object') {
if (item instanceof Error) {
ret = 'error';
} else if (item instanceof EmberObject) {
ret = 'instance';
} else if (item instanceof Date) {
ret = 'date';
}
}
return ret;
}