-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-partial-is-array.js
98 lines (86 loc) · 4.09 KB
/
js-partial-is-array.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
/*
|----------------------------------------------------------------------------------------------------------------------
| A partial to check whether an object is an array.
|----------------------------------------------------------------------------------------------------------------------
*/
/**
* More information on [JavaScript Open Standards]{@link https://github.com/jsopenstd/jsopenstd}.
*
* @namespace js.partial
* @version 0.0.2
*
* @author Richard King <richrdkng@gmail.com> [GitHub]{@link https://github.com/richrdkng}
* @license [MIT]{@link https://github.com/jsopenstd/js-partial-foreach/blob/master/license.md}
*/
/**
* UMD - [returnExports.js pattern]{@link https://github.com/umdjs/umd/blob/master/templates/returnExports.js}
* For more information and license, check the link below:
* [UMD GitHub Repository]{@link https://github.com/umdjs/umd}
*/
(function(root, factory) {
// AMD
/* istanbul ignore next: ignore coverage test for UMD */
if (typeof define === 'function' && define.amd) {
define([], factory);
// CommonJS
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
// Browser
} else {
root.js_partial_isArray = factory();
}
}(this, function() {
'use strict';
/**
* Determines whether an object is an array.
* By default it handles **typed arrays and array-likes as non-arrays**.
*
* @function isArray
* @memberOf js.partial
*
* @param {*} object - The object to check.
* @param {boolean|null} [handleTypedArrayAsArray=false] - Handle a typed array as a regular array too.
* By default a typed array is not classified
* as a regular array.
* **Pass null to skip this argument** and leave its value
* with the default value.
* @param {boolean} [handleArrayLikeAsArray=false] - Handle an array-like as a regular array too.
* An array-like is anything, that has a valid .length
* property and behaves as a collection, that stores values
* such as arguments, strings, other objects based on
* arrays or objects.
* By default an array-like is not classified
* as a regular array.
*
* @returns {boolean} If the object is an array, it will return true.
*/
return function isArray(object, handleTypedArrayAsArray, handleArrayLikeAsArray) {
var handleTypedArray = handleTypedArrayAsArray === true,
handleArrayLike = handleArrayLikeAsArray === true;
if (object !== null && typeof object === 'object') {
if (Object.prototype.toString.call(object) === '[object Array]') {
return true;
}
if (handleTypedArray) {
if (object instanceof Int8Array ||
object instanceof Uint8Array ||
object instanceof Uint8ClampedArray ||
object instanceof Int16Array ||
object instanceof Uint16Array ||
object instanceof Int32Array ||
object instanceof Uint32Array ||
object instanceof Float32Array ||
object instanceof Float64Array) {
return true;
}
}
if (handleArrayLike) {
if ('length' in object &&
typeof object.length === 'number') {
return true;
}
}
}
return false;
};
}));