1.0.2 โข Published 4 months ago
not-empty-ts v1.0.2
๐ 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