-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (129 loc) · 3.9 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
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
/**
*
* Package: which-type-object
* Author: Ganesh B
* Description:
* Install: npm i which-type-object --save
* Github: https://github.com/ganeshkbhat/which-type-object
* npmjs Link: https://www.npmjs.com/package/which-type-object
* File:
* File Description:
*
* CREDITS: Some sections of the code has been extracted from underscore JS and modified
* https://underscorejs.org/docs/modules/isDataView.html
* https://underscorejs.org/docs/modules/_tagTester.html
*
*
*/
/* eslint no-console: 0 */
'use strict';
/**
* isBrowser
*
* @return {*}
*/
function isBrowser() {
if (typeof process === "object" && typeof require === "function") {
return false;
}
if (typeof importScripts === "function") { return false; }
if (typeof window === "object") { return true; }
}
function TagTester(name) {
var tag = '[object ' + name + ']';
return function (obj) {
return toString.call(obj) === tag;
};
}
function TypeTester(name) {
return TagTester(name) || TagTester(name) === TagTester(Function("return " + name + ";")(name));
}
// Sample Usage for
var isTagArrayBuffer = TagTester('ArrayBuffer');
var isTagFunction = TagTester('Function');
var isTagDataView = TagTester('DataView');
var isTagObject = TagTester('Object');
var typedArrayPattern = /\[object ((I|Ui)nt(8|16|32)|Float(32|64)|Uint8Clamped|Big(I|Ui)nt64)Array\]/;
var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
var supportsArrayBuffer = () => typeof ArrayBuffer !== 'undefined',
ObjProto = Object.prototype,
toString = ObjProto.toString,
supportsDataView = () => typeof DataView !== 'undefined',
nativeIsArrayBufferView = supportsArrayBuffer() && ArrayBuffer.isView;
var nativeIsArray = Array.isArray,
nativeKeys = Object.keys,
nativeCreate = Object.create;
var hasEnumBug = !{ toString: null }.propertyIsEnumerable('toString');
var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];
var hasDataViewBug = (
supportsDataView() && (!/\[native code\]/.test(String(DataView)) || isTagObject(new DataView(new ArrayBuffer(8))))
)
if (!nativeIsArrayBufferView) {
// add polyfill here
}
function getShallowProperty(key) {
return function (obj) {
return obj == null ? void 0 : obj[key];
};
}
// Common internal logic for `isArrayLike` and `isBufferLike`.
function createSizePropertyCheck(getSizeProperty) {
return function (collection) {
var sizeProperty = getSizeProperty(collection);
return (
typeof sizeProperty == 'number' &&
sizeProperty >= 0 &&
sizeProperty <= MAX_ARRAY_INDEX
);
};
}
var getByteLength = getShallowProperty('byteLength');
var isBufferLike = createSizePropertyCheck(getByteLength);
function isTypedArray(obj) {
return (supportsArrayBuffer) ?
(
nativeIsArrayBufferView ? (nativeIsArrayBufferView(obj) && !isTagDataView(obj)) :
isBufferLike(obj) && typedArrayPattern.test(toString.call(obj))
) : false;
}
function alternateIsDataView(obj) {
return obj != null && isTagFunction(obj.getInt8) && isTagArrayBuffer(obj.buffer);
}
var isValidDataView = (hasDataViewBug ? alternateIsDataView : isTagDataView);
if (!isBrowser()) {
module.exports = {
TagTester,
TypeTester,
isTagArrayBuffer,
isTagFunction,
isTagDataView,
nativeIsArrayBufferView,
nativeIsArray,
nativeKeys,
nativeCreate,
hasDataViewBug,
hasEnumBug,
isValidDataView,
isTagObject,
isBufferLike,
isTypedArray
}
module.exports.default = {
TagTester,
TypeTester,
isTagArrayBuffer,
isTagFunction,
isTagDataView,
nativeIsArrayBufferView,
nativeIsArray,
nativeKeys,
nativeCreate,
hasDataViewBug,
hasEnumBug,
isValidDataView,
isTagObject,
isBufferLike,
isTypedArray
}
}