uci-utils-type/test/cast.test.js

127 lines
3.6 KiB
JavaScript

import assert from 'assert'
import { check, typecast, cast, toString, toBoolean, toPromise } from '../src/index.js'
import { of } from 'rxjs'
describe('symbol', function () {
it('should support isSymbol', function () {
assert(check.isSymbol(Symbol('test')))
})
})
describe('.string()', function () {
it('should return a string', function () {
assert(typecast.string(2) === '2')
assert(check.isString(typecast.string(2)))
})
it('should return an empty string when given a null-ish value', function () {
assert(typecast.string(null) === '')
assert(typecast.string(undefined) === '')
})
it('should support toString', function () {
assert(toString(2) === '2')
assert(check.isString(toString(2)))
})
it('convert an observable to promise with toPromise', function () {
assert.throws(() => { toPromise(() => { }) }, Error)
assert(check.isPromise(toPromise(of(1, 2, 3))))
})
})
describe('.number()', function () {
it('should return a number', function () {
assert(typecast.number('123') === 123)
})
it('should return 0 if typecasting fails', function () {
assert(typecast.number('abc') === 0)
})
it('should return an 0 when given a null-ish value', function () {
assert(typecast.number(null) === 0)
assert(typecast.number(undefined) === 0)
})
})
const adate = [1995, 11, 17, 3, 24, 0]
const sdate = 'December 17, 1995 03:24:00'
describe('.date()', function () {
it('should return a date from a string', function () {
assert(typecast.date(sdate).getTime() === 819199440000)
})
it('should return a date from an array of values', function () {
assert(typecast.date(...adate).getTime() === 819199440000)
})
it('should return default date if typecasting fails', function () {
assert(typecast.date('abc') instanceof Date)
assert(typecast.date('abc').valueOf() == 0)
})
it('should return default date when given a null-ish value', function () {
assert(typecast.date(null) instanceof Date)
assert(typecast.date(null).valueOf() == 0)
assert(typecast.date(undefined) instanceof Date)
assert(typecast.date(undefined).valueOf() == 0)
})
})
describe('.array()', function () {
it('should return an array', function () {
var arr = [1, 2, 3]
assert(typecast.array(arr) === arr)
assert(typecast.array(1) instanceof Array)
assert(typecast.array('a, b, c').length == 3)
assert(typecast.array('a, b, c')[1] === 'b')
})
it('should preserve non-string objects', function () {
var now = new Date()
assert(Array.isArray(typecast.array(now)))
assert(typecast.array(now).length == 1)
assert(typecast.array(now)[0] === now)
})
it('should return an empty array when given a null-ish value', function () {
assert(Array.isArray(typecast.array(null)))
assert(typecast.array(null).length == 0)
assert(Array.isArray(typecast.array(undefined)))
assert(typecast.array(undefined).length == 0)
})
})
describe('.boolean()', function () {
it('should be using UCI to-boolean', function () {
assert(typecast.boolean('true') === true)
assert(typecast.boolean('nope') === false)
assert(typecast.boolean('sure') === true)
assert(typecast.boolean('bogus') === false)
assert(typecast.boolean() === false) // undefined is false
assert(typecast.boolean(-1) === false)
assert(typecast.boolean(4) === true)
})
})
describe('generic cast function', function () {
it('should work', function () {
assert(cast(123, 'string') === '123')
})
it('should throw when given an invalid type', function () {
var err
try {
typecast(1, 'invalid')
} catch (e) {
err = e
}
assert(err)
})
})