What is check-more-types?
The check-more-types npm package provides a variety of type checking and assertion functions that enhance JavaScript's type testing capabilities. It offers a wide range of predicates for more precise type validation, making it easier to enforce type safety in JavaScript applications.
What are check-more-types's main functionalities?
Primitive type checks
This feature allows for checking JavaScript's primitive types such as strings, numbers, and booleans. It's useful for basic type validation.
const check = require('check-more-types');
// checks if a value is a string
check.string('hello'); // true
// checks if a value is a number
check.number(42); // true
Complex type checks
This feature extends the library's capabilities to more complex types, such as arrays of specific types or objects matching a schema. It's particularly useful for validating data structures.
const check = require('check-more-types');
// checks if a value is an array of strings
check.arrayOfStrings(['hello', 'world']); // true
// checks if a value is an object with specific properties
check.schema({
name: check.string,
age: check.number
})({ name: 'John', age: 30 }); // true
Custom predicates
check-more-types allows for the creation of custom predicates, enhancing its flexibility and allowing users to define their own specific type checks.
const check = require('check-more-types');
// defines a custom predicate for checking if a number is even
check.mixin({
even: function (n) { return check.number(n) && n % 2 === 0; }
});
// uses the custom predicate
check.even(42); // true
Other packages similar to check-more-types
joi
Joi is a powerful schema description language and data validator for JavaScript. Compared to check-more-types, Joi offers a more comprehensive API for defining complex validation schemas, making it better suited for validating nested objects and complex data structures.
prop-types
Prop-types is a library for type checking React component props. While check-more-types is more general-purpose, prop-types is specifically designed for React applications, offering a straightforward way to ensure components receive props of the correct type.
validator
Validator is a library that provides string validation and sanitization. Unlike check-more-types, which offers a broad range of type checks, Validator focuses on string validation, offering functions for format validation, sanitization, and more.
check-more-types v0.1.2
Additional type checks for check-types.js
Install
node: npm install check-more-types --save
global.check = requier('check-types');
require('check-more-types');
// patches global check object
browser bower install check-more-types --save
<script src="check-types.js"></script>
<script src="check-more-types.js"></script>
API
check.defined
check.defined(0); // true
check.defined(1); // true
check.defined(true); // true
check.defined(false); // true
check.defined(null); // true
check.defined(''); // true
check.defined(); // false
check.defined(root.does_not_exist); // false
check.defined({}.does_not_exist); // false
check.bit
check.bit(0); // true
check.bit(1); // true
check.bit('1'); // false
check.bit(2); // false
check.bit(true); // false
check.unemptyArray
check.unemptyArray(null); // false
check.unemptyArray(1); // false
check.unemptyArray({}); // false
check.unemptyArray([]); // false
check.unemptyArray(root.does_not_exist); // false
check.unemptyArray([1]); // true
check.unemptyArray(['foo', 'bar']); // true
check.arrayOfStrings
// second argument is checkLowerCase
check.arrayOfStrings(['foo', 'Foo']); // true
check.arrayOfStrings(['foo', 'Foo'], true); // false
check.arrayOfStrings(['foo', 'bar'], true); // true
check.arrayOfStrings(['FOO', 'BAR'], true); // false
check.arrayOfArraysOfStrings
// second argument is checkLowerCase
check.arrayOfArraysOfStrings([['foo'], ['bar'}}); // true
check.arrayOfArraysOfStrings([['foo'], ['bar'}}, true); // true
check.arrayOfArraysOfStrings([['foo'], ['BAR'}}, true); // false
check.lowerCase
check.lowerCase('foo bar'); // true
check.lowerCase('*foo ^bar'); // true
check.lowerCase('fooBar'); // false
check.has
var obj = {
foo: 'foo',
bar: 0
};
check.has(obj, 'foo'); // true
check.has(obj, 'bar'); // true
check.has(obj, 'baz'); // false
check.all
var obj = {
foo: 'foo',
bar: 'bar',
baz: 'baz'
};
var predicates = {
foo: check.unemptyString,
bar: function(value) {
return value === 'bar';
}
};
check.all(obj, predicates); // true
Small print
Author: Kensho © 2014
Support: if you find any problems with this library,
open issue on Github
MIT License
The MIT License (MIT)
Copyright (c) 2014 Kensho
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.