0.1.2 Added testing and improved options for default behaviors

master
David Kebler 2020-01-14 12:58:40 -08:00
parent f05cf40fd6
commit 36daa6b5b9
6 changed files with 79 additions and 30 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/node_modules/ /node_modules/
/coverage/ /coverage/
*.lock

View File

@ -2,3 +2,4 @@ tests/
test/ test/
*.test.js *.test.js
testing/ testing/
*.lock

View File

@ -1,16 +1,13 @@
{ {
"name": "@uci-utils/to-boolean", "name": "@uci-utils/to-boolean",
"version": "0.1.1", "version": "0.1.2",
"description": "function to return a boolean value from sets of default values", "description": "function to return a boolean value from sets of default values",
"main": "src/boolean.js", "main": "src/boolean.js",
"scripts": { "scripts": {
"example": "node --r esm examples/example", "example": "node -r esm examples/example",
"example:dev": "UCI_ENV=dev ./node_modules/.bin/nodemon -r esm examples/example", "example:dev": "UCI_ENV=dev ./node_modules/.bin/nodemon -r esm examples/example",
"test": "./node_modules/.bin/mocha -r esm --timeout 30000", "test": "./node_modules/.bin/mocha -r esm --timeout 30000 || exit 0",
"testd": "UCI_ENV=dev ./node_modules/.bin/nodemon --exec './node_modules/.bin/mocha -r esm --timeout 30000' || exit 0", "testd": "./node_modules/.bin/nodemon --exec './node_modules/.bin/mocha -r esm --timeout 30000' || exit 0"
"testdd": "UCI_LOG_LEVEL='trace' npm run testd",
"testde": "UCI_LOG_LEVEL='warn' npm run testd",
"testl": "UCI_ENV=pro UCI_LOG_PATH=./test/test.log 0 npm run test || exit 0"
}, },
"author": "David Kebler", "author": "David Kebler",
"license": "MIT", "license": "MIT",

View File

@ -3,14 +3,15 @@ const rFalse=['f','false','n','no','off','negative','down','disabled','nope']
function createBoolean (opts={}) { function createBoolean (opts={}) {
return (value => { return (value => {
if (value===undefined) return opts.undefined if (value===undefined) return opts.undefined==='same' ? undefined : opts.undefined || false
if (value===null) return opts.null if (value===null) return opts.null==='same' ? null : opts.null || false
if (!isNaN(Number(value))) return Number(value) <1 ? false :true if (!isNaN(Number(value))) return (opts.number==='default' ? !!value : (Number(value) > (opts.number || 0)? true : false))
if (typeof value==='string') { if (typeof value==='string') {
value = value.trim() value = value.trim()
value = value.toLowerCase() value = value.toLowerCase()
if ((opts.rTrue || rTrue).includes(value)) return true if ((opts.rTrue || rTrue).includes(value)) return true
if ((opts.rFalse || rFalse).includes(value)) return false if ((opts.rFalse || rFalse).includes(value)) return false
return !!opts.string || false
} }
return !!value return !!value
}) })

69
test/boolean.test.js Normal file
View File

@ -0,0 +1,69 @@
import { expect } from 'chai'
import {toBoolean, createBoolean, rTrue,rFalse }from '../src/boolean'
describe('No options default function check - ', function () {
it('should return false for undefined', function () {
expect(toBoolean()).to.equal(false)
})
it('should return false for null', function () {
expect(toBoolean(null)).to.equal(false)
})
console.log('falsy', rFalse)
it('should return false for all falsy', function () {
expect(rFalse.reduce((acc,value)=>acc && toBoolean(value),false)).to.equal(false)
})
console.log('truthy', rTrue)
it('should return true for all truthy', function () {
expect(rTrue.reduce((acc,value)=>acc && toBoolean(value),true)).to.equal(true)
})
it('should return false for random word not in either list', function () {
expect(toBoolean('bogus')).to.equal(false)
})
it('should return false for number <=0', function () {
expect(toBoolean(-1)).to.equal(false)
})
it('should return true for number >0', function () {
expect(toBoolean(3)).to.equal(true)
})
})
let toBoolean2 = createBoolean({undefined:'same', null:'same',string:true,number:5})
describe('created Boolean function with these options \'{undefined:\'same\', null:\'same\',string:true,number:5}', function () {
it('should return undefined for undefined', function () {
expect(toBoolean2()).to.equal(undefined)
})
it('should return null for null', function () {
expect(toBoolean2(null)).to.equal(null)
})
it('should return true for random word', function () {
expect(toBoolean2('bogus')).to.equal(true)
})
it('but should return false for empty sting \'\' or \' \'', function () {
expect(!!(toBoolean2('') ^ toBoolean2(' '))).to.equal(false)
})
it('but should still return false for all falsy words', function () {
expect(rFalse.reduce((acc,value)=>acc && toBoolean(value),false)).to.equal(false)
})
it('should return false for number <=5', function () {
expect(toBoolean2(4)).to.equal(false)
})
})
let toBoolean3 = createBoolean({undefined:true, null:true,number:'default'})
describe('created boolean with {undefined:true, null:true, number:\'default\'}', function () {
it('should return true for undefined', function () {
expect(toBoolean3()).to.equal(true)
})
it('should return true for null', function () {
expect(toBoolean3(null)).to.equal(true)
})
it('should return false number =0', function () {
expect(toBoolean3(0)).to.equal(false)
})
it('and should return true for any other number like default casting', function () {
expect(toBoolean3(-1) && toBoolean3(1)).to.equal(true)
})
})

View File

@ -1,20 +0,0 @@
import { expect } from 'chai'
import u from '../src/type'
describe('Variable Types Library - ', function () {
it('Should include custom types', function () {
expect(u.isBuffer(Buffer.from('this is a test'))).to.equal(true)
expect(u.getType(Buffer.from('this is a test'))).to.equal('buffer')
expect(u.isPromise(Promise.resolve(2))).to.equal(true)
expect(u.getType(Promise.resolve(2))).to.equal('promise')
})
it('Should load typechecker', function () {
expect(u.isPlainObject([1])).to.equal(false)
expect(u.getType(true)).to.equal('boolean')
expect(u.getObjectType('a string')).to.equal('[object String]')
expect(u.getType('this is a test')).to.equal('string')
})
})