-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
37 lines (34 loc) · 1.02 KB
/
index.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
/**
* Gets the most specific common type of all the values in an array.
* @param {Array} array - The array to check. There is no error checking;
* this must be an array.
* @returns {String|undefined} A primitive typename, "object" (e.g., if an
* array has a string and a, number), or "array"
* (if all elements are themselves arrays).
* Returns undefined is array is empty.
*/
module.exports = function arrayTypeOfValues(array) {
if (array.length === 0)
{
return undefined;
}
var types = array.map(
function(value)
{
if (typeof value === 'undefined')
{
return 'undefined';
}
if (value.constructor === Array)
{
return 'array';
}
if (typeof value === 'object')
{
return value === null ? 'null' : 'object';
}
return typeof value;
}
);
return types.every(function(t) { return t === types[0]; }) ? types[0] : 'object';
}