Skip to content

shahradelahi/assert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

@se-oss/assert

CI NPM Version MIT License npm bundle size Install Size

@se-oss/assert is a lightweight, type-safe assertion library for TypeScript and JavaScript, powered by @se-oss/is.


πŸ“¦ Installation

npm install @se-oss/assert
Install using your favorite package manager

pnpm

pnpm install @se-oss/assert

yarn

yarn add @se-oss/assert

πŸ“– Usage

@se-oss/assert provides a set of assertion functions that throw a TypeError if the condition is not met. It is powered by @se-oss/is, so all predicates from is are available as assertions.

Basic Example

import { assert } from '@se-oss/assert';

// Throws if not a string
assert.string('hello');

// Throws with a custom message
assert.string(123, 'Value must be a string');
// => TypeError: Value must be a string

Common Assertions

// Numbers
assert.number(42);
assert.integer(42);
assert.positiveNumber(10);
assert.inRange(15, 'Not in range', [10, 20]);

// Objects & Arrays
assert.plainObject({ foo: 'bar' });
assert.array([1, 2, 3]);
assert.nonEmptyArray([1]);

// Strings
assert.nonEmptyString('hello');
assert.json('{"a":1}');
assert.ipv4('127.0.0.1');

// Logic
assert.truthy(true);
assert.falsy(false);

Integration with TypeScript

The library is fully type-safe and provides type guards for your variables.

function process(value: unknown) {
  assert.string(value);

  // 'value' is now narrowed to string
  console.log(value.toUpperCase());
}

πŸ“š Documentation

For all configuration options, please see the API docs.

🀝 Contributing

Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub

Thanks again for your support, it is much appreciated! πŸ™

License

MIT Β© Shahrad Elahi and contributors.

About

πŸ›‘οΈ Lightweight, type-safe assertion library for TypeScript.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors