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

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 5 months ago

F4 Finalna Verzija

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