9c734ee0c46dff2e59413ad4fa97be40ddb9b4b37ad24aeeed23a17e658a823dadf41a9c7cd4afbf9a04e2398db35e2350f5b722cc1c3e170fdfae996248da 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. var test = require('tape');
  3. var typedArrayByteLength = 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 availableTypedArrays = require('available-typed-arrays')();
  10. test('not arrays', function (t) {
  11. t.test('non-number/string primitives', function (st) {
  12. // @ts-expect-error
  13. st.equal(false, typedArrayByteLength(), 'undefined is not typed array');
  14. st.equal(false, typedArrayByteLength(null), 'null is not typed array');
  15. st.equal(false, typedArrayByteLength(false), 'false is not typed array');
  16. st.equal(false, typedArrayByteLength(true), 'true is not typed array');
  17. st.end();
  18. });
  19. t.equal(false, typedArrayByteLength({}), 'object is not typed array');
  20. t.equal(false, typedArrayByteLength(/a/g), 'regex literal is not typed array');
  21. t.equal(false, typedArrayByteLength(new RegExp('a', 'g')), 'regex object is not typed array');
  22. t.equal(false, typedArrayByteLength(new Date()), 'new Date() is not typed array');
  23. t.test('numbers', function (st) {
  24. st.equal(false, typedArrayByteLength(42), 'number is not typed array');
  25. st.equal(false, typedArrayByteLength(Object(42)), 'number object is not typed array');
  26. st.equal(false, typedArrayByteLength(NaN), 'NaN is not typed array');
  27. st.equal(false, typedArrayByteLength(Infinity), 'Infinity is not typed array');
  28. st.end();
  29. });
  30. t.test('strings', function (st) {
  31. st.equal(false, typedArrayByteLength('foo'), 'string primitive is not typed array');
  32. st.equal(false, typedArrayByteLength(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, typedArrayByteLength(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, typedArrayByteLength(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, typedArrayByteLength(arrowFn), 'arrow function is not typed array');
  49. t.end();
  50. });
  51. test('Typed Arrays', { skip: availableTypedArrays.length === 0 }, function (t) {
  52. var length = 64;
  53. var byteLength = 32;
  54. forEach(availableTypedArrays, function (typedArray) {
  55. var buffer = new ArrayBuffer(length);
  56. var TypedArray = global[typedArray];
  57. if (isCallable(TypedArray)) {
  58. // @ts-expect-error TS doesn't seem to know about the second TA arg
  59. var arr = new TypedArray(buffer, byteLength);
  60. t.equal(typedArrayByteLength(arr), byteLength, 'new ' + typedArray + '(new ArrayBuffer(' + length + '), ' + byteLength + ') is typed array of byte Length ' + byteLength);
  61. } else {
  62. t.comment('# SKIP ' + typedArray + ' is not supported');
  63. }
  64. });
  65. var buffer = new ArrayBuffer(8);
  66. var uint8 = new Uint8Array(buffer, 2);
  67. t.equal(typedArrayByteLength(uint8), 6, 'byteLength is as expected');
  68. t.end();
  69. });