is-check-js
v1.0.0
Published
A comprehensive utility library for type checking and validation
Maintainers
Readme
is-check-js
A comprehensive utility library for type checking and validation in Node.js and JavaScript applications.
Features
- Comprehensive Type Checking: Over 30 utility functions for type validation
- TypeScript Support: Full TypeScript definitions included
- Zero Dependencies: Lightweight with no external dependencies
- Well Tested: 100% test coverage with Jest
- Modern JavaScript: Supports all modern JavaScript types including BigInt and Symbol
- Advanced Validations: Email, URL, IP address, credit card, and more
Installation
yarn add is-check-jsQuick Start
import { isString, isEmail, isEmpty, isPositive } from 'is-check-js';
// Basic type checking
console.log(isString('hello')); // true
console.log(isString(123)); // false
// Advanced validation
console.log(isEmail('[email protected]')); // true
console.log(isEmpty('')); // true
console.log(isPositive(5)); // trueAPI Reference
Basic Type Checking
isString(value)
Check if value is a string.
isString('hello'); // true
isString(123); // falseisNumber(value)
Check if value is a number (excluding NaN).
isNumber(123); // true
isNumber('123'); // false
isNumber(NaN); // falseisBoolean(value)
Check if value is a boolean.
isBoolean(true); // true
isBoolean('true'); // falseisFunction(value)
Check if value is a function.
isFunction(() => {}); // true
isFunction(Math.max); // trueisArray(value)
Check if value is an array.
isArray([]); // true
isArray([1, 2, 3]); // true
isArray({}); // falseisObject(value)
Check if value is a plain object (not array, null, or other objects).
isObject({}); // true
isObject({ a: 1 }); // true
isObject([]); // false
isObject(null); // falseisObjectLike(value)
Check if value is any type of object (including arrays, dates, etc.).
isObjectLike({}); // true
isObjectLike([]); // true
isObjectLike(new Date()); // true
isObjectLike(null); // falseisDate(value)
Check if value is a valid Date object.
isDate(new Date()); // true
isDate(new Date('invalid')); // falseisRegExp(value)
Check if value is a RegExp object.
isRegExp(/test/); // true
isRegExp(new RegExp('test')); // trueisNull(value), isUndefined(value), isNil(value)
Check for null, undefined, or both.
isNull(null); // true
isUndefined(undefined); // true
isNil(null); // true
isNil(undefined); // trueisSymbol(value), isBigInt(value)
Check for modern JavaScript types.
isSymbol(Symbol('test')); // true
isBigInt(123n); // trueAdvanced Validation
isEmpty(value)
Check if value is empty (works with strings, arrays, objects, Maps, Sets).
isEmpty(''); // true
isEmpty([]); // true
isEmpty({}); // true
isEmpty(null); // true
isEmpty('hello'); // falseisEmail(value)
Validate email addresses.
isEmail('[email protected]'); // true
isEmail('invalid-email'); // falseisURL(value)
Validate URLs.
isURL('https://example.com'); // true
isURL('http://localhost:3000'); // true
isURL('not-a-url'); // falseisPhoneNumber(value)
Basic phone number validation.
isPhoneNumber('1234567890'); // true
isPhoneNumber('+1 (555) 123-4567'); // trueisJSON(value)
Check if string is valid JSON.
isJSON('{"name": "John"}'); // true
isJSON('{invalid}'); // falseNumber Validation
isPositive(value), isNegative(value), isZero(value)
Number sign validation.
isPositive(5); // true
isNegative(-3); // true
isZero(0); // trueisInteger(value), isFloat(value)
Number type validation.
isInteger(42); // true
isFloat(3.14); // trueisEven(value), isOdd(value)
Even/odd number validation.
isEven(4); // true
isOdd(3); // trueisInRange(value, min, max)
Check if number is within a range.
isInRange(5, 0, 10); // true
isInRange(15, 0, 10); // falseAdvanced Format Validation
isCreditCard(value)
Validate credit card numbers using Luhn algorithm.
isCreditCard('4111111111111111'); // true (Visa test number)isIPv4(value), isIPv6(value)
IP address validation.
isIPv4('192.168.1.1'); // true
isIPv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // trueisHexColor(value)
Hexadecimal color validation.
isHexColor('#ff5733'); // true
isHexColor('#fff'); // trueisUUID(value)
UUID validation.
isUUID('123e4567-e89b-12d3-a456-426614174000'); // trueUtility Functions
isAny(value, ...predicates)
Check if value matches any of the provided predicates.
isAny('hello', isString, isNumber); // true
isAny(123, isString, isNumber); // trueisAll(value, ...predicates)
Check if value matches all of the provided predicates.
isAll('hello', isString, v => v.length > 0); // trueisType(value, type)
Check if value is of a specific type using string comparison.
isType('hello', 'string'); // true
isType([], 'array'); // true
isType(null, 'null'); // trueisTruthy(value), isFalsy(value)
Check truthiness and falsiness.
isTruthy(1); // true
isFalsy(0); // truegetType(value)
Get the type of a value as a string.
getType('hello'); // 'string'
getType([]); // 'array'
getType(null); // 'null'
getType(new Date()); // 'date'TypeScript Support
The library includes comprehensive TypeScript definitions:
import { isString, isEmail, isEmpty } from 'is-check-js';
const userInput: unknown = getUserInput();
if (isString(userInput)) {
// TypeScript knows userInput is a string here
console.log(userInput.toUpperCase());
}
if (isEmail(userInput)) {
// TypeScript knows userInput is a string (email)
sendEmail(userInput);
}Testing
Run the test suite:
yarn test
# Run with coverage
yarn test:coverage
# Run in watch mode
yarn test:watchContributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Add tests for your changes
- Ensure all tests pass (
yarn test) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
MIT License - see the LICENSE file for details.
Changelog
1.0.0
- Initial release with comprehensive type checking and validation utilities
- TypeScript support
- Full test coverage
- Zero dependencies
