| 1 |
'use strict'; |
| 2 |
|
| 3 |
var test = require('tape'); |
| 4 |
var inspect = require('object-inspect'); |
| 5 |
var whichBoxedPrimitive = require('..'); |
| 6 |
|
| 7 |
var debug = function (v, m) { return inspect(v) + ' ' + m; }; |
| 8 |
|
| 9 |
var forEach = function (arr, func) { |
| 10 |
var i; |
| 11 |
for (i = 0; i < arr.length; ++i) { |
| 12 |
func(arr[i], i, arr); |
| 13 |
} |
| 14 |
}; |
| 15 |
|
| 16 |
var hasSymbols = require('has-symbols')(); |
| 17 |
var hasBigInts = typeof BigInt === 'function'; |
| 18 |
|
| 19 |
var primitives = [ |
| 20 |
true, |
| 21 |
false, |
| 22 |
42, |
| 23 |
NaN, |
| 24 |
Infinity, |
| 25 |
'', |
| 26 |
'foo' |
| 27 |
].concat( |
| 28 |
hasSymbols ? [Symbol(), Symbol.iterator] : [], |
| 29 |
hasBigInts ? BigInt(42) : [] |
| 30 |
); |
| 31 |
|
| 32 |
var objects = [ |
| 33 |
/a/g, |
| 34 |
new Date(), |
| 35 |
function () {}, |
| 36 |
[], |
| 37 |
{} |
| 38 |
]; |
| 39 |
|
| 40 |
test('isBoxedPrimitive', function (t) { |
| 41 |
t.test('unboxed primitives', function (st) { |
| 42 |
forEach([null, undefined].concat(primitives), function (primitive) { |
| 43 |
st.equal(null, whichBoxedPrimitive(primitive), debug(primitive, 'is a primitive, but not a boxed primitive')); |
| 44 |
}); |
| 45 |
st.end(); |
| 46 |
}); |
| 47 |
|
| 48 |
t.test('boxed primitives', function (st) { |
| 49 |
forEach(primitives, function (primitive) { |
| 50 |
var boxed = Object(primitive); |
| 51 |
var expected = boxed.constructor.name; |
| 52 |
st.equal(typeof expected, 'string', 'expected is string'); |
| 53 |
st.equal(whichBoxedPrimitive(boxed), expected, debug(boxed, 'is a boxed primitive: ' + expected)); |
| 54 |
}); |
| 55 |
st.end(); |
| 56 |
}); |
| 57 |
|
| 58 |
t.test('non-primitive objects', function (st) { |
| 59 |
forEach(objects, function (object) { |
| 60 |
st.equal(undefined, whichBoxedPrimitive(object), debug(object, 'is not a primitive, boxed or otherwise')); |
| 61 |
}); |
| 62 |
st.end(); |
| 63 |
}); |
| 64 |
|
| 65 |
t.end(); |
| 66 |
}); |