43d1b13e1d969734c9d8f1c3710e98878009dd8419e75b6977d13816f1ff2c904acbf009f7c7ff215fb3a747e6b1b211ba635ea3385d762224f6efc43142c9 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. var test = require('tape');
  3. var typedArrayLength = require('../');
  4. var isCallable = require('is-callable');
  5. var generators = require('make-generator-function')();
  6. var arrowFn = require('make-arrow-function')();
  7. var forEach = require('for-each');
  8. var inspect = require('object-inspect');
  9. var typedArrayNames = require('possible-typed-array-names');
  10. test('not arrays', function (t) {
  11. t.test('non-number/string primitives', function (st) {
  12. // @ts-expect-error
  13. st.equal(false, typedArrayLength(), 'undefined is not typed array');
  14. st.equal(false, typedArrayLength(null), 'null is not typed array');
  15. st.equal(false, typedArrayLength(false), 'false is not typed array');
  16. st.equal(false, typedArrayLength(true), 'true is not typed array');
  17. st.end();
  18. });
  19. t.equal(false, typedArrayLength({}), 'object is not typed array');
  20. t.equal(false, typedArrayLength(/a/g), 'regex literal is not typed array');
  21. t.equal(false, typedArrayLength(new RegExp('a', 'g')), 'regex object is not typed array');
  22. t.equal(false, typedArrayLength(new Date()), 'new Date() is not typed array');
  23. t.test('numbers', function (st) {
  24. st.equal(false, typedArrayLength(42), 'number is not typed array');
  25. st.equal(false, typedArrayLength(Object(42)), 'number object is not typed array');
  26. st.equal(false, typedArrayLength(NaN), 'NaN is not typed array');
  27. st.equal(false, typedArrayLength(Infinity), 'Infinity is not typed array');
  28. st.end();
  29. });
  30. t.test('strings', function (st) {
  31. st.equal(false, typedArrayLength('foo'), 'string primitive is not typed array');
  32. st.equal(false, typedArrayLength(Object('foo')), 'string object is not typed array');
  33. st.end();
  34. });
  35. t.end();
  36. });
  37. test('Functions', function (t) {
  38. t.equal(false, typedArrayLength(function () {}), 'function is not typed array');
  39. t.end();
  40. });
  41. test('Generators', { skip: generators.length === 0 }, function (t) {
  42. forEach(generators, function (genFn) {
  43. t.equal(false, typedArrayLength(genFn), 'generator function ' + inspect(genFn) + ' is not typed array');
  44. });
  45. t.end();
  46. });
  47. test('Arrow functions', { skip: !arrowFn }, function (t) {
  48. t.equal(false, typedArrayLength(arrowFn), 'arrow function is not typed array');
  49. t.end();
  50. });
  51. test('Typed Arrays', function (t) {
  52. forEach(typedArrayNames, function (typedArray) {
  53. /** @type {Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor | BigInt64ArrayConstructor | BigUint64ArrayConstructor} */
  54. var TypedArray = global[typedArray];
  55. if (isCallable(TypedArray)) {
  56. var length = 10;
  57. var arr = new TypedArray(length);
  58. t.equal(typedArrayLength(arr), length, 'new ' + typedArray + '(10) is typed array of length ' + length);
  59. } else {
  60. t.comment('# SKIP ' + typedArray + ' is not supported');
  61. }
  62. });
  63. t.end();
  64. });