7f0a3aa49e067fc0c8c94e163f4e2d2ec1ef2e33036d737494bbc91fd84ee03fded536c2f88a6ad69a4c0dd187dab0320d2f00515b541e0bb4053a824ab79b 3.5 KB

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