source: imaps-frontend/node_modules/typed-array-byte-offset/index.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.6 KB
Line 
1'use strict';
2
3var forEach = require('for-each');
4var callBind = require('call-bind');
5
6var typedArrays = require('available-typed-arrays')();
7
8/** @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} TypedArray */
9/** @typedef {(x: TypedArray) => number} ByteOffsetGetter */
10
11/** @type {Object.<typeof typedArrays, ByteOffsetGetter>} */
12var getters = {};
13var hasProto = require('has-proto')();
14
15var gOPD = require('gopd');
16var oDP = Object.defineProperty;
17if (gOPD) {
18 /** @type {ByteOffsetGetter} */
19 var getByteOffset = function (x) {
20 return x.byteOffset;
21 };
22 forEach(typedArrays, function (typedArray) {
23 // In Safari 7, Typed Array constructors are typeof object
24 if (typeof global[typedArray] === 'function' || typeof global[typedArray] === 'object') {
25 var Proto = global[typedArray].prototype;
26 // @ts-expect-error TS can't guarantee the callback is invoked sync
27 var descriptor = gOPD(Proto, 'byteOffset');
28 if (!descriptor && hasProto) {
29 // @ts-expect-error hush, TS, every object has a dunder proto
30 var superProto = Proto.__proto__; // eslint-disable-line no-proto
31 // @ts-expect-error TS can't guarantee the callback is invoked sync
32 descriptor = gOPD(superProto, 'byteOffset');
33 }
34 // Opera 12.16 has a magic byteOffset data property on instances AND on Proto
35 if (descriptor && descriptor.get) {
36 getters[typedArray] = callBind(descriptor.get);
37 } else if (oDP) {
38 // this is likely an engine where instances have a magic byteOffset data property
39 var arr = new global[typedArray](2);
40 // @ts-expect-error TS can't guarantee the callback is invoked sync
41 descriptor = gOPD(arr, 'byteOffset');
42 if (descriptor && descriptor.configurable) {
43 oDP(arr, 'length', { value: 3 });
44 }
45 if (arr.length === 2) {
46 getters[typedArray] = getByteOffset;
47 }
48 }
49 }
50 });
51}
52
53/** @type {ByteOffsetGetter} */
54var tryTypedArrays = function tryAllTypedArrays(value) {
55 /** @type {number} */ var foundOffset;
56 forEach(getters, /** @type {(getter: ByteOffsetGetter) => void} */ function (getter) {
57 if (typeof foundOffset !== 'number') {
58 try {
59 var offset = getter(value);
60 if (typeof offset === 'number') {
61 foundOffset = offset;
62 }
63 } catch (e) {}
64 }
65 });
66 // @ts-expect-error TS can't guarantee the callback is invoked sync
67 return foundOffset;
68};
69
70var isTypedArray = require('is-typed-array');
71
72/** @type {import('.')} */
73module.exports = function typedArrayByteOffset(value) {
74 if (!isTypedArray(value)) {
75 return false;
76 }
77 return tryTypedArrays(value);
78};
Note: See TracBrowser for help on using the repository browser.