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

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

F4 Finalna Verzija

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