UNPKG

@tdb/util

Version:
153 lines 7.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var chai_1 = require("chai"); var _1 = require("."); describe('value.toNumber', function () { it('returns the non-number value', function () { var NOW = new Date(); var OBJECT = { foo: 123 }; chai_1.expect(_1.value.toNumber('hello')).to.equal('hello'); chai_1.expect(_1.value.toNumber(OBJECT)).to.equal(OBJECT); chai_1.expect(_1.value.toNumber(NOW)).to.equal(NOW); chai_1.expect(_1.value.toNumber(null)).to.equal(null); chai_1.expect(_1.value.toNumber(undefined)).to.equal(undefined); }); it('converts a string to a number', function () { chai_1.expect(_1.value.toNumber('0')).to.equal(0); chai_1.expect(_1.value.toNumber('-1')).to.equal(-1); chai_1.expect(_1.value.toNumber('1')).to.equal(1); chai_1.expect(_1.value.toNumber('12.34')).to.equal(12.34); }); it('does not convert a number/unit string toa number', function () { chai_1.expect(_1.value.toNumber('10px')).to.equal('10px'); }); }); describe('value.toBool', function () { describe('existing Boolean value', function () { it('true (no change)', function () { chai_1.expect(_1.value.toBool(true)).to.equal(true); }); it('false (no change)', function () { chai_1.expect(_1.value.toBool(false)).to.equal(false); }); }); describe('string to Boolean', function () { it('converts `true` (string) to true', function () { chai_1.expect(_1.value.toBool('true')).to.equal(true); chai_1.expect(_1.value.toBool('True')).to.equal(true); chai_1.expect(_1.value.toBool(' truE ')).to.equal(true); }); it('converts `false` (string) to false', function () { chai_1.expect(_1.value.toBool('false')).to.equal(false); chai_1.expect(_1.value.toBool('False')).to.equal(false); chai_1.expect(_1.value.toBool(' falSe ')).to.equal(false); }); }); it('does not convert other value types', function () { chai_1.expect(_1.value.toBool(undefined)).to.equal(undefined); chai_1.expect(_1.value.toBool(null)).to.equal(undefined); chai_1.expect(_1.value.toBool('Foo')).to.equal(undefined); chai_1.expect(_1.value.toBool('')).to.equal(undefined); chai_1.expect(_1.value.toBool(' ')).to.equal(undefined); chai_1.expect(_1.value.toBool(123)).to.equal(undefined); chai_1.expect(_1.value.toBool({ foo: 123 })).to.eql(undefined); }); it('returns the given default value', function () { chai_1.expect(_1.value.toBool(undefined, true)).to.equal(true); chai_1.expect(_1.value.toBool(undefined, false)).to.equal(false); chai_1.expect(_1.value.toBool(undefined, 123)).to.equal(123); chai_1.expect(_1.value.toBool(null, true)).to.equal(true); chai_1.expect(_1.value.toBool(null, false)).to.equal(false); chai_1.expect(_1.value.toBool(null, 123)).to.equal(123); }); }); describe('toType', function () { it('converts to bool (true)', function () { chai_1.expect(_1.value.toType('true')).to.equal(true); chai_1.expect(_1.value.toType(' true ')).to.equal(true); chai_1.expect(_1.value.toType('True')).to.equal(true); chai_1.expect(_1.value.toType('TRUE')).to.equal(true); }); it('converts to bool (false)', function () { chai_1.expect(_1.value.toType('false')).to.equal(false); chai_1.expect(_1.value.toType(' false ')).to.equal(false); chai_1.expect(_1.value.toType('False')).to.equal(false); chai_1.expect(_1.value.toType('FALSE')).to.equal(false); }); it('converts to number', function () { chai_1.expect(_1.value.toType('123')).to.equal(123); chai_1.expect(_1.value.toType(' -123 ')).to.equal(-123); chai_1.expect(_1.value.toType('0')).to.equal(0); chai_1.expect(_1.value.toType('0.0001')).to.equal(0.0001); }); it('converts does not convert', function () { var now = new Date(); chai_1.expect(_1.value.toType('foo')).to.eql('foo'); chai_1.expect(_1.value.toType(undefined)).to.eql(undefined); chai_1.expect(_1.value.toType(null)).to.eql(null); chai_1.expect(_1.value.toType({})).to.eql({}); chai_1.expect(_1.value.toType(now)).to.eql(now); chai_1.expect(_1.value.toType(123)).to.eql(123); }); }); describe('isPromise', function () { var myThing = function () { return new Promise(function (resolve, reject) { resolve(); }); }; it('is a promise', function () { var promise = myThing(); chai_1.expect(_1.value.isPromise(promise)).to.equal(true); }); it('is not a promise', function () { chai_1.expect(_1.value.isPromise()).to.equal(false); chai_1.expect(_1.value.isPromise(null)).to.equal(false); chai_1.expect(_1.value.isPromise(123)).to.equal(false); chai_1.expect(_1.value.isPromise({ then: 123 })).to.equal(false); }); }); describe('isJson', function () { it('is not JSON', function () { chai_1.expect(_1.value.isJson()).to.eql(false); chai_1.expect(_1.value.isJson(null)).to.eql(false); chai_1.expect(_1.value.isJson(123)).to.eql(false); chai_1.expect(_1.value.isJson(new Date())).to.eql(false); chai_1.expect(_1.value.isJson({})).to.eql(false); }); it('is a string but not JSON', function () { chai_1.expect(_1.value.isJson('')).to.eql(false); chai_1.expect(_1.value.isJson(' ')).to.eql(false); chai_1.expect(_1.value.isJson('hello')).to.eql(false); }); it('is JSON', function () { chai_1.expect(_1.value.isJson('{}')).to.eql(true); chai_1.expect(_1.value.isJson('[]')).to.eql(true); chai_1.expect(_1.value.isJson('{ "foo": 123 }')).to.eql(true); chai_1.expect(_1.value.isJson('[1,2,3]')).to.eql(true); }); it('is JSON (trimmed string)', function () { chai_1.expect(_1.value.isJson(' {} ')).to.eql(true); chai_1.expect(_1.value.isJson(' [] ')).to.eql(true); }); }); describe('isIsoDate', function () { it('does not fail if the value is not a string', function () { chai_1.expect(_1.value.isIsoDate()).to.eql(false); chai_1.expect(_1.value.isIsoDate(123)).to.eql(false); chai_1.expect(_1.value.isIsoDate({})).to.eql(false); chai_1.expect(_1.value.isIsoDate(new Date())).to.eql(false); }); it('is an ISO date', function () { chai_1.expect(_1.value.isIsoDate('2015-02-21T00:52:43.822Z')).to.eql(true); chai_1.expect(_1.value.isIsoDate('2015-02-21T00:52:43Z')).to.eql(true); chai_1.expect(_1.value.isIsoDate('2015-02-21T00:52Z')).to.eql(true); }); it('is not an ISO date', function () { chai_1.expect(_1.value.isIsoDate('2015-02-21T00:52:43.822')).to.eql(false); chai_1.expect(_1.value.isIsoDate('2015-02-21T00:52:43')).to.eql(false); chai_1.expect(_1.value.isIsoDate('2015-02-21T00:52')).to.eql(false); chai_1.expect(_1.value.isIsoDate('2015-02-21T00Z')).to.eql(false); }); }); //# sourceMappingURL=value.to.test.js.map