- Timestamp:
- 12/12/24 17:06:06 (6 weeks ago)
- Branches:
- main
- Children:
- 79a0317
- Parents:
- d565449
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified imaps-frontend/node_modules/es-to-primitive/test/es5.js ¶
rd565449 r0c6b92a 4 4 var toPrimitive = require('../es5'); 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 hasSymbols = require('has-symbols')();9 var v = require('es-value-fixtures'); 10 10 11 11 test('function properties', function (t) { … … 16 16 }); 17 17 18 var primitives = [null, undefined, true, false, 0, -0, 42, NaN, Infinity, -Infinity, '', 'abc'];19 20 18 test('primitives', function (t) { 21 forEach( primitives, function (i) {19 forEach(v.primitives, function (i) { 22 20 t.ok(is(toPrimitive(i), i), 'toPrimitive(' + debug(i) + ') returns the same value'); 23 21 t.ok(is(toPrimitive(i, String), i), 'toPrimitive(' + debug(i) + ', String) returns the same value'); … … 27 25 }); 28 26 29 test('Symbols', { skip: !hasSymbols }, function (t) { 30 var symbols = [ 31 Symbol('foo'), 32 Symbol.iterator, 33 Symbol['for']('foo') // eslint-disable-line no-restricted-properties 34 ]; 35 forEach(symbols, function (sym) { 27 test('Symbols', { skip: !v.hasSymbols }, function (t) { 28 forEach(v.symbols, function (sym) { 36 29 t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value'); 37 30 t.equal(toPrimitive(sym, String), sym, 'toPrimitive(' + debug(sym) + ', String) returns the same value'); … … 70 63 }); 71 64 72 var coercibleObject = { valueOf: function () { return 3; }, toString: function () { return 42; } }; 73 var valueOfOnlyObject = { valueOf: function () { return 4; }, toString: function () { return {}; } }; 74 var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } }; 75 var coercibleFnObject = { 76 valueOf: function () { return function valueOfFn() {}; }, 77 toString: function () { return 42; } 78 }; 79 var uncoercibleObject = { valueOf: function () { return {}; }, toString: function () { return {}; } }; 80 var uncoercibleFnObject = { 81 valueOf: function () { return function valueOfFn() {}; }, 82 toString: function () { return function toStrFn() {}; } 83 }; 65 test('Objects', function (t) { 66 t.equal(toPrimitive(v.coercibleObject), v.coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf'); 67 t.equal(toPrimitive(v.coercibleObject, String), v.coercibleObject.toString(), 'coercibleObject with hint String coerces to toString'); 68 t.equal(toPrimitive(v.coercibleObject, Number), v.coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf'); 84 69 85 test('Objects', function (t) { 86 t.equal(toPrimitive(coercibleObject), coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf'); 87 t.equal(toPrimitive(coercibleObject, String), coercibleObject.toString(), 'coercibleObject with hint String coerces to toString'); 88 t.equal(toPrimitive(coercibleObject, Number), coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf'); 89 90 t.equal(toPrimitive(coercibleFnObject), coercibleFnObject.toString(), 'coercibleFnObject coerces to toString'); 91 t.equal(toPrimitive(coercibleFnObject, String), coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to toString'); 92 t.equal(toPrimitive(coercibleFnObject, Number), coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to toString'); 70 t.equal(toPrimitive(v.coercibleFnObject), v.coercibleFnObject.toString(), 'coercibleFnObject coerces to toString'); 71 t.equal(toPrimitive(v.coercibleFnObject, String), v.coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to toString'); 72 t.equal(toPrimitive(v.coercibleFnObject, Number), v.coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to toString'); 93 73 94 74 t.ok(is(toPrimitive({}), '[object Object]'), '{} with no hint coerces to Object#toString'); … … 96 76 t.ok(is(toPrimitive({}, Number), '[object Object]'), '{} with hint Number coerces to Object#toString'); 97 77 98 t.equal(toPrimitive( toStringOnlyObject),toStringOnlyObject.toString(), 'toStringOnlyObject returns toString');99 t.equal(toPrimitive( toStringOnlyObject, String),toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns toString');100 t.equal(toPrimitive( toStringOnlyObject, Number),toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns toString');78 t.equal(toPrimitive(v.toStringOnlyObject), v.toStringOnlyObject.toString(), 'toStringOnlyObject returns toString'); 79 t.equal(toPrimitive(v.toStringOnlyObject, String), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns toString'); 80 t.equal(toPrimitive(v.toStringOnlyObject, Number), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns toString'); 101 81 102 t.equal(toPrimitive(v alueOfOnlyObject),valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');103 t.equal(toPrimitive(v alueOfOnlyObject, String),valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns valueOf');104 t.equal(toPrimitive(v alueOfOnlyObject, Number),valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf');82 t.equal(toPrimitive(v.valueOfOnlyObject), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf'); 83 t.equal(toPrimitive(v.valueOfOnlyObject, String), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns valueOf'); 84 t.equal(toPrimitive(v.valueOfOnlyObject, Number), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf'); 105 85 106 86 t.test('exceptions', function (st) { 107 st['throws'](toPrimitive.bind(null, uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError');108 st['throws'](toPrimitive.bind(null, uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError');109 st['throws'](toPrimitive.bind(null, uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError');87 st['throws'](toPrimitive.bind(null, v.uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError'); 88 st['throws'](toPrimitive.bind(null, v.uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError'); 89 st['throws'](toPrimitive.bind(null, v.uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError'); 110 90 111 st['throws'](toPrimitive.bind(null, uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError');112 st['throws'](toPrimitive.bind(null, uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError');113 st['throws'](toPrimitive.bind(null, uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError');91 st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError'); 92 st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError'); 93 st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError'); 114 94 st.end(); 115 95 });
Note:
See TracChangeset
for help on using the changeset viewer.