source: imaps-frontend/node_modules/typed-array-byte-length/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 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 {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} TypedArray */
12/** @typedef {import('possible-typed-array-names')[number]} TypedArrayNames */
13/** @typedef {(value: TypedArray) => number} Getter */
14
15/** @type {Object.<TypedArrayNames, Getter>} */
16var getters = {};
17
18var oDP = Object.defineProperty;
19if (gOPD) {
20 /** @type {Getter} */
21 var getByteLength = function (x) {
22 return x.byteLength;
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 doesn't narrow properly inside callbacks
29 var descriptor = gOPD(Proto, 'byteLength');
30 if (!descriptor && hasProto) {
31 // @ts-expect-error hush, TS, every object has a dunder proto
32 var superProto = Proto.__proto__; // eslint-disable-line no-proto
33 // @ts-expect-error TS doesn't narrow properly inside callbacks
34 descriptor = gOPD(superProto, 'byteLength');
35 }
36 // Opera 12.16 has a magic byteLength data property on instances AND on Proto
37 if (descriptor && descriptor.get) {
38 getters[typedArray] = callBind(descriptor.get);
39 } else if (oDP) {
40 // this is likely an engine where instances have a magic byteLength data property
41 var arr = new global[typedArray](2);
42 // @ts-expect-error TS doesn't narrow properly inside callbacks
43 descriptor = gOPD(arr, 'byteLength');
44 if (descriptor && descriptor.configurable) {
45 oDP(arr, 'length', { value: 3 });
46 }
47 if (arr.length === 2) {
48 getters[typedArray] = getByteLength;
49 }
50 }
51 }
52 });
53}
54
55/** @type {Getter} */
56var tryTypedArrays = function tryAllTypedArrays(value) {
57 /** @type {number} */ var foundByteLength;
58 forEach(getters, /** @type {(getter: Getter) => void} */ function (getter) {
59 if (typeof foundByteLength !== 'number') {
60 try {
61 var byteLength = getter(value);
62 if (typeof byteLength === 'number') {
63 foundByteLength = byteLength;
64 }
65 } catch (e) {}
66 }
67 });
68 // @ts-expect-error TS can't guarantee the callback is invoked sync
69 return foundByteLength;
70};
71
72/** @type {import('.')} */
73module.exports = function typedArrayByteLength(value) {
74 if (!isTypedArray(value)) {
75 return false;
76 }
77 return tryTypedArrays(value);
78};
Note: See TracBrowser for help on using the repository browser.