0.1.5 allow add vs overwrite of true/false sets

master
David Kebler 2020-02-10 21:44:43 -08:00
parent 9d9c681689
commit 41e2df91eb
3 changed files with 10 additions and 7 deletions

View File

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

View File

@ -1,6 +1,6 @@
{ {
"name": "@uci-utils/to-boolean", "name": "@uci-utils/to-boolean",
"version": "0.1.4", "version": "0.1.5",
"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": {

View File

@ -1,18 +1,20 @@
let rTrue=['t','true','y','yes','on','positive','up','enabled','affirmative','yea','sure','online'] let rTrue=['t','true','y','yes','on','positive','up','enabled','affirmative','yea','sure','online','set','closed']
let rFalse=['f','false','n','no','off','negative','down','disabled','nope','offline'] let rFalse=['f','false','n','no','off','negative','down','disabled','nope','offline','unset','open']
function createBoolean (opts={}) { function createBoolean (opts={}) {
return (value => { return (value => {
if (opts.addTrue) rTrue = rTrue.concat(opts.addTrue) if (opts.addTrue) rTrue = rTrue.concat(opts.addTrue)
if (opts.addFalse) rFalse = rFalse.concat(opts.addFalse) if (opts.addFalse) rFalse = rFalse.concat(opts.addFalse)
if (value===undefined) return opts.undefined==='same' ? undefined : (opts.undefined || false) if (opts.True) rTrue = opts.True
if (value===null) return opts.null==='same' ? null : (opts.null || false) if (opts.False) rFalse = opts.False
if (value===undefined) return opts.undefined==='same' ? undefined : (opts.undefined || false) // false by default
if (value===null) return opts.null==='same' ? null : (opts.null || false) // false by default
if (!isNaN(Number(value))) return (opts.number==='default' ? !!value : (Number(value) > (opts.number || 0)? true : false)) 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 (rTrue.includes(value)) return true
if ((opts.rFalse || rFalse).includes(value)) return false if (rFalse.includes(value)) return false
return !!opts.string || false return !!opts.string || false
} }
return !!value return !!value