@se-oss/is
@se-oss/is is an elegant and type-safe value validation library for TypeScript and JavaScript.
Installation
npm install @se-oss/is
Install using your favorite package manager
pnpm
pnpm install @se-oss/is
yarn
yarn add @se-oss/is
Usage
Basic validation
import is from '@se-oss/is';
is.string('☕');
//=> true
is.number(42);
//=> true
is.array([1, 2, 3]);
//=> true
is.urlInstance(new URL('https://example.com'));
//=> true
Type detection
import is from '@se-oss/is';
is('hello');
//=> 'string'
is(new Map());
//=> 'Map'
is(new Uint8Array());
//=> 'Uint8Array'
Type narrowing
import is from '@se-oss/is';
const value: unknown = '☕';
if (is.string(value)) {
console.log(value.length);
//=> 2
}
Logical operators
import is from '@se-oss/is';
// Check if any of the predicates match
is.any([is.string, is.number], 'hello');
//=> true
// Check if all of the predicates match
is.all([is.array, is.nonEmptyArray], [1, 2, 3]);
//=> true
// Check if a value is optional
is.optional(undefined, is.string);
//=> true
// Use as predicate factories
const isStringOrNumber = is.any([is.string, is.number]);
isStringOrNumber('hello');
//=> true
Advanced usage
import is from '@se-oss/is';
// Validate array elements
is.array([1, 2, 3], is.number);
//=> true
// Check if a number is in range
is.inRange(3, [0, 5]);
//=> true
// Check for plain objects
is.plainObject({ a: 1 });
//=> true
Web & String validation
import is from '@se-oss/is';
is.json('{"a":1}');
//=> true
is.base64('SGVsbG8gd29ybGQ=');
//=> true
is.ipv4('127.0.0.1');
//=> true
is.hexColor('#ffffff');
//=> true
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.