index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. var forEach = require('for-each');
  3. var availableTypedArrays = require('available-typed-arrays');
  4. var callBind = require('call-bind');
  5. var callBound = require('call-bound');
  6. var gOPD = require('gopd');
  7. var getProto = require('get-proto');
  8. var $toString = callBound('Object.prototype.toString');
  9. var hasToStringTag = require('has-tostringtag/shams')();
  10. var g = typeof globalThis === 'undefined' ? global : globalThis;
  11. var typedArrays = availableTypedArrays();
  12. var $slice = callBound('String.prototype.slice');
  13. /** @type {<T = unknown>(array: readonly T[], value: unknown) => number} */
  14. var $indexOf = callBound('Array.prototype.indexOf', true) || function indexOf(array, value) {
  15. for (var i = 0; i < array.length; i += 1) {
  16. if (array[i] === value) {
  17. return i;
  18. }
  19. }
  20. return -1;
  21. };
  22. /** @typedef {import('./types').Getter} Getter */
  23. /** @type {import('./types').Cache} */
  24. var cache = { __proto__: null };
  25. if (hasToStringTag && gOPD && getProto) {
  26. forEach(typedArrays, function (typedArray) {
  27. var arr = new g[typedArray]();
  28. if (Symbol.toStringTag in arr && getProto) {
  29. var proto = getProto(arr);
  30. // @ts-expect-error TS won't narrow inside a closure
  31. var descriptor = gOPD(proto, Symbol.toStringTag);
  32. if (!descriptor && proto) {
  33. var superProto = getProto(proto);
  34. // @ts-expect-error TS won't narrow inside a closure
  35. descriptor = gOPD(superProto, Symbol.toStringTag);
  36. }
  37. // @ts-expect-error TODO: fix
  38. cache['$' + typedArray] = callBind(descriptor.get);
  39. }
  40. });
  41. } else {
  42. forEach(typedArrays, function (typedArray) {
  43. var arr = new g[typedArray]();
  44. var fn = arr.slice || arr.set;
  45. if (fn) {
  46. cache[
  47. /** @type {`$${import('.').TypedArrayName}`} */ ('$' + typedArray)
  48. ] = /** @type {import('./types').BoundSlice | import('./types').BoundSet} */ (
  49. // @ts-expect-error TODO FIXME
  50. callBind(fn)
  51. );
  52. }
  53. });
  54. }
  55. /** @type {(value: object) => false | import('.').TypedArrayName} */
  56. var tryTypedArrays = function tryAllTypedArrays(value) {
  57. /** @type {ReturnType<typeof tryAllTypedArrays>} */ var found = false;
  58. forEach(
  59. /** @type {Record<`\$${import('.').TypedArrayName}`, Getter>} */ (cache),
  60. /** @type {(getter: Getter, name: `\$${import('.').TypedArrayName}`) => void} */
  61. function (getter, typedArray) {
  62. if (!found) {
  63. try {
  64. // @ts-expect-error a throw is fine here
  65. if ('$' + getter(value) === typedArray) {
  66. found = /** @type {import('.').TypedArrayName} */ ($slice(typedArray, 1));
  67. }
  68. } catch (e) { /**/ }
  69. }
  70. }
  71. );
  72. return found;
  73. };
  74. /** @type {(value: object) => false | import('.').TypedArrayName} */
  75. var trySlices = function tryAllSlices(value) {
  76. /** @type {ReturnType<typeof tryAllSlices>} */ var found = false;
  77. forEach(
  78. /** @type {Record<`\$${import('.').TypedArrayName}`, Getter>} */(cache),
  79. /** @type {(getter: Getter, name: `\$${import('.').TypedArrayName}`) => void} */ function (getter, name) {
  80. if (!found) {
  81. try {
  82. // @ts-expect-error a throw is fine here
  83. getter(value);
  84. found = /** @type {import('.').TypedArrayName} */ ($slice(name, 1));
  85. } catch (e) { /**/ }
  86. }
  87. }
  88. );
  89. return found;
  90. };
  91. /** @type {import('.')} */
  92. module.exports = function whichTypedArray(value) {
  93. if (!value || typeof value !== 'object') { return false; }
  94. if (!hasToStringTag) {
  95. /** @type {string} */
  96. var tag = $slice($toString(value), 8, -1);
  97. if ($indexOf(typedArrays, tag) > -1) {
  98. return tag;
  99. }
  100. if (tag !== 'Object') {
  101. return false;
  102. }
  103. // node < 0.6 hits here on real Typed Arrays
  104. return trySlices(value);
  105. }
  106. if (!gOPD) { return null; } // unknown engine
  107. return tryTypedArrays(value);
  108. };