index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. var test = require('tape');
  3. var inspect = require('object-inspect');
  4. var forEach = require('for-each');
  5. var v = require('es-value-fixtures');
  6. var availableTypedArrays = require('available-typed-arrays')();
  7. var isArrayBuffer = require('..');
  8. test('isArrayBuffer', function (t) {
  9. t.equal(typeof isArrayBuffer, 'function', 'is a function');
  10. /** @type {unknown[]} */
  11. var nonABs = [].concat(
  12. // @ts-expect-error TS sucks with [].concat
  13. v.primitives,
  14. v.objects,
  15. typeof SharedArrayBuffer === 'function' ? new SharedArrayBuffer(0) : []
  16. );
  17. forEach(nonABs, function (nonAB) {
  18. t.equal(isArrayBuffer(nonAB), false, inspect(nonAB) + ' is not an ArrayBuffer');
  19. });
  20. t.test('actual ArrayBuffer instances', { skip: typeof ArrayBuffer === 'undefined' }, function (st) {
  21. // @ts-expect-error TS grumbles about 0 args
  22. var ab = new ArrayBuffer();
  23. st.equal(isArrayBuffer(ab), true, inspect(ab) + ' is an ArrayBuffer');
  24. var ab42 = new ArrayBuffer(42);
  25. st.equal(isArrayBuffer(ab42), true, inspect(ab42) + ' is an ArrayBuffer');
  26. var dv = new DataView(ab42);
  27. st.equal(isArrayBuffer(dv), false, inspect(dv) + ' is not an ArrayBuffer');
  28. st.end();
  29. });
  30. t.test('Typed Arrays', { skip: availableTypedArrays.length === 0 }, function (st) {
  31. forEach(availableTypedArrays, function (TypedArray) {
  32. var ta = new global[TypedArray](0);
  33. st.equal(isArrayBuffer(ta.buffer), true, inspect(ta.buffer) + ', the TA\'s buffer, is an ArrayBuffer');
  34. st.equal(isArrayBuffer(ta), false, inspect(ta) + ' is not an ArrayBuffer');
  35. });
  36. st.end();
  37. });
  38. t.end();
  39. });