es5.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. var test = require('tape');
  3. var toPrimitive = require('../es5');
  4. var is = require('object-is');
  5. var forEach = require('foreach');
  6. var functionName = require('function.prototype.name');
  7. var debug = require('object-inspect');
  8. var hasSymbols = require('has-symbols')();
  9. test('function properties', function (t) {
  10. t.equal(toPrimitive.length, 1, 'length is 1');
  11. t.equal(functionName(toPrimitive), 'ToPrimitive', 'name is ToPrimitive');
  12. t.end();
  13. });
  14. var primitives = [null, undefined, true, false, 0, -0, 42, NaN, Infinity, -Infinity, '', 'abc'];
  15. test('primitives', function (t) {
  16. forEach(primitives, function (i) {
  17. t.ok(is(toPrimitive(i), i), 'toPrimitive(' + debug(i) + ') returns the same value');
  18. t.ok(is(toPrimitive(i, String), i), 'toPrimitive(' + debug(i) + ', String) returns the same value');
  19. t.ok(is(toPrimitive(i, Number), i), 'toPrimitive(' + debug(i) + ', Number) returns the same value');
  20. });
  21. t.end();
  22. });
  23. test('Symbols', { skip: !hasSymbols }, function (t) {
  24. var symbols = [
  25. Symbol('foo'),
  26. Symbol.iterator,
  27. Symbol['for']('foo') // eslint-disable-line no-restricted-properties
  28. ];
  29. forEach(symbols, function (sym) {
  30. t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value');
  31. t.equal(toPrimitive(sym, String), sym, 'toPrimitive(' + debug(sym) + ', String) returns the same value');
  32. t.equal(toPrimitive(sym, Number), sym, 'toPrimitive(' + debug(sym) + ', Number) returns the same value');
  33. });
  34. var primitiveSym = Symbol('primitiveSym');
  35. var stringSym = Symbol.prototype.toString.call(primitiveSym);
  36. var objectSym = Object(primitiveSym);
  37. t.equal(toPrimitive(objectSym), primitiveSym, 'toPrimitive(' + debug(objectSym) + ') returns ' + debug(primitiveSym));
  38. // This is different from ES2015, as the ES5 algorithm doesn't account for the existence of Symbols:
  39. t.equal(toPrimitive(objectSym, String), stringSym, 'toPrimitive(' + debug(objectSym) + ', String) returns ' + debug(stringSym));
  40. t.equal(toPrimitive(objectSym, Number), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', Number) returns ' + debug(primitiveSym));
  41. t.end();
  42. });
  43. test('Arrays', function (t) {
  44. var arrays = [[], ['a', 'b'], [1, 2]];
  45. forEach(arrays, function (arr) {
  46. t.ok(is(toPrimitive(arr), arr.toString()), 'toPrimitive(' + debug(arr) + ') returns toString of the array');
  47. t.equal(toPrimitive(arr, String), arr.toString(), 'toPrimitive(' + debug(arr) + ') returns toString of the array');
  48. t.ok(is(toPrimitive(arr, Number), arr.toString()), 'toPrimitive(' + debug(arr) + ') returns toString of the array');
  49. });
  50. t.end();
  51. });
  52. test('Dates', function (t) {
  53. var dates = [new Date(), new Date(0), new Date(NaN)];
  54. forEach(dates, function (date) {
  55. t.equal(toPrimitive(date), date.toString(), 'toPrimitive(' + debug(date) + ') returns toString of the date');
  56. t.equal(toPrimitive(date, String), date.toString(), 'toPrimitive(' + debug(date) + ') returns toString of the date');
  57. t.ok(is(toPrimitive(date, Number), date.valueOf()), 'toPrimitive(' + debug(date) + ') returns valueOf of the date');
  58. });
  59. t.end();
  60. });
  61. var coercibleObject = { valueOf: function () { return 3; }, toString: function () { return 42; } };
  62. var valueOfOnlyObject = { valueOf: function () { return 4; }, toString: function () { return {}; } };
  63. var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } };
  64. var coercibleFnObject = {
  65. valueOf: function () { return function valueOfFn() {}; },
  66. toString: function () { return 42; }
  67. };
  68. var uncoercibleObject = { valueOf: function () { return {}; }, toString: function () { return {}; } };
  69. var uncoercibleFnObject = {
  70. valueOf: function () { return function valueOfFn() {}; },
  71. toString: function () { return function toStrFn() {}; }
  72. };
  73. test('Objects', function (t) {
  74. t.equal(toPrimitive(coercibleObject), coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf');
  75. t.equal(toPrimitive(coercibleObject, String), coercibleObject.toString(), 'coercibleObject with hint String coerces to toString');
  76. t.equal(toPrimitive(coercibleObject, Number), coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf');
  77. t.equal(toPrimitive(coercibleFnObject), coercibleFnObject.toString(), 'coercibleFnObject coerces to toString');
  78. t.equal(toPrimitive(coercibleFnObject, String), coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to toString');
  79. t.equal(toPrimitive(coercibleFnObject, Number), coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to toString');
  80. t.ok(is(toPrimitive({}), '[object Object]'), '{} with no hint coerces to Object#toString');
  81. t.equal(toPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString');
  82. t.ok(is(toPrimitive({}, Number), '[object Object]'), '{} with hint Number coerces to Object#toString');
  83. t.equal(toPrimitive(toStringOnlyObject), toStringOnlyObject.toString(), 'toStringOnlyObject returns toString');
  84. t.equal(toPrimitive(toStringOnlyObject, String), toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns toString');
  85. t.equal(toPrimitive(toStringOnlyObject, Number), toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns toString');
  86. t.equal(toPrimitive(valueOfOnlyObject), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');
  87. t.equal(toPrimitive(valueOfOnlyObject, String), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns valueOf');
  88. t.equal(toPrimitive(valueOfOnlyObject, Number), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf');
  89. t.test('exceptions', function (st) {
  90. st['throws'](toPrimitive.bind(null, uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError');
  91. st['throws'](toPrimitive.bind(null, uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError');
  92. st['throws'](toPrimitive.bind(null, uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError');
  93. st['throws'](toPrimitive.bind(null, uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError');
  94. st['throws'](toPrimitive.bind(null, uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError');
  95. st['throws'](toPrimitive.bind(null, uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError');
  96. st.end();
  97. });
  98. t.end();
  99. });