| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var test = require('tape'); |
| 4 | var toPrimitive = require('../es6'); |
| 5 | var is = require('object-is'); |
| 6 | var forEach = require('foreach'); |
| 7 | var functionName = require('function.prototype.name'); |
| 8 | var debug = require('object-inspect'); |
| 9 | |
| 10 | var hasSymbols = require('has-symbols')(); |
| 11 | var hasSymbolToPrimitive = hasSymbols && typeof Symbol.toPrimitive === 'symbol'; |
| 12 | |
| 13 | test('function properties', function (t) { |
| 14 | t.equal(toPrimitive.length, 1, 'length is 1'); |
| 15 | t.equal(functionName(toPrimitive), 'ToPrimitive', 'name is ToPrimitive'); |
| 16 | |
| 17 | t.end(); |
| 18 | }); |
| 19 | |
| 20 | var primitives = [null, undefined, true, false, 0, -0, 42, NaN, Infinity, -Infinity, '', 'abc']; |
| 21 | |
| 22 | test('primitives', function (t) { |
| 23 | forEach(primitives, function (i) { |
| 24 | t.ok(is(toPrimitive(i), i), 'toPrimitive(' + debug(i) + ') returns the same value'); |
| 25 | t.ok(is(toPrimitive(i, String), i), 'toPrimitive(' + debug(i) + ', String) returns the same value'); |
| 26 | t.ok(is(toPrimitive(i, Number), i), 'toPrimitive(' + debug(i) + ', Number) returns the same value'); |
| 27 | }); |
| 28 | t.end(); |
| 29 | }); |
| 30 | |
| 31 | test('Symbols', { skip: !hasSymbols }, function (t) { |
| 32 | var symbols = [ |
| 33 | Symbol('foo'), |
| 34 | Symbol.iterator, |
| 35 | Symbol['for']('foo') // eslint-disable-line no-restricted-properties |
| 36 | ]; |
| 37 | forEach(symbols, function (sym) { |
| 38 | t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value'); |
| 39 | t.equal(toPrimitive(sym, String), sym, 'toPrimitive(' + debug(sym) + ', String) returns the same value'); |
| 40 | t.equal(toPrimitive(sym, Number), sym, 'toPrimitive(' + debug(sym) + ', Number) returns the same value'); |
| 41 | }); |
| 42 | |
| 43 | var primitiveSym = Symbol('primitiveSym'); |
| 44 | var objectSym = Object(primitiveSym); |
| 45 | t.equal(toPrimitive(objectSym), primitiveSym, 'toPrimitive(' + debug(objectSym) + ') returns ' + debug(primitiveSym)); |
| 46 | t.equal(toPrimitive(objectSym, String), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', String) returns ' + debug(primitiveSym)); |
| 47 | t.equal(toPrimitive(objectSym, Number), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', Number) returns ' + debug(primitiveSym)); |
| 48 | t.end(); |
| 49 | }); |
| 50 | |
| 51 | test('Arrays', function (t) { |
| 52 | var arrays = [[], ['a', 'b'], [1, 2]]; |
| 53 | forEach(arrays, function (arr) { |
| 54 | t.equal(toPrimitive(arr), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array'); |
| 55 | t.equal(toPrimitive(arr, String), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array'); |
| 56 | t.equal(toPrimitive(arr, Number), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array'); |
| 57 | }); |
| 58 | t.end(); |
| 59 | }); |
| 60 | |
| 61 | test('Dates', function (t) { |
| 62 | var dates = [new Date(), new Date(0), new Date(NaN)]; |
| 63 | forEach(dates, function (date) { |
| 64 | t.equal(toPrimitive(date), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date'); |
| 65 | t.equal(toPrimitive(date, String), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date'); |
| 66 | t.ok(is(toPrimitive(date, Number), Number(date)), 'toPrimitive(' + debug(date) + ') returns the number version of the date'); |
| 67 | }); |
| 68 | t.end(); |
| 69 | }); |
| 70 | |
| 71 | var coercibleObject = { valueOf: function () { return 3; }, toString: function () { return 42; } }; |
| 72 | var valueOfOnlyObject = { valueOf: function () { return 4; }, toString: function () { return {}; } }; |
| 73 | var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } }; |
| 74 | var coercibleFnObject = { |
| 75 | valueOf: function () { return function valueOfFn() {}; }, |
| 76 | toString: function () { return 42; } |
| 77 | }; |
| 78 | var uncoercibleObject = { valueOf: function () { return {}; }, toString: function () { return {}; } }; |
| 79 | var uncoercibleFnObject = { |
| 80 | valueOf: function () { return function valueOfFn() {}; }, |
| 81 | toString: function () { return function toStrFn() {}; } |
| 82 | }; |
| 83 | |
| 84 | test('Objects', function (t) { |
| 85 | t.equal(toPrimitive(coercibleObject), coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf'); |
| 86 | t.equal(toPrimitive(coercibleObject, Number), coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf'); |
| 87 | t.equal(toPrimitive(coercibleObject, String), coercibleObject.toString(), 'coercibleObject with hint String coerces to non-stringified toString'); |
| 88 | |
| 89 | t.equal(toPrimitive(coercibleFnObject), coercibleFnObject.toString(), 'coercibleFnObject coerces to non-stringified toString'); |
| 90 | t.equal(toPrimitive(coercibleFnObject, Number), coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to non-stringified toString'); |
| 91 | t.equal(toPrimitive(coercibleFnObject, String), coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to non-stringified toString'); |
| 92 | |
| 93 | t.equal(toPrimitive({}), '[object Object]', '{} with no hint coerces to Object#toString'); |
| 94 | t.equal(toPrimitive({}, Number), '[object Object]', '{} with hint Number coerces to Object#toString'); |
| 95 | t.equal(toPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString'); |
| 96 | |
| 97 | t.equal(toPrimitive(toStringOnlyObject), toStringOnlyObject.toString(), 'toStringOnlyObject returns non-stringified toString'); |
| 98 | t.equal(toPrimitive(toStringOnlyObject, Number), toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns non-stringified toString'); |
| 99 | t.equal(toPrimitive(toStringOnlyObject, String), toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns non-stringified toString'); |
| 100 | |
| 101 | t.equal(toPrimitive(valueOfOnlyObject), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf'); |
| 102 | t.equal(toPrimitive(valueOfOnlyObject, Number), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf'); |
| 103 | t.equal(toPrimitive(valueOfOnlyObject, String), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns non-stringified valueOf'); |
| 104 | |
| 105 | t.test('Symbol.toPrimitive', { skip: !hasSymbolToPrimitive }, function (st) { |
| 106 | var overriddenObject = { toString: st.fail, valueOf: st.fail }; |
| 107 | overriddenObject[Symbol.toPrimitive] = function (hint) { return String(hint); }; |
| 108 | |
| 109 | st.equal(toPrimitive(overriddenObject), 'default', 'object with Symbol.toPrimitive + no hint invokes that'); |
| 110 | st.equal(toPrimitive(overriddenObject, Number), 'number', 'object with Symbol.toPrimitive + hint Number invokes that'); |
| 111 | st.equal(toPrimitive(overriddenObject, String), 'string', 'object with Symbol.toPrimitive + hint String invokes that'); |
| 112 | |
| 113 | var nullToPrimitive = { toString: coercibleObject.toString, valueOf: coercibleObject.valueOf }; |
| 114 | nullToPrimitive[Symbol.toPrimitive] = null; |
| 115 | st.equal(toPrimitive(nullToPrimitive), toPrimitive(coercibleObject), 'object with no hint + null Symbol.toPrimitive ignores it'); |
| 116 | st.equal(toPrimitive(nullToPrimitive, Number), toPrimitive(coercibleObject, Number), 'object with hint Number + null Symbol.toPrimitive ignores it'); |
| 117 | st.equal(toPrimitive(nullToPrimitive, String), toPrimitive(coercibleObject, String), 'object with hint String + null Symbol.toPrimitive ignores it'); |
| 118 | |
| 119 | st.test('exceptions', function (sst) { |
| 120 | var nonFunctionToPrimitive = { toString: sst.fail, valueOf: sst.fail }; |
| 121 | nonFunctionToPrimitive[Symbol.toPrimitive] = {}; |
| 122 | sst['throws'](toPrimitive.bind(null, nonFunctionToPrimitive), TypeError, 'Symbol.toPrimitive returning a non-function throws'); |
| 123 | |
| 124 | var uncoercibleToPrimitive = { toString: sst.fail, valueOf: sst.fail }; |
| 125 | uncoercibleToPrimitive[Symbol.toPrimitive] = function (hint) { |
| 126 | return { toString: function () { return hint; } }; |
| 127 | }; |
| 128 | sst['throws'](toPrimitive.bind(null, uncoercibleToPrimitive), TypeError, 'Symbol.toPrimitive returning an object throws'); |
| 129 | |
| 130 | var throwingToPrimitive = { toString: sst.fail, valueOf: sst.fail }; |
| 131 | throwingToPrimitive[Symbol.toPrimitive] = function (hint) { throw new RangeError(hint); }; |
| 132 | sst['throws'](toPrimitive.bind(null, throwingToPrimitive), RangeError, 'Symbol.toPrimitive throwing throws'); |
| 133 | |
| 134 | sst.end(); |
| 135 | }); |
| 136 | |
| 137 | st.end(); |
| 138 | }); |
| 139 | |
| 140 | t.test('exceptions', function (st) { |
| 141 | st['throws'](toPrimitive.bind(null, uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError'); |
| 142 | st['throws'](toPrimitive.bind(null, uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError'); |
| 143 | st['throws'](toPrimitive.bind(null, uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError'); |
| 144 | |
| 145 | st['throws'](toPrimitive.bind(null, uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError'); |
| 146 | st['throws'](toPrimitive.bind(null, uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError'); |
| 147 | st['throws'](toPrimitive.bind(null, uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError'); |
| 148 | st.end(); |
| 149 | }); |
| 150 | t.end(); |
| 151 | }); |