DEV Community

Cover image for Unleashing the Magic of `is.truthy` and `is.not_truthy` with 'thiis': A Playground of JavaScript Booleans
Ivan Karbashevskyi
Ivan Karbashevskyi

Posted on • Edited on

Unleashing the Magic of `is.truthy` and `is.not_truthy` with 'thiis': A Playground of JavaScript Booleans

JavaScript is an enchanting universe filled with wonders, and at its heart lies the mystical concept of "truthy" values. These values are the unsung heroes, encompassing anything that evaluates to true in a boolean context. But how can you unravel the magic of determining if a value is truly "truthy" or not? Enter the duo of is.truthy and is.not_truthy methods from the 'thiis' package. In this delightful article, we'll embark on an adventure to explore these tools and witness their prowess in the realm of JavaScript.

Unveiling the Charms of "Truthy" Values

Before we embark on our journey, let's bask in the brilliance of "truthy" values. They are the silent forces behind conditional statements, encompassing values like true, non-empty strings, non-zero numbers, and more. Understanding these magical values is key to creating robust and expressive code.

Introducing is.truthy - Your Truth Unveiler

Documentation link

Picture yourself on a quest to unveil the truth within your code. The is.truthy method is your reliable companion, adept at confirming that a value is genuinely "truthy." Witness its magic in action:

import { is } from 'thiis'; // Import the "is" object from the "thiis" package

const myValue = 'I am truthy!';
const result = is.truthy(myValue);

console.log(result); // true
Enter fullscreen mode Exit fullscreen mode

In this example, we import the "is" object from the "thiis" package and employ the is.truthy method to confirm that myValue is indeed a "truthy" value. As expected, it returns true because the value is genuinely "truthy."

A Whirlwind of Examples

Now, let's embark on a whirlwind tour of practical examples that showcase the versatility of is.truthy and its delightful partner, is.not_truthy. We'll explore six distinct scenarios, including some captivating ones.

1. Embracing Truth in Conditions

The primary role of is.truthy is to embrace truth in conditions. It assists in determining when a value is genuinely "truthy," adding clarity to your code:

import { is } from 'thiis';

const potentialTruth = someFunctionThatMayReturnTruthyValue();

if (is.truthy(potentialTruth)) {
  // Bask in the glory of truth!
} else {
  // Navigate through other possibilities.
}
Enter fullscreen mode Exit fullscreen mode

2. Guarding Against Non-Truth with is.not_truthy

On the flip side, is.not_truthy acts as your guardian against non-truth. Use it to ensure a value isn't "truthy" before venturing forth:

import { is } from 'thiis';

const importantValue = someFunctionThatShouldNotBeTruthy();

if (is.not_truthy(importantValue)) {
  // Your guardian shields you from unintended truth!
} else {
  // Time to explore other avenues.
}
Enter fullscreen mode Exit fullscreen mode

3. Empowering Validation with is.truthy

When validating user input, ensuring that the input is "truthy" is paramount. Leverage is.truthy to validate and handle situations where the input might be "truthy":

import { is } from 'thiis';

function validateUserInput(input) {
  if (is.truthy(input)) {
    return 'Input validated successfully!';
  } else {
    return 'Please provide valid input.';
  }
}
Enter fullscreen mode Exit fullscreen mode

4. Confident Actions with is.not_truthy

Confidence is key in programming. Confirm that a value is not "truthy" before executing specific actions with is.not_truthy:

import { is } from 'thiis';

const userChoice = getUserInput();

if (is.not_truthy(userChoice)) {
  // Execute actions for non-truthy choices.
} else {
  // Gracefully handle other scenarios.
}
Enter fullscreen mode Exit fullscreen mode

5. Stream of Truth with is.truthy

Let's venture into the realm of streams using stream$ from RxJS. By using filter and is.truthy, we can ensure that the stream processes only "truthy" values:

import { is } from 'thiis';
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';

const stream$ = from(['', 0, null, 'truthy value', undefined]);

stream$
  .pipe(
    filter(is.truthy)
  )
  .subscribe(value => {
    console.log(value); // Only "truthy" values will dance in the stream.
  });
Enter fullscreen mode Exit fullscreen mode

In this example, the filter(is.truthy) ensures that only "truthy" values get processed by the stream.

6. Array Symphony with is.not_truthy

Arrays offer another canvas for is.not_truthy to showcase its charm. Use every() to confirm that all elements are not "truthy" and some() to check if at least one isn't:

import { is } from 'thiis';

const notTruthyArray = [1, 'truthy', true];
const mixedArray = ['', 'truthy', 0, null];

const allElementsNotTruthy = notTruthyArray.every(is.not_truthy); // false
const someElementsNotTruthy = mixedArray.some(is.not_truthy); // true

console.log(allElementsNotTruthy);
console.log(someElementsNotTruthy);
Enter fullscreen mode Exit fullscreen mode

Here, allElementsNotTruthy checks if all elements in notTruthyArray are not "truthy," and someElementsNotTruthy checks if at least one element in mixedArray is not "truthy."

The Magical Journey Continues

The is.truthy and is.not_truthy methods from the 'thiis' package are your magical companions in the enchant

ing world of JavaScript. They infuse playfulness and precision into type checking, ensuring your code interacts with "truthy" values exactly as intended. By adding the 'thiis' package to your JavaScript toolkit and exploring its documentation for more tips and examples, you can navigate the JavaScript landscape with joy and a sprinkle of magic.

So, continue coding, and remember that the magic in JavaScript is endless!

🎗 ChatGPT & DALL·E 3

Top comments (1)

Collapse
 
karbashevskyi profile image
Ivan Karbashevskyi

Telegram channel:
t.me/thiis_pkg

NPM:
npmjs.com/thiis