extended-booleans
v1.0.0
Published
Ever wish there were more booleans than just true or false?
Downloads
4
Readme
Extended JavaScript Booleans
Ever wish there were more booleans than just true or false? Extended Booleans is here to fix that. Extended Booleans provides 8 different values which dominate over JavaScript's built-in measly two. Extended Booleans can be:
- True: Represents a definitive affirmation. This is your standard true value, used when something is positively confirmed.
- False: Represents a definitive negation. This is your standard false value, used when something is definitely not true.
- Unknown: Used when the truth value of a condition is not known or cannot be determined at the current time. Basically saying "I don't know". Also could be used as a fallback value if a boolean is unspecified.
- Maybe: This is to represent that it could be SortOf, Indeterminate, or NonApplicable. Just a generic value for "It could be yes or no."
- SortOf: Indicates a partial agreement or a scenario that is somewhat true but not entirely. It's useful for situations where something is partially true but not fully or explicitly. Like if you have a function called
isNumber
which checks if a value is a number, and it takes some options. One of the options isallowNumberStrings
which is a true or false boolean that tells the function if the function will allow number strings like"32"
or"2"
. But when a user doesn't pass in the option the function could return SortOf. - Indeterminate: Signifies a state where the truth value cannot be determined because the information is ambiguous or contradictory. This is useful in complex decision-making processes where results are not clear-cut, or a function that takes in a lot of parameters that could contradict each other.
- NotApplicable: Used when a condition or value does not apply to the current context or situation. Could be used as an alternative for throwing a TypeError when you don't want to cause an error.
- Rejected: Indicates a definite refusal or denial. This is used in contexts where something has been explicitly and firmly turned down or deemed invalid.
Examples
Example using all of these:
const {
True,
False,
Unknown,
Maybe,
SortOf,
Indeterminate,
NotApplicable,
Rejected,
} = require('extended-booleans').default
// some dummy data
const users = [
{
id: 0,
name: 'John Doe',
password: '1234',
},
{
id: 1,
name: 'Jane Doe',
password: '54321',
},
]
// a function only John Doe can access
function JohnDoeIsNumber(
value,
options = {
allowAnyNumberStrings: Unknown, // fallback value,
allowNumberStringsContainingNumbersGreaterThan20: Unknown,
},
password = Unknown
) {
let {
allowAnyNumberStrings,
allowNumberStringsContainingNumbersGreaterThan20,
} = options // destructuring
if (
allowNumberStringsContainingNumbersGreaterThan20 !== True ||
allowNumberStringsContainingNumbersGreaterThan20 !== False ||
allowNumberStringsContainingNumbersGreaterThan20 !== Unknown
)
allowNumberStringsContainingNumbersGreaterThan20 = Unknown
const JohnDoe = users[0]
if (typeof password !== 'string') return NotApplicable
if (password !== JohnDoe.password) return Rejected
if (allowAnyNumberStrings === True) {
if (
allowNumberStringsContainingNumbersGreaterThan20 === False &&
parseInt(value) > 20
)
return False
if (
allowNumberStringsContainingNumbersGreaterThan20 === Unknown &&
parseInt(value) > 20
)
return Maybe
return typeof parseInt(value) === 'number'
} else if (allowAnyNumberStrings === False) {
if (allowNumberStringsContainingNumbersGreaterThan20 === True)
return Indeterminate
return typeof value === 'number'
} else {
return SortOf
}
}