npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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 is allowNumberStrings 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
  }
}