b73db3097dd365d8db19ace57134c2f8502432038ecfed7b7546eafbd4ffa8cb9fcacf4ca9c193f4e2211ba7ee6f199224b8aea178fcb17be9d04d6a558cc9 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. var test = require('tape');
  3. var whichTypedArray = require('../');
  4. var isCallable = require('is-callable');
  5. var hasToStringTag = require('has-tostringtag/shams')();
  6. var generators = require('make-generator-function')();
  7. var arrows = require('make-arrow-function').list();
  8. var forEach = require('for-each');
  9. var typedArrayNames = [
  10. 'Int8Array',
  11. 'Uint8Array',
  12. 'Uint8ClampedArray',
  13. 'Int16Array',
  14. 'Uint16Array',
  15. 'Int32Array',
  16. 'Uint32Array',
  17. 'Float32Array',
  18. 'Float64Array',
  19. 'BigInt64Array',
  20. 'BigUint64Array'
  21. ];
  22. test('not arrays', function (t) {
  23. t.test('non-number/string primitives', function (st) {
  24. // @ts-expect-error
  25. st.equal(false, whichTypedArray(), 'undefined is not typed array');
  26. st.equal(false, whichTypedArray(null), 'null is not typed array');
  27. st.equal(false, whichTypedArray(false), 'false is not typed array');
  28. st.equal(false, whichTypedArray(true), 'true is not typed array');
  29. st.end();
  30. });
  31. t.equal(false, whichTypedArray({}), 'object is not typed array');
  32. t.equal(false, whichTypedArray(/a/g), 'regex literal is not typed array');
  33. t.equal(false, whichTypedArray(new RegExp('a', 'g')), 'regex object is not typed array');
  34. t.equal(false, whichTypedArray(new Date()), 'new Date() is not typed array');
  35. t.test('numbers', function (st) {
  36. st.equal(false, whichTypedArray(42), 'number is not typed array');
  37. st.equal(false, whichTypedArray(Object(42)), 'number object is not typed array');
  38. st.equal(false, whichTypedArray(NaN), 'NaN is not typed array');
  39. st.equal(false, whichTypedArray(Infinity), 'Infinity is not typed array');
  40. st.end();
  41. });
  42. t.test('strings', function (st) {
  43. st.equal(false, whichTypedArray('foo'), 'string primitive is not typed array');
  44. st.equal(false, whichTypedArray(Object('foo')), 'string object is not typed array');
  45. st.end();
  46. });
  47. t.end();
  48. });
  49. test('Functions', function (t) {
  50. t.equal(false, whichTypedArray(function () {}), 'function is not typed array');
  51. t.end();
  52. });
  53. test('Generators', { skip: generators.length === 0 }, function (t) {
  54. forEach(generators, function (genFn) {
  55. t.equal(false, whichTypedArray(genFn), 'generator function ' + genFn + ' is not typed array');
  56. });
  57. t.end();
  58. });
  59. test('Arrow functions', { skip: arrows.length === 0 }, function (t) {
  60. forEach(arrows, function (arrowFn) {
  61. t.equal(false, whichTypedArray(arrowFn), 'arrow function ' + arrowFn + ' is not typed array');
  62. });
  63. t.end();
  64. });
  65. test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
  66. forEach(typedArrayNames, function (typedArray) {
  67. // @ts-expect-error TODO: fix
  68. if (typeof global[typedArray] === 'function') {
  69. // @ts-expect-error TODO: fix
  70. var fakeTypedArray = [];
  71. // @ts-expect-error TODO: fix
  72. fakeTypedArray[Symbol.toStringTag] = typedArray;
  73. // @ts-expect-error TODO: fix
  74. t.equal(false, whichTypedArray(fakeTypedArray), 'faked ' + typedArray + ' is not typed array');
  75. } else {
  76. t.comment('# SKIP ' + typedArray + ' is not supported');
  77. }
  78. });
  79. t.end();
  80. });
  81. test('Typed Arrays', function (t) {
  82. forEach(typedArrayNames, function (typedArray) {
  83. // @ts-expect-error TODO: fix
  84. /** @type {import('../').TypedArrayConstructor} */ var TypedArray = global[typedArray];
  85. if (isCallable(TypedArray)) {
  86. var arr = new TypedArray(10);
  87. t.equal(whichTypedArray(arr), typedArray, 'new ' + typedArray + '(10) is typed array of type ' + typedArray);
  88. } else {
  89. t.comment('# SKIP ' + typedArray + ' is not supported');
  90. }
  91. });
  92. t.end();
  93. });