Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50660 views
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 debug = require('util').inspect;
8
9
var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol';
10
var hasSymbolToPrimitive = hasSymbols && typeof Symbol.toPrimitive === 'symbol';
11
12
var primitives = [null, undefined, true, false, 0, -0, 42, NaN, Infinity, -Infinity, '', 'abc'];
13
14
test('primitives', function (t) {
15
forEach(primitives, function (i) {
16
t.ok(is(toPrimitive(i), i), 'toPrimitive(' + debug(i) + ') returns the same value');
17
t.ok(is(toPrimitive(i, String), i), 'toPrimitive(' + debug(i) + ', String) returns the same value');
18
t.ok(is(toPrimitive(i, Number), i), 'toPrimitive(' + debug(i) + ', Number) returns the same value');
19
});
20
t.end();
21
});
22
23
test('Symbols', { skip: !hasSymbols }, function (t) {
24
var symbols = [Symbol(), Symbol.iterator, Symbol.for('foo')];
25
forEach(symbols, function (sym) {
26
t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value');
27
t.equal(toPrimitive(sym, String), sym, 'toPrimitive(' + debug(sym) + ', String) returns the same value');
28
t.equal(toPrimitive(sym, Number), sym, 'toPrimitive(' + debug(sym) + ', Number) returns the same value');
29
});
30
31
var primitiveSym = Symbol('primitiveSym');
32
var objectSym = Object(primitiveSym);
33
t.equal(toPrimitive(objectSym), primitiveSym, 'toPrimitive(' + debug(objectSym) + ') returns ' + debug(primitiveSym));
34
t.equal(toPrimitive(objectSym, String), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', String) returns ' + debug(primitiveSym));
35
t.equal(toPrimitive(objectSym, Number), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', Number) returns ' + debug(primitiveSym));
36
t.end();
37
});
38
39
test('Arrays', function (t) {
40
var arrays = [[], ['a', 'b'], [1, 2]];
41
forEach(arrays, function (arr) {
42
t.equal(toPrimitive(arr), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array');
43
t.equal(toPrimitive(arr, String), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array');
44
t.equal(toPrimitive(arr, Number), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array');
45
});
46
t.end();
47
});
48
49
test('Dates', function (t) {
50
var dates = [new Date(), new Date(0), new Date(NaN)];
51
forEach(dates, function (date) {
52
t.equal(toPrimitive(date), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date');
53
t.equal(toPrimitive(date, String), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date');
54
t.ok(is(toPrimitive(date, Number), Number(date)), 'toPrimitive(' + debug(date) + ') returns the number version of the date');
55
});
56
t.end();
57
});
58
59
var coercibleObject = { valueOf: function () { return 3; }, toString: function () { return 42; } };
60
var valueOfOnlyObject = { valueOf: function () { return 4; }, toString: function () { return {}; } };
61
var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } };
62
var coercibleFnObject = { valueOf: function () { return function valueOfFn() {}; }, toString: function () { return 42; } };
63
var uncoercibleObject = { valueOf: function () { return {}; }, toString: function () { return {}; } };
64
var uncoercibleFnObject = { valueOf: function () { return function valueOfFn() {}; }, toString: function () { return function toStrFn() {}; } };
65
66
test('Objects', function (t) {
67
t.equal(toPrimitive(coercibleObject), coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf');
68
t.equal(toPrimitive(coercibleObject, Number), coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf');
69
t.equal(toPrimitive(coercibleObject, String), coercibleObject.toString(), 'coercibleObject with hint String coerces to non-stringified toString');
70
71
t.equal(toPrimitive(coercibleFnObject), coercibleFnObject.toString(), 'coercibleFnObject coerces to non-stringified toString');
72
t.equal(toPrimitive(coercibleFnObject, Number), coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to non-stringified toString');
73
t.equal(toPrimitive(coercibleFnObject, String), coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to non-stringified toString');
74
75
t.equal(toPrimitive({}), '[object Object]', '{} with no hint coerces to Object#toString');
76
t.equal(toPrimitive({}, Number), '[object Object]', '{} with hint Number coerces to Object#toString');
77
t.equal(toPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString');
78
79
t.equal(toPrimitive(toStringOnlyObject), toStringOnlyObject.toString(), 'toStringOnlyObject returns non-stringified toString');
80
t.equal(toPrimitive(toStringOnlyObject, Number), toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns non-stringified toString');
81
t.equal(toPrimitive(toStringOnlyObject, String), toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns non-stringified toString');
82
83
t.equal(toPrimitive(valueOfOnlyObject), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');
84
t.equal(toPrimitive(valueOfOnlyObject, Number), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf');
85
t.equal(toPrimitive(valueOfOnlyObject, String), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns non-stringified valueOf');
86
87
t.test('Symbol.toPrimitive', { skip: !hasSymbolToPrimitive }, function (st) {
88
var overriddenObject = { toString: st.fail, valueOf: st.fail };
89
overriddenObject[Symbol.toPrimitive] = function (hint) { return String(hint); };
90
91
st.equal(toPrimitive(overriddenObject), 'default', 'object with Symbol.toPrimitive + no hint invokes that');
92
st.equal(toPrimitive(overriddenObject, Number), 'number', 'object with Symbol.toPrimitive + hint Number invokes that');
93
st.equal(toPrimitive(overriddenObject, String), 'string', 'object with Symbol.toPrimitive + hint String invokes that');
94
95
var nullToPrimitive = { toString: coercibleObject.toString, valueOf: coercibleObject.valueOf };
96
nullToPrimitive[Symbol.toPrimitive] = null;
97
st.equal(toPrimitive(nullToPrimitive), toPrimitive(coercibleObject), 'object with no hint + null Symbol.toPrimitive ignores it');
98
st.equal(toPrimitive(nullToPrimitive, Number), toPrimitive(coercibleObject, Number), 'object with hint Number + null Symbol.toPrimitive ignores it');
99
st.equal(toPrimitive(nullToPrimitive, String), toPrimitive(coercibleObject, String), 'object with hint String + null Symbol.toPrimitive ignores it');
100
101
st.test('exceptions', function (sst) {
102
var nonFunctionToPrimitive = { toString: sst.fail, valueOf: sst.fail };
103
nonFunctionToPrimitive[Symbol.toPrimitive] = {};
104
sst['throws'](toPrimitive.bind(null, nonFunctionToPrimitive), TypeError, 'Symbol.toPrimitive returning a non-function throws');
105
106
var uncoercibleToPrimitive = { toString: sst.fail, valueOf: sst.fail };
107
uncoercibleToPrimitive[Symbol.toPrimitive] = function (hint) { return { toString: function () { return hint; } }; };
108
sst['throws'](toPrimitive.bind(null, uncoercibleToPrimitive), TypeError, 'Symbol.toPrimitive returning an object throws');
109
110
var throwingToPrimitive = { toString: sst.fail, valueOf: sst.fail };
111
throwingToPrimitive[Symbol.toPrimitive] = function (hint) { throw new RangeError(hint); };
112
sst['throws'](toPrimitive.bind(null, throwingToPrimitive), RangeError, 'Symbol.toPrimitive throwing throws');
113
114
sst.end();
115
});
116
117
st.end();
118
});
119
120
t.test('exceptions', function (st) {
121
st['throws'](toPrimitive.bind(null, uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError');
122
st['throws'](toPrimitive.bind(null, uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError');
123
st['throws'](toPrimitive.bind(null, uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError');
124
125
st['throws'](toPrimitive.bind(null, uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError');
126
st['throws'](toPrimitive.bind(null, uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError');
127
st['throws'](toPrimitive.bind(null, uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError');
128
st.end();
129
});
130
t.end();
131
});
132
133