index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. var test = require('tape');
  3. var inspect = require('object-inspect');
  4. var is = require('object-is');
  5. var forEach = require('for-each');
  6. var v = require('es-value-fixtures');
  7. var unboxPrimitive = require('..');
  8. test('primitives', function (t) {
  9. forEach([null, undefined], function (nullValue) {
  10. t['throws'](
  11. // @ts-expect-error
  12. function () { unboxPrimitive(nullValue); },
  13. TypeError,
  14. inspect(nullValue) + ' is not a primitive'
  15. );
  16. });
  17. // eslint-disable-next-line no-extra-parens
  18. forEach(/** @type {typeof v.nonNullPrimitives} */ ([].concat(
  19. // @ts-expect-error TS sucks with concat
  20. v.nonNullPrimitives,
  21. v.zeroes,
  22. v.infinities,
  23. NaN
  24. )), function (primitive) {
  25. var obj = Object(primitive);
  26. t.ok(
  27. is(unboxPrimitive(obj), primitive),
  28. inspect(obj) + 'unboxes to ' + inspect(primitive)
  29. );
  30. });
  31. t.end();
  32. });
  33. test('objects', function (t) {
  34. // eslint-disable-next-line no-extra-parens
  35. forEach(/** @type {typeof v.objects} */ (/** @type {unknown} */ ([].concat(
  36. // @ts-expect-error TS sucks with concat
  37. v.objects,
  38. {},
  39. [],
  40. function () {},
  41. /a/g,
  42. new Date()
  43. ))), function (object) {
  44. t['throws'](
  45. // @ts-expect-error
  46. function () { unboxPrimitive(object); },
  47. TypeError,
  48. inspect(object) + ' is not a primitive'
  49. );
  50. });
  51. t.end();
  52. });