Packages@accelint/predicates
isNothing
Type guard that determines if a value is null or undefined
Usage
import { isNothing } from '@accelint/predicates/is-nothing';
isNothing(null); // true
isNothing(undefined); // true
isNothing(0); // false
isNothing(''); // false
isNothing(false); // falseReference
function isNothing(val: unknown): booleanParameters
| Parameter | Type | Description |
|---|---|---|
val | unknown | Value to check |
Returns
Returns true if the value is null or undefined, false otherwise.
Examples
Example: Early return for missing values
import { isNothing } from '@accelint/predicates/is-nothing';
function processUser(user: User | null | undefined) {
if (isNothing(user)) {
console.log('No user provided');
return;
}
// Safe to use user here
console.log(user.name);
}Example: Filtering out null/undefined from arrays
import { isNothing } from '@accelint/predicates/is-nothing';
const values = [1, null, 2, undefined, 3, 0, false];
const notNull = values.filter(v => !isNothing(v));
// [1, 2, 3, 0, false]Example: Validating required fields
import { isNothing } from '@accelint/predicates/is-nothing';
interface FormData {
name?: string;
email?: string;
phone?: string;
}
function validateRequired(data: FormData): boolean {
return !isNothing(data.name) && !isNothing(data.email);
}
validateRequired({ name: 'John', email: 'john@example.com' }); // true
validateRequired({ name: 'John' }); // false
validateRequired({ email: 'john@example.com' }); // falseExample: Default value assignment
import { isNothing } from '@accelint/predicates/is-nothing';
function getConfigValue(value: string | null | undefined): string {
if (isNothing(value)) {
return 'default-config';
}
return value;
}
getConfigValue(null); // "default-config"
getConfigValue(undefined); // "default-config"
getConfigValue('custom'); // "custom"
getConfigValue(''); // "" (empty string is not nothing)Good to know: Uses loose equality (
==) rather than strict equality (===), so it matches bothnullandundefinedwith a single comparison. This is one of the rare cases where loose equality is preferred for brevity.
Related
- isSomething - Opposite check: value is not null or undefined
- isString - Check if value is a string
- isNumber - Check if value is a number