not-empty-ts
v1.0.2
Published
A lightweight utility to check if an object is not empty, not null, or not undefined.
Maintainers
Readme
🐊 Not empty
A lightweight utility to check if a variable is not empty.
Features
- 🎯 Zero dependencies
- 📦 Lightweight (~XKB gzipped)
- 💪 Fully typed (TypeScript)
- 🔍 Preserves type information
- ⚡ Tree-shakeable
- 🧪 Well tested
📦 Installation
npm install not-empty-ts📚 Usage
import { notEmpty } from "not-empty-ts";
type User = {
name: string;
age: number;
};
async function getUser(): Promise<User|null> {
const response = await fetch("https://api.example.com/user");
const user = await response.json();
if (response.ok) {
return user;
}
return null;
}
const user = await getUser();
if (notEmpty(user) && user.age > 29){ // now this is type safe
console.log("User is older than 29");
}
📘 API
//Checks if the given object is not null or undefined and returns it.
notEmpty<T>(o: T | null | undefined): T
//Checks if the given object is not null and returns it.
notNull<T>(o: T | null): T
//Checks if the given object is not undefined and returns it.
notUndefined<T>(o: T | undefined): T🤝 Contributing
Contributions are welcome! Please open an issue or submit a pull request.
🚲 Testing
We use Vitest for testing aim for 100% coverage.
npm test📝 License
MIT
