-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (139 loc) · 4.06 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
154
155
156
157
158
159
160
161
162
/**
*
* Package: which-istypedarray
* Author: Ganesh B
* Description:
* Install: npm i which-istypedarray --save
* Github: https://github.com/ganeshkbhat/which-istypedarray/
* npmjs Link: https://www.npmjs.com/package/which-istypedarray/
* File: index.js
* File Description:
*
*
*/
/* 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; }
}
var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
function tagTester(name) {
var tag = '[object ' + name + ']';
return function (obj) {
return toString.call(obj) === tag;
};
}
var supportsArrayBuffer = () => typeof ArrayBuffer !== 'undefined',
ObjProto = Object.prototype,
toString = ObjProto.toString,
supportsDataView = () => typeof DataView !== 'undefined',
nativeIsArrayBufferView = supportsArrayBuffer() && ArrayBuffer.isView;
// Is a given variable an object?
function isObject(obj) {
var type = typeof obj;
return type === 'function' || (type === 'object' && !!obj);
}
// Is a given value equal to null?
function isNull(obj) {
return obj === null;
}
// Is a given variable undefined?
function isUndefined(obj) {
return obj === void 0;
}
// Is a given value a boolean?
function isBoolean(obj) {
return obj === true || obj === false || toString.call(obj) === '[object Boolean]';
}
// Is a given value a DOM element?
function isElement(obj) {
return !!(obj && obj.nodeType === 1);
}
// Sample Usage for
var isString = tagTester('String');
var isNumber = tagTester('Number');
var isDate = tagTester('Date');
var isRegExp = tagTester('RegExp');
var isError = tagTester('Error');
var isSymbol = tagTester('Symbol');
var isArrayBuffer = tagTester('ArrayBuffer');
var isFunction = tagTester('Function');
var isDataView = tagTester('DataView');
var hasObjectTag = tagTester('Object');
function alternateIsDataView(obj) {
return obj != null && isFunction$1(obj.getInt8) && isArrayBuffer(obj.buffer);
}
var hasDataViewBug = (
supportsDataView() && (!/\[native code\]/.test(String(DataView)) || hasObjectTag(new DataView(new ArrayBuffer(8))))
)
var isDataView$1 = (hasDataViewBug ? alternateIsDataView : isDataView);
var typedArrayPattern = /\[object ((I|Ui)nt(8|16|32)|Float(32|64)|Uint8Clamped|Big(I|Ui)nt64)Array\]/;
var isTypedArrayUsingPattern = (arg) => typedArrayPattern.test(toString.call(arg));
function toBufferView(bufferSource) {
return new Uint8Array(
bufferSource.buffer || bufferSource,
bufferSource.byteOffset || 0,
getByteLength(bufferSource)
);
}
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) {
// Credit : Underscore.js
// `ArrayBuffer.isView` is the most future-proof, so use it when available.
// Otherwise, fall back on the above regular expression.
return nativeIsArrayBufferView
? nativeIsArrayBufferView(obj) && !isDataView$1(obj)
: isBufferLike(obj) && typedArrayPattern.test(toString.call(obj));
}
if (!isBrowser()) {
module.exports = {
isTypedArray,
isBufferLike,
supportsArrayBuffer,
toString,
supportsDataView,
nativeIsArrayBufferView,
isTypedArrayUsingPattern,
toBufferView,
getShallowProperty,
tagTester
}
module.exports.default = {
isTypedArray,
isBufferLike,
supportsArrayBuffer,
toString,
supportsDataView,
nativeIsArrayBufferView,
isTypedArrayUsingPattern,
toBufferView,
getShallowProperty,
tagTester
}
}