1.0.2 โ€ข Published 4 months ago

not-empty-ts v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

๐ŸŠ Not empty

build and test workflow npm version License: MIT TypeScript

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

1.0.2

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago