Legend:
- Unmodified
- Added
- Removed
-
imaps-frontend/node_modules/es-to-primitive/test/es6.js
rd565449 r0c6b92a 4 4 var toPrimitive = require('../es6'); 5 5 var is = require('object-is'); 6 var forEach = require('for each');6 var forEach = require('for-each'); 7 7 var functionName = require('function.prototype.name'); 8 8 var debug = require('object-inspect'); 9 var v = require('es-value-fixtures'); 9 10 10 var hasSymbols = require('has-symbols')(); 11 var hasSymbolToPrimitive = hasSymbols && typeof Symbol.toPrimitive === 'symbol'; 11 /** @typedef {{ toString?: unknown, valueOf?: unknown, [Symbol.toPrimitive]?: unknown }} Coercible */ 12 12 13 13 test('function properties', function (t) { … … 18 18 }); 19 19 20 var primitives = [null, undefined, true, false, 0, -0, 42, NaN, Infinity, -Infinity, '', 'abc'];21 22 20 test('primitives', function (t) { 23 forEach( primitives, function (i) {21 forEach(v.primitives, function (i) { 24 22 t.ok(is(toPrimitive(i), i), 'toPrimitive(' + debug(i) + ') returns the same value'); 25 23 t.ok(is(toPrimitive(i, String), i), 'toPrimitive(' + debug(i) + ', String) returns the same value'); … … 29 27 }); 30 28 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) { 29 test('Symbols', { skip: !v.hasSymbols }, function (t) { 30 forEach(v.symbols, function (sym) { 38 31 t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value'); 39 32 t.equal(toPrimitive(sym, String), sym, 'toPrimitive(' + debug(sym) + ', String) returns the same value'); … … 69 62 }); 70 63 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 }; 64 test('Objects', function (t) { 65 t.equal(toPrimitive(v.coercibleObject), v.coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf'); 66 t.equal(toPrimitive(v.coercibleObject, Number), v.coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf'); 67 t.equal(toPrimitive(v.coercibleObject, String), v.coercibleObject.toString(), 'coercibleObject with hint String coerces to non-stringified toString'); 83 68 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'); 69 t.equal(toPrimitive(v.coercibleFnObject), v.coercibleFnObject.toString(), 'coercibleFnObject coerces to non-stringified toString'); 70 t.equal(toPrimitive(v.coercibleFnObject, Number), v.coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to non-stringified toString'); 71 t.equal(toPrimitive(v.coercibleFnObject, String), v.coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to non-stringified toString'); 92 72 93 73 t.equal(toPrimitive({}), '[object Object]', '{} with no hint coerces to Object#toString'); … … 95 75 t.equal(toPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString'); 96 76 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');77 t.equal(toPrimitive(v.toStringOnlyObject), v.toStringOnlyObject.toString(), 'toStringOnlyObject returns non-stringified toString'); 78 t.equal(toPrimitive(v.toStringOnlyObject, Number), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns non-stringified toString'); 79 t.equal(toPrimitive(v.toStringOnlyObject, String), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns non-stringified toString'); 100 80 101 t.equal(toPrimitive(v alueOfOnlyObject),valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');102 t.equal(toPrimitive(v alueOfOnlyObject, Number),valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf');103 t.equal(toPrimitive(v alueOfOnlyObject, String),valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns non-stringified valueOf');81 t.equal(toPrimitive(v.valueOfOnlyObject), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf'); 82 t.equal(toPrimitive(v.valueOfOnlyObject, Number), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf'); 83 t.equal(toPrimitive(v.valueOfOnlyObject, String), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns non-stringified valueOf'); 104 84 105 t.test('Symbol.toPrimitive', { skip: !hasSymbolToPrimitive }, function (st) { 85 t.test('Symbol.toPrimitive', { skip: !v.hasSymbols || !Symbol.toPrimitive }, function (st) { 86 /** @type {Coercible} */ 106 87 var overriddenObject = { toString: st.fail, valueOf: st.fail }; 107 overriddenObject[Symbol.toPrimitive] = function (hint) { return String(hint); }; 88 overriddenObject[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ function (hint) { 89 return String(hint); 90 }; 108 91 109 92 st.equal(toPrimitive(overriddenObject), 'default', 'object with Symbol.toPrimitive + no hint invokes that'); … … 111 94 st.equal(toPrimitive(overriddenObject, String), 'string', 'object with Symbol.toPrimitive + hint String invokes that'); 112 95 113 var nullToPrimitive = { toString: coercibleObject.toString, valueOf: coercibleObject.valueOf }; 96 /** @type {Coercible} */ 97 var nullToPrimitive = { toString: v.coercibleObject.toString, valueOf: v.coercibleObject.valueOf }; 114 98 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');99 st.equal(toPrimitive(nullToPrimitive), toPrimitive(v.coercibleObject), 'object with no hint + null Symbol.toPrimitive ignores it'); 100 st.equal(toPrimitive(nullToPrimitive, Number), toPrimitive(v.coercibleObject, Number), 'object with hint Number + null Symbol.toPrimitive ignores it'); 101 st.equal(toPrimitive(nullToPrimitive, String), toPrimitive(v.coercibleObject, String), 'object with hint String + null Symbol.toPrimitive ignores it'); 118 102 119 103 st.test('exceptions', function (sst) { 104 /** @type {Coercible} */ 120 105 var nonFunctionToPrimitive = { toString: sst.fail, valueOf: sst.fail }; 121 106 nonFunctionToPrimitive[Symbol.toPrimitive] = {}; 122 107 sst['throws'](toPrimitive.bind(null, nonFunctionToPrimitive), TypeError, 'Symbol.toPrimitive returning a non-function throws'); 123 108 109 /** @type {Coercible} */ 124 110 var uncoercibleToPrimitive = { toString: sst.fail, valueOf: sst.fail }; 125 uncoercibleToPrimitive[Symbol.toPrimitive] = function (hint) {111 uncoercibleToPrimitive[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ function (hint) { 126 112 return { toString: function () { return hint; } }; 127 113 }; 128 114 sst['throws'](toPrimitive.bind(null, uncoercibleToPrimitive), TypeError, 'Symbol.toPrimitive returning an object throws'); 129 115 116 /** @type {Coercible} */ 130 117 var throwingToPrimitive = { toString: sst.fail, valueOf: sst.fail }; 131 throwingToPrimitive[Symbol.toPrimitive] = function (hint) { throw new RangeError(hint); }; 118 throwingToPrimitive[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ function (hint) { 119 throw new RangeError(hint); 120 }; 132 121 sst['throws'](toPrimitive.bind(null, throwingToPrimitive), RangeError, 'Symbol.toPrimitive throwing throws'); 133 122 … … 139 128 140 129 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');130 st['throws'](toPrimitive.bind(null, v.uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError'); 131 st['throws'](toPrimitive.bind(null, v.uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError'); 132 st['throws'](toPrimitive.bind(null, v.uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError'); 144 133 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');134 st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError'); 135 st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError'); 136 st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError'); 148 137 st.end(); 149 138 });
Note:
See TracChangeset
for help on using the changeset viewer.