close
close

# Replace generic validation with reusable functions

# Replace generic validation with reusable functions

JavaScript and TypeScript developers often write the same conditionals over and over again. If you’re a web developer, you’ve probably come across code like this:

const handleSaveTextKeydown = (event: KeyboardEvent) => {
   if (event.key === 'Enter') {
    //... save text
   }
}
Go to full screen mode

Exit full screen

In this case, event.key is of the type stringand it’s easy to introduce bugs by accidentally adding a space 'Enter 'For example.

Why is this condition not defined in a function?

const handleSaveTextKeydown = (event: KeyboardEvent) => {
   if (checkIsEnterKey(event.key)) {
    //... save text
   }
}
Go to full screen mode

Exit full screen

This ensures that all checks on the Enter key are consistent and reliable.

Now consider this validation:

type Value = null | object;

const value = {} as Value;

if (typeof value === 'object') {
  value; // value type is null | object
}
Go to full screen mode

Exit full screen

Even though TypeScript is smart, value within the condition remains of type ValueThis is because typeof null returns 'object'.

So you have to write:

if (value !== null && typeof value === 'object') {
  value; // value type is object
}
Go to full screen mode

Exit full screen

Many developers will not capture this in a function and instead write it repeatedly when they encounter this situation.

How many times in your life have you written about the same condition?

How many times have you made the same mistake?

How many times will you write down the same condition in the future?

If I were you, I would do the following:

if (checkIsObject(value)) {
  value; // value type is object
}
Go to full screen mode

Exit full screen

There are many advantages to encapsulating generic conditions in functions.

Consider the following example:

const array = (0, 1, 2, 3, 4, 5, null, undefined);
Go to full screen mode

Exit full screen

Let’s create an array that excludes only null values.

You can emphasize brevity and write it like this, for example:

const numbers = array.filter(Boolean);
Go to full screen mode

Exit full screen

Unfortunately, this is not ideal. 0 is also evaluated as false and is excluded. So you should write:

const numbers = array.filter(item => item !== null && item !== undefined);
Go to full screen mode

Exit full screen

Don’t you think this is ugly, non-reusable code?

I can write more elegant code:

const numbers = array.filter(checkIsNullish);
Go to full screen mode

Exit full screen

Stop writing generic conditions repeatedly. It only leads to errors and makes the code less readable.

I would like to introduce you to a library I created: checker.

This utility function library represents commonly used conditions in general web development and low-level development as functions. All functions take an input and return a boolean value.

At the time of writing this article, it contains a wealth of functions to handle data types such as strings, numbers, booleans and nullish values. All functions are tested, documented and easy to use.

Let’s look at some real-world examples.

The packages provided by this library are all published on JSR. They can be easily installed in NPM, PNPM, Yarn, Bun and Deno projects.

Here we take the @checker/string package as an example with NPM.

  1. Install the package

Run the following command in your project folder:

  npx jsr add @checker/string
Go to full screen mode

Exit full screen

  1. Using the functions
  import { checkIsNotEmptyString, checkIsIndexFound } from "@checker/string";

  const value = "Hello";

  const formatted = value.trim();

  if (checkIsNotEmptyString(formatted)) {
    // formatted !== ''
    // When formatted is not an empty string
  }

  const index = value.indexOf("el");

  if (checkIsIndexFound(index)) {
    // index !== -1
    // When "el" is found in value
  }
Go to full screen mode

Exit full screen

I am not fond of using logical negation operators such as !SOME_CONDITION to invert a boolean value. This is because it is implicit, and simply inverting the boolean value by adding or removing it can lead to many dangerous situations.

Therefore, all functions have a corresponding checkIsNot~ functions defined.

Encapsulate generic conditions in functions. This makes the code more readable and bugs easier to spot.

Thanks for reading.