DEV Community

Cover image for Discover the Charm of is.char and is.not_char in JavaScript with 'thiis'
Ivan Karbashevskyi
Ivan Karbashevskyi

Posted on

Discover the Charm of is.char and is.not_char in JavaScript with 'thiis'

JavaScript, with its dynamic nature, often involves dealing with various data types. When it comes to characters, ensuring that you're working with them correctly is crucial. That's where the is.char method and its counterpart, is.not_char from the 'thiis' package, come into play. In this article, we'll embark on a charming journey, exploring these methods and their utility in different scenarios.

Unveiling the Magic of Characters in JavaScript

Image description

Before we embark on our journey, let's take a moment to appreciate characters in JavaScript. A character is a single unit of text, and it can be a letter, number, or a special symbol. Ensuring that a value is indeed a character is essential for handling strings with precision.

Meet is.char - Your Character Connoisseur

Documentation link

Imagine you're on a quest to determine whether a value is a character. The is.char method is like your character connoisseur, helping you confirm if a value is indeed a character. Let's witness its charm:

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

const myCharacter = 'a';
const result = is.char(myCharacter);

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.char method to confirm that myCharacter is indeed a character. As expected, it returns true because the value is indeed a character.

The Charmed Examples

Now, let's explore six examples that showcase the versatility of is.char and its companion is.not_char. We'll cover a range of scenarios, including a delightful encounter with stream$ and a charming exploration of an array.

1. Checking a Single Character

The primary purpose of is.char is to confirm if a value is a single character. This is useful when validating user input or working with individual characters in a string:

import { is } from 'thiis';

const userInput = 'b';

if (is.char(userInput)) {
  // It's a charmer! Proceed with delight.
} else {
  // Handle the case where it's not a character gracefully.
}
Enter fullscreen mode Exit fullscreen mode

2. Guarding Against Non-Characters

On the flip side, is.not_char is your guardian against non-character values. It ensures that a value is not a character before proceeding:

import { is } from 'thiis';

const potentialNonChar = 123;

if (is.not_char(potentialNonChar)) {
  // Your guardian saves the day! Proceed with joy.
} else {
  // Handle the scenario where it's unexpectedly a character.
}
Enter fullscreen mode Exit fullscreen mode

3. Charmed Validation Function

Let's create a charming validation function that ensures a string consists of only characters:

import { is } from 'thiis';

function validateString(inputString) {
  if (inputString.split('').every(is.char)) {
    return 'String validated with charm!';
  } else {
    return 'Oops, non-characters detected!';
  }
}
Enter fullscreen mode Exit fullscreen mode

4. Charming Array Exploration

Arrays often contain a mix of data types. Use is.char to ensure that only characters make it into your array:

import { is } from 'thiis';

const charArray = ['a', 'b', 'c'];
const mixedArray = ['x', 123, 'y', true];

const allAreChars = charArray.every(is.char); // true
const someAreChars = mixedArray.some(is.char); // true

console.log(allAreChars);
console.log(someAreChars);
Enter fullscreen mode Exit fullscreen mode

5. Charmed Stream of Characters

Let's explore a scenario involving stream$ from RxJS. Using filter and is.char, we can ensure that the stream processes only character values:

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

const charStream$ = from(['a', 123, 'b', true]);

charStream$
  .pipe(
    filter(is.char)
  )
  .subscribe(char => {
    console.log(char); // Only character values will be part of the charmed stream.
  });
Enter fullscreen mode Exit fullscreen mode

6. Checking Charming User Input

When interacting with user input, especially from forms, use is.char to ensure that the entered value is a charming single character:

import { is } from 'thiis';

const userInput = document.getElementById('charInput').value;

if (is.char(userInput)) {
  // The user's input is a delightful single character!
} else {
  // Kindly guide the user to enter a valid character.
}
Enter fullscreen mode Exit fullscreen mode

The Charmed Conclusion

The is.char and is.not_char methods from the 'thiis' package add a touch of charm to your JavaScript journey. They ensure that your code interacts with characters exactly as intended, bringing clarity and precision to your character-related tasks. By incorporating the 'thiis' package into your JavaScript toolkit and exploring its documentation for more tips and examples, you can navigate the JavaScript landscape with a smile and a sprinkle of charm.

So, keep coding, and may your JavaScript endeavors be filled with characters that charm and delight!

🎗 ChatGPT & DALL·E 3

Top comments (1)

Collapse
 
karbashevskyi profile image
Ivan Karbashevskyi

Telegram channel:
t.me/thiis_pkg

NPM:
npmjs.com/thiis