index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. var test = require('tape');
  3. var inspect = require('object-inspect');
  4. var whichBoxedPrimitive = require('..');
  5. var debug = function (v, m) { return inspect(v) + ' ' + m; };
  6. var forEach = function (arr, func) {
  7. var i;
  8. for (i = 0; i < arr.length; ++i) {
  9. func(arr[i], i, arr);
  10. }
  11. };
  12. var hasSymbols = require('has-symbols')();
  13. var hasBigInts = typeof BigInt === 'function';
  14. var primitives = [
  15. true,
  16. false,
  17. 42,
  18. NaN,
  19. Infinity,
  20. '',
  21. 'foo'
  22. ].concat(
  23. hasSymbols ? [Symbol(), Symbol.iterator] : [],
  24. hasBigInts ? BigInt(42) : []
  25. );
  26. var objects = [
  27. /a/g,
  28. new Date(),
  29. function () {},
  30. [],
  31. {}
  32. ];
  33. test('isBoxedPrimitive', function (t) {
  34. t.test('unboxed primitives', function (st) {
  35. forEach([null, undefined].concat(primitives), function (primitive) {
  36. st.equal(null, whichBoxedPrimitive(primitive), debug(primitive, 'is a primitive, but not a boxed primitive'));
  37. });
  38. st.end();
  39. });
  40. t.test('boxed primitives', function (st) {
  41. forEach(primitives, function (primitive) {
  42. var boxed = Object(primitive);
  43. var expected = boxed.constructor.name;
  44. st.equal(typeof expected, 'string', 'expected is string');
  45. st.equal(whichBoxedPrimitive(boxed), expected, debug(boxed, 'is a boxed primitive: ' + expected));
  46. });
  47. st.end();
  48. });
  49. t.test('non-primitive objects', function (st) {
  50. forEach(objects, function (object) {
  51. st.equal(undefined, whichBoxedPrimitive(object), debug(object, 'is not a primitive, boxed or otherwise'));
  52. });
  53. st.end();
  54. });
  55. t.end();
  56. });