index.js 1.1 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 hasSymbols = require('has-symbols')();
  7. var hasBigInts = require('has-bigints')();
  8. var unboxPrimitive = require('..');
  9. var debug = function (v, m) { return inspect(v) + ' ' + m; };
  10. test('primitives', function (t) {
  11. var primitives = [
  12. true,
  13. false,
  14. '',
  15. 'foo',
  16. 42,
  17. NaN,
  18. Infinity,
  19. 0
  20. ];
  21. if (hasSymbols) {
  22. primitives.push(Symbol(), Symbol.iterator, Symbol('f'));
  23. }
  24. if (hasBigInts) {
  25. primitives.push(BigInt(42), BigInt(0));
  26. }
  27. forEach(primitives, function (primitive) {
  28. var obj = Object(primitive);
  29. t.ok(
  30. is(unboxPrimitive(obj), primitive),
  31. debug(obj, 'unboxes to ' + inspect(primitive))
  32. );
  33. });
  34. t.end();
  35. });
  36. test('objects', function (t) {
  37. var objects = [
  38. {},
  39. [],
  40. function () {},
  41. /a/g,
  42. new Date()
  43. ];
  44. forEach(objects, function (object) {
  45. t['throws'](
  46. function () { unboxPrimitive(object); },
  47. TypeError,
  48. debug(object, 'is not a primitive')
  49. );
  50. });
  51. t.end();
  52. });