0.1.5 allow add vs overwrite of true/false sets
parent
9d9c681689
commit
41e2df91eb
|
@ -3,3 +3,4 @@ test/
|
|||
*.test.js
|
||||
testing/
|
||||
*.lock
|
||||
examples/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"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",
|
||||
"main": "src/boolean.js",
|
||||
"scripts": {
|
||||
|
|
|
@ -1,18 +1,20 @@
|
|||
let rTrue=['t','true','y','yes','on','positive','up','enabled','affirmative','yea','sure','online']
|
||||
let rFalse=['f','false','n','no','off','negative','down','disabled','nope','offline']
|
||||
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','unset','open']
|
||||
|
||||
function createBoolean (opts={}) {
|
||||
return (value => {
|
||||
if (opts.addTrue) rTrue = rTrue.concat(opts.addTrue)
|
||||
if (opts.addFalse) rFalse = rFalse.concat(opts.addFalse)
|
||||
if (value===undefined) return opts.undefined==='same' ? undefined : (opts.undefined || false)
|
||||
if (value===null) return opts.null==='same' ? null : (opts.null || false)
|
||||
if (opts.True) rTrue = opts.True
|
||||
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 (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
|
||||
if (rTrue.includes(value)) return true
|
||||
if (rFalse.includes(value)) return false
|
||||
return !!opts.string || false
|
||||
}
|
||||
return !!value
|
||||
|
|
Loading…
Reference in New Issue