2017-04-20 19:17:05 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
const cast = require('../../src/util/cast');
|
2016-10-03 16:34:57 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('toNumber', t => {
|
2016-10-03 16:34:57 -04:00
|
|
|
// Numeric
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.toNumber(0), 0);
|
|
|
|
t.strictEqual(cast.toNumber(1), 1);
|
|
|
|
t.strictEqual(cast.toNumber(3.14), 3.14);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// String
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.toNumber('0'), 0);
|
|
|
|
t.strictEqual(cast.toNumber('1'), 1);
|
|
|
|
t.strictEqual(cast.toNumber('3.14'), 3.14);
|
|
|
|
t.strictEqual(cast.toNumber('0.1e10'), 1000000000);
|
|
|
|
t.strictEqual(cast.toNumber('foobar'), 0);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Boolean
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.toNumber(true), 1);
|
|
|
|
t.strictEqual(cast.toNumber(false), 0);
|
|
|
|
t.strictEqual(cast.toNumber('true'), 0);
|
|
|
|
t.strictEqual(cast.toNumber('false'), 0);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Undefined & object
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.toNumber(undefined), 0);
|
|
|
|
t.strictEqual(cast.toNumber({}), 0);
|
|
|
|
t.strictEqual(cast.toNumber(NaN), 0);
|
2016-10-03 16:34:57 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('toBoolean', t => {
|
2016-10-03 16:34:57 -04:00
|
|
|
// Numeric
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.toBoolean(0), false);
|
|
|
|
t.strictEqual(cast.toBoolean(1), true);
|
|
|
|
t.strictEqual(cast.toBoolean(3.14), true);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// String
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.toBoolean('0'), false);
|
|
|
|
t.strictEqual(cast.toBoolean('1'), true);
|
|
|
|
t.strictEqual(cast.toBoolean('3.14'), true);
|
|
|
|
t.strictEqual(cast.toBoolean('0.1e10'), true);
|
|
|
|
t.strictEqual(cast.toBoolean('foobar'), true);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Boolean
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.toBoolean(true), true);
|
|
|
|
t.strictEqual(cast.toBoolean(false), false);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Undefined & object
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.toBoolean(undefined), false);
|
|
|
|
t.strictEqual(cast.toBoolean({}), true);
|
2016-10-03 16:34:57 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('toString', t => {
|
2016-10-03 16:34:57 -04:00
|
|
|
// Numeric
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.toString(0), '0');
|
|
|
|
t.strictEqual(cast.toString(1), '1');
|
|
|
|
t.strictEqual(cast.toString(3.14), '3.14');
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// String
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.toString('0'), '0');
|
|
|
|
t.strictEqual(cast.toString('1'), '1');
|
|
|
|
t.strictEqual(cast.toString('3.14'), '3.14');
|
|
|
|
t.strictEqual(cast.toString('0.1e10'), '0.1e10');
|
|
|
|
t.strictEqual(cast.toString('foobar'), 'foobar');
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Boolean
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.toString(true), 'true');
|
|
|
|
t.strictEqual(cast.toString(false), 'false');
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Undefined & object
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.toString(undefined), 'undefined');
|
|
|
|
t.strictEqual(cast.toString({}), '[object Object]');
|
2016-10-03 16:34:57 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('toRbgColorList', t => {
|
2016-10-03 16:34:57 -04:00
|
|
|
// Hex (minimal, see "color" util tests)
|
2016-10-24 10:57:52 -04:00
|
|
|
t.deepEqual(cast.toRgbColorList('#000'), [0, 0, 0]);
|
|
|
|
t.deepEqual(cast.toRgbColorList('#000000'), [0, 0, 0]);
|
|
|
|
t.deepEqual(cast.toRgbColorList('#fff'), [255, 255, 255]);
|
|
|
|
t.deepEqual(cast.toRgbColorList('#ffffff'), [255, 255, 255]);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Decimal (minimal, see "color" util tests)
|
2016-10-24 10:57:52 -04:00
|
|
|
t.deepEqual(cast.toRgbColorList(0), [0, 0, 0]);
|
|
|
|
t.deepEqual(cast.toRgbColorList(1), [0, 0, 1]);
|
|
|
|
t.deepEqual(cast.toRgbColorList(16777215), [255, 255, 255]);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Malformed
|
2016-10-24 10:57:52 -04:00
|
|
|
t.deepEqual(cast.toRgbColorList('ffffff'), [0, 0, 0]);
|
|
|
|
t.deepEqual(cast.toRgbColorList('foobar'), [0, 0, 0]);
|
2016-10-03 16:34:57 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('toRbgColorObject', t => {
|
2017-01-19 18:19:06 -05:00
|
|
|
// Hex (minimal, see "color" util tests)
|
|
|
|
t.deepEqual(cast.toRgbColorObject('#000'), {r: 0, g: 0, b: 0});
|
|
|
|
t.deepEqual(cast.toRgbColorObject('#000000'), {r: 0, g: 0, b: 0});
|
|
|
|
t.deepEqual(cast.toRgbColorObject('#fff'), {r: 255, g: 255, b: 255});
|
|
|
|
t.deepEqual(cast.toRgbColorObject('#ffffff'), {r: 255, g: 255, b: 255});
|
|
|
|
|
|
|
|
// Decimal (minimal, see "color" util tests)
|
2017-01-27 12:05:17 -05:00
|
|
|
t.deepEqual(cast.toRgbColorObject(0), {a: 255, r: 0, g: 0, b: 0});
|
|
|
|
t.deepEqual(cast.toRgbColorObject(1), {a: 255, r: 0, g: 0, b: 1});
|
|
|
|
t.deepEqual(cast.toRgbColorObject(16777215), {a: 255, r: 255, g: 255, b: 255});
|
|
|
|
t.deepEqual(cast.toRgbColorObject('0x80010203'), {a: 128, r: 1, g: 2, b: 3});
|
2017-01-19 18:19:06 -05:00
|
|
|
|
|
|
|
// Malformed
|
2017-01-27 12:05:17 -05:00
|
|
|
t.deepEqual(cast.toRgbColorObject('ffffff'), {a: 255, r: 0, g: 0, b: 0});
|
|
|
|
t.deepEqual(cast.toRgbColorObject('foobar'), {a: 255, r: 0, g: 0, b: 0});
|
2017-01-19 18:19:06 -05:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('compare', t => {
|
2016-10-03 16:34:57 -04:00
|
|
|
// Numeric
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.compare(0, 0), 0);
|
|
|
|
t.strictEqual(cast.compare(1, 0), 1);
|
|
|
|
t.strictEqual(cast.compare(0, 1), -1);
|
|
|
|
t.strictEqual(cast.compare(1, 1), 0);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// String
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.compare('0', '0'), 0);
|
|
|
|
t.strictEqual(cast.compare('0.1e10', '1000000000'), 0);
|
|
|
|
t.strictEqual(cast.compare('foobar', 'FOOBAR'), 0);
|
|
|
|
t.ok(cast.compare('dog', 'cat') > 0);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Boolean
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.compare(true, true), 0);
|
|
|
|
t.strictEqual(cast.compare(true, false), 1);
|
|
|
|
t.strictEqual(cast.compare(false, true), -1);
|
|
|
|
t.strictEqual(cast.compare(true, true), 0);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Undefined & object
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.compare(undefined, undefined), 0);
|
|
|
|
t.strictEqual(cast.compare(undefined, 'undefined'), 0);
|
|
|
|
t.strictEqual(cast.compare({}, {}), 0);
|
|
|
|
t.strictEqual(cast.compare({}, '[object Object]'), 0);
|
2016-10-03 16:34:57 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('isInt', t => {
|
2016-10-03 16:34:57 -04:00
|
|
|
// Numeric
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.isInt(0), true);
|
|
|
|
t.strictEqual(cast.isInt(1), true);
|
|
|
|
t.strictEqual(cast.isInt(0.0), true);
|
|
|
|
t.strictEqual(cast.isInt(3.14), false);
|
|
|
|
t.strictEqual(cast.isInt(NaN), true);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// String
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.isInt('0'), true);
|
|
|
|
t.strictEqual(cast.isInt('1'), true);
|
|
|
|
t.strictEqual(cast.isInt('0.0'), false);
|
|
|
|
t.strictEqual(cast.isInt('0.1e10'), false);
|
|
|
|
t.strictEqual(cast.isInt('3.14'), false);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Boolean
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.isInt(true), true);
|
|
|
|
t.strictEqual(cast.isInt(false), true);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Undefined & object
|
2016-10-03 18:23:43 -04:00
|
|
|
t.strictEqual(cast.isInt(undefined), false);
|
|
|
|
t.strictEqual(cast.isInt({}), false);
|
2016-10-03 16:34:57 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('toListIndex', t => {
|
|
|
|
const list = [0, 1, 2, 3, 4, 5];
|
|
|
|
const empty = [];
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Valid
|
2019-06-01 01:49:46 -04:00
|
|
|
t.strictEqual(cast.toListIndex(1, list.length, false), 1);
|
|
|
|
t.strictEqual(cast.toListIndex(6, list.length, false), 6);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// Invalid
|
2019-06-01 01:49:46 -04:00
|
|
|
t.strictEqual(cast.toListIndex(-1, list.length, false), cast.LIST_INVALID);
|
|
|
|
t.strictEqual(cast.toListIndex(0.1, list.length, false), cast.LIST_INVALID);
|
|
|
|
t.strictEqual(cast.toListIndex(0, list.length, false), cast.LIST_INVALID);
|
|
|
|
t.strictEqual(cast.toListIndex(7, list.length, false), cast.LIST_INVALID);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// "all"
|
2019-06-01 01:49:46 -04:00
|
|
|
t.strictEqual(cast.toListIndex('all', list.length, true), cast.LIST_ALL);
|
|
|
|
t.strictEqual(cast.toListIndex('all', list.length, false), cast.LIST_INVALID);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// "last"
|
2019-06-01 01:49:46 -04:00
|
|
|
t.strictEqual(cast.toListIndex('last', list.length, false), list.length);
|
|
|
|
t.strictEqual(cast.toListIndex('last', empty.length, false), cast.LIST_INVALID);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// "random"
|
2019-06-01 01:49:46 -04:00
|
|
|
const random = cast.toListIndex('random', list.length, false);
|
2016-10-03 16:34:57 -04:00
|
|
|
t.ok(random <= list.length);
|
|
|
|
t.ok(random > 0);
|
2019-06-01 01:49:46 -04:00
|
|
|
t.strictEqual(cast.toListIndex('random', empty.length, false), cast.LIST_INVALID);
|
2016-10-03 16:34:57 -04:00
|
|
|
|
|
|
|
// "any" (alias for "random")
|
2019-06-01 01:49:46 -04:00
|
|
|
const any = cast.toListIndex('any', list.length, false);
|
2016-10-03 16:34:57 -04:00
|
|
|
t.ok(any <= list.length);
|
|
|
|
t.ok(any > 0);
|
2019-06-01 01:49:46 -04:00
|
|
|
t.strictEqual(cast.toListIndex('any', empty.length, false), cast.LIST_INVALID);
|
2016-10-03 16:34:57 -04:00
|
|
|
t.end();
|
|
|
|
});
|