es2015.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict';
  2. var test = require('tape');
  3. var toPrimitive = require('../es2015');
  4. var is = require('object-is');
  5. var forEach = require('for-each');
  6. var functionName = require('function.prototype.name');
  7. var debug = require('object-inspect');
  8. var v = require('es-value-fixtures');
  9. /** @typedef {{ toString?: unknown, valueOf?: unknown, [Symbol.toPrimitive]?: unknown }} Coercible */
  10. test('function properties', function (t) {
  11. t.equal(toPrimitive.length, 1, 'length is 1');
  12. t.equal(functionName(toPrimitive), 'ToPrimitive', 'name is ToPrimitive');
  13. t.end();
  14. });
  15. test('primitives', function (t) {
  16. forEach(v.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: !v.hasSymbols }, function (t) {
  24. forEach(v.symbols, function (sym) {
  25. t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value');
  26. t.equal(toPrimitive(sym, String), sym, 'toPrimitive(' + debug(sym) + ', String) returns the same value');
  27. t.equal(toPrimitive(sym, Number), sym, 'toPrimitive(' + debug(sym) + ', Number) returns the same value');
  28. });
  29. var primitiveSym = Symbol('primitiveSym');
  30. var objectSym = Object(primitiveSym);
  31. t.equal(toPrimitive(objectSym), primitiveSym, 'toPrimitive(' + debug(objectSym) + ') returns ' + debug(primitiveSym));
  32. t.equal(toPrimitive(objectSym, String), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', String) returns ' + debug(primitiveSym));
  33. t.equal(toPrimitive(objectSym, Number), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', Number) returns ' + debug(primitiveSym));
  34. t.end();
  35. });
  36. test('Arrays', function (t) {
  37. var arrays = [[], ['a', 'b'], [1, 2]];
  38. forEach(arrays, function (arr) {
  39. t.equal(toPrimitive(arr), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array');
  40. t.equal(toPrimitive(arr, String), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array');
  41. t.equal(toPrimitive(arr, Number), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array');
  42. });
  43. t.end();
  44. });
  45. test('Dates', function (t) {
  46. var dates = [new Date(), new Date(0), new Date(NaN)];
  47. forEach(dates, function (date) {
  48. t.equal(toPrimitive(date), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date');
  49. t.equal(toPrimitive(date, String), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date');
  50. t.ok(is(toPrimitive(date, Number), Number(date)), 'toPrimitive(' + debug(date) + ') returns the number version of the date');
  51. });
  52. t.end();
  53. });
  54. test('Objects', function (t) {
  55. t.equal(toPrimitive(v.coercibleObject), v.coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf');
  56. t.equal(toPrimitive(v.coercibleObject, Number), v.coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf');
  57. t.equal(toPrimitive(v.coercibleObject, String), v.coercibleObject.toString(), 'coercibleObject with hint String coerces to non-stringified toString');
  58. t.equal(toPrimitive(v.coercibleFnObject), v.coercibleFnObject.toString(), 'coercibleFnObject coerces to non-stringified toString');
  59. t.equal(toPrimitive(v.coercibleFnObject, Number), v.coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to non-stringified toString');
  60. t.equal(toPrimitive(v.coercibleFnObject, String), v.coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to non-stringified toString');
  61. t.equal(toPrimitive({}), '[object Object]', '{} with no hint coerces to Object#toString');
  62. t.equal(toPrimitive({}, Number), '[object Object]', '{} with hint Number coerces to Object#toString');
  63. t.equal(toPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString');
  64. t.equal(toPrimitive(v.toStringOnlyObject), v.toStringOnlyObject.toString(), 'toStringOnlyObject returns non-stringified toString');
  65. t.equal(toPrimitive(v.toStringOnlyObject, Number), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns non-stringified toString');
  66. t.equal(toPrimitive(v.toStringOnlyObject, String), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns non-stringified toString');
  67. t.equal(toPrimitive(v.valueOfOnlyObject), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');
  68. t.equal(toPrimitive(v.valueOfOnlyObject, Number), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf');
  69. t.equal(toPrimitive(v.valueOfOnlyObject, String), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns non-stringified valueOf');
  70. t.test('Symbol.toPrimitive', { skip: !v.hasSymbols || !Symbol.toPrimitive }, function (st) {
  71. /** @type {Coercible} */
  72. var overriddenObject = { toString: st.fail, valueOf: st.fail };
  73. overriddenObject[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ function (hint) {
  74. return String(hint);
  75. };
  76. st.equal(toPrimitive(overriddenObject), 'default', 'object with Symbol.toPrimitive + no hint invokes that');
  77. st.equal(toPrimitive(overriddenObject, Number), 'number', 'object with Symbol.toPrimitive + hint Number invokes that');
  78. st.equal(toPrimitive(overriddenObject, String), 'string', 'object with Symbol.toPrimitive + hint String invokes that');
  79. /** @type {Coercible} */
  80. var nullToPrimitive = { toString: v.coercibleObject.toString, valueOf: v.coercibleObject.valueOf };
  81. nullToPrimitive[Symbol.toPrimitive] = null;
  82. st.equal(toPrimitive(nullToPrimitive), toPrimitive(v.coercibleObject), 'object with no hint + null Symbol.toPrimitive ignores it');
  83. st.equal(toPrimitive(nullToPrimitive, Number), toPrimitive(v.coercibleObject, Number), 'object with hint Number + null Symbol.toPrimitive ignores it');
  84. st.equal(toPrimitive(nullToPrimitive, String), toPrimitive(v.coercibleObject, String), 'object with hint String + null Symbol.toPrimitive ignores it');
  85. st.test('exceptions', function (sst) {
  86. /** @type {Coercible} */
  87. var nonFunctionToPrimitive = { toString: sst.fail, valueOf: sst.fail };
  88. nonFunctionToPrimitive[Symbol.toPrimitive] = {};
  89. sst['throws'](toPrimitive.bind(null, nonFunctionToPrimitive), TypeError, 'Symbol.toPrimitive returning a non-function throws');
  90. /** @type {Coercible} */
  91. var uncoercibleToPrimitive = { toString: sst.fail, valueOf: sst.fail };
  92. uncoercibleToPrimitive[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ function (hint) {
  93. return { toString: function () { return hint; } };
  94. };
  95. sst['throws'](toPrimitive.bind(null, uncoercibleToPrimitive), TypeError, 'Symbol.toPrimitive returning an object throws');
  96. /** @type {Coercible} */
  97. var throwingToPrimitive = { toString: sst.fail, valueOf: sst.fail };
  98. throwingToPrimitive[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ function (hint) {
  99. throw new RangeError(hint);
  100. };
  101. sst['throws'](toPrimitive.bind(null, throwingToPrimitive), RangeError, 'Symbol.toPrimitive throwing throws');
  102. sst.end();
  103. });
  104. st.end();
  105. });
  106. t.test('exceptions', function (st) {
  107. st['throws'](toPrimitive.bind(null, v.uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError');
  108. st['throws'](toPrimitive.bind(null, v.uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError');
  109. st['throws'](toPrimitive.bind(null, v.uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError');
  110. st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError');
  111. st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError');
  112. st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError');
  113. st.end();
  114. });
  115. t.end();
  116. });