From 36daa6b5b928a0b7d961ab2aca80048ef63c944a Mon Sep 17 00:00:00 2001 From: David Kebler Date: Tue, 14 Jan 2020 12:58:40 -0800 Subject: [PATCH] 0.1.2 Added testing and improved options for default behaviors --- .gitignore | 1 + .npmignore | 1 + package.json | 11 +++---- src/boolean.js | 7 +++-- test/boolean.test.js | 69 ++++++++++++++++++++++++++++++++++++++++++++ test/type.test.js | 20 ------------- 6 files changed, 79 insertions(+), 30 deletions(-) create mode 100644 test/boolean.test.js delete mode 100644 test/type.test.js diff --git a/.gitignore b/.gitignore index e61051f..faad3eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules/ /coverage/ +*.lock diff --git a/.npmignore b/.npmignore index f16fc41..3724d35 100644 --- a/.npmignore +++ b/.npmignore @@ -2,3 +2,4 @@ tests/ test/ *.test.js testing/ +*.lock diff --git a/package.json b/package.json index e5cbc2e..11decd3 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,13 @@ { "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", "main": "src/boolean.js", "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", - "test": "./node_modules/.bin/mocha -r esm --timeout 30000", - "testd": "UCI_ENV=dev ./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" + "test": "./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" }, "author": "David Kebler", "license": "MIT", diff --git a/src/boolean.js b/src/boolean.js index 2582733..39d4462 100644 --- a/src/boolean.js +++ b/src/boolean.js @@ -3,14 +3,15 @@ const rFalse=['f','false','n','no','off','negative','down','disabled','nope'] function createBoolean (opts={}) { return (value => { - if (value===undefined) return opts.undefined - if (value===null) return opts.null - if (!isNaN(Number(value))) return Number(value) <1 ? false :true + if (value===undefined) return opts.undefined==='same' ? undefined : opts.undefined || false + if (value===null) return opts.null==='same' ? null : opts.null || false + if (!isNaN(Number(value))) return (opts.number==='default' ? !!value : (Number(value) > (opts.number || 0)? true : false)) if (typeof value==='string') { value = value.trim() value = value.toLowerCase() if ((opts.rTrue || rTrue).includes(value)) return true if ((opts.rFalse || rFalse).includes(value)) return false + return !!opts.string || false } return !!value }) diff --git a/test/boolean.test.js b/test/boolean.test.js new file mode 100644 index 0000000..6b19616 --- /dev/null +++ b/test/boolean.test.js @@ -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) + }) +}) diff --git a/test/type.test.js b/test/type.test.js deleted file mode 100644 index 0d81533..0000000 --- a/test/type.test.js +++ /dev/null @@ -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') - }) - -})