DEV Community

Cover image for Unveiling the Magic of is.function and is.not_function in JavaScript with 'thiis'
Ivan Karbashevskyi
Ivan Karbashevskyi

Posted on

Unveiling the Magic of is.function and is.not_function in JavaScript with 'thiis'

JavaScript is a realm of wonders, and at its heart lies the powerful concept of functions. They are the building blocks of dynamic and expressive code. But how can you be sure if something is truly a function or, on the flip side, anything but a function? Enter the enchanting world of is.function and is.not_function methods from the 'thiis' package. In this article, we'll embark on a delightful journey to explore these tools, unraveling their magic in the world of JavaScript.

Image description

The Spell of Functions in JavaScript

Before we set off on our magical adventure, let's take a moment to appreciate the role of functions in JavaScript. They're like spells you cast to make things happen, encapsulating logic and enabling code to perform actions.

Meet is.function - Your Wizard of Functions

Documentation link

Imagine you're on a quest to distinguish between mere mortals and the magical wizards (functions) in your code. The is.function method is your trusty wizard detector, ensuring that a value is indeed a function. Let's see it in action:

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

const myFunction = () => {
  // A magical function indeed!
};

const result = is.function(myFunction);

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

In this example, we import the "is" object from the "thiis" package and use the is.function method to confirm that myFunction is indeed a magical function. As expected, it returns true because the value is indeed a function.

The Journey of Examples

Now, let's explore a series of whimsical examples that showcase the versatility of is.function and its playful companion, is.not_function. We'll embark on six unique scenarios, including a couple of magical ones.

1. Casting Spells with is.function

The primary role of is.function is to identify magical functions. It helps you ensure that a value is indeed a function before casting any spells:

import { is } from 'thiis';

const spell = someMagicalFunction();

if (is.function(spell)) {
  // Cast your spell with confidence!
  spell();
} else {
  // Handle the absence of magic gracefully.
}
Enter fullscreen mode Exit fullscreen mode

2. Guarding Against Muggles with is.not_function

Conversely, is.not_function serves as your guardian against non-magical beings (non-functions). It's handy when you want to ensure that a value isn't a function before proceeding:

import { is } from 'thiis';

const mundaneCreature = someNonMagicalBeing();

if (is.not_function(mundaneCreature)) {
  // Guard against non-magical mishaps!
} else {
  // Time to explore magical possibilities.
}
Enter fullscreen mode Exit fullscreen mode

3. Magical Validation with is.function

When working with user input, ensuring that the input is a magical function is crucial. Use is.function to validate and handle situations where the input might be a magical spell:

import { is } from 'thiis';

function validateMagicalSpell(spell) {
  if (is.function(spell)) {
    // Your spell is validated and ready to cast!
    spell();
  } else {
    return 'Please provide a valid magical spell.';
  }
}
Enter fullscreen mode Exit fullscreen mode

4. Magical Conditions with is.not_function

In decision-making scenarios, you might want to ensure that a value is anything but a magical function before proceeding:

import { is } from 'thiis';

const ordinaryObject = getOrdinaryObject();

if (is.not_function(ordinaryObject)) {
  // Execute actions for non-magical scenarios.
} else {
  // Handle magical beings with grace.
}
Enter fullscreen mode Exit fullscreen mode

5. Stream of Magic with is.function

Now, let's weave a magical scenario involving stream$ from RxJS. Using filter and is.function, we can ensure that the stream processes only magical functions:

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

const stream$ = from([() => 'A magical function', 'Not a function', () => 'Another spell', 42]);

stream$
  .pipe(
    filter(is.function)
  )
  .subscribe(spell => {
    console.log(spell()); // Only magical functions will be part of the stream's tale.
  });
Enter fullscreen mode Exit fullscreen mode

In this example, the filter(is.function) ensures that only magical functions get processed by the stream.

6. Array Enchantment with is.not_function

Arrays are another realm where is.not_function can work its enchantment. Use every() to confirm that all elements are not magical functions and some() to check if at least one isn't:

import { is } from 'thiis';

const notMagicalArray = [42, 'Not a spell', true];
const mixedArray = [() => 'A magical function', 'Just a string', null, undefined];

const allElementsNotFunction = notMagicalArray.every(is.not_function); // true
const someElementsNotFunction = mixedArray.some(is.not_function); // true

console.log(allElementsNotFunction);
console.log(someElementsNotFunction);
Enter fullscreen mode Exit fullscreen mode

Here, allElementsNotFunction checks if all elements in notMagicalArray are not magical functions, and someElementsNotFunction checks if at least one element in mixedArray is not a magical function.

The Adventure Continues

The is.function and is.not_function methods from the 'thiis' package are your delightful companions on your magical JavaScript adventure. They make type checking playful and precise, ensuring your code interacts with magical functions 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 confidence and a

sprinkle of magic.

So, wave your wand, cast your spells, and remember that the magic of JavaScript is always at your fingertips!

🎗 ChatGPT & DALL·E 3

Top comments (1)

Collapse
 
karbashevskyi profile image
Ivan Karbashevskyi

Telegram channel:
t.me/thiis_pkg

NPM:
npmjs.com/thiis