Commit 08f85425 authored by Milan Wikarski's avatar Milan Wikarski
Browse files

Array items type check

parent 1be7c6ec
node_modules
npm-debug.log
\ No newline at end of file
npm-debug.log
todo.md
\ No newline at end of file
module.exports = require('./src/js-type');
const _is = {
string: require('./src/is-string'),
numeric: require('./src/is-numeric'),
object: require('./src/is-object'),
null: require('./src/is-null'),
undefined: require('./src/is-undefined'),
callable: require('./src/is-callable'),
emptyString: require('./src/is-empty-string'),
instance: require('./src/is-instance')
};
/**
* Returns `true` if val is an array; `false` otherwise.
* typeCheck can be one of:
* - Function -- in this case it has to be a function from is object. Each value in array will be checked vs this function
* - Array -- in this case it has to be a valid argument for is function
*
* @param {*} val
* @param {Function|Array} typeCheck?
* @returns {boolean}
*/
_is.array = function isArray(val, typeCheck = null) {
if (!Array.isArray(val)) {
return false;
}
if (_is.null(typeCheck)) {
return true;
}
if (_is.callable(typeCheck)) {
for (let i = 0; i < val.length; i++) {
if (!typeCheck(val[i])) {
return false;
}
}
return true;
}
if (_is.array(val)) {
for (let i = 0; i < val.length; i++) {
if (!is(val[i], typeCheck)) {
return false;
}
}
return true;
}
};
/**
* Checks if value is of one of valid types specified by `types` array.
* valid items in `types` are:
* - `{string}`: type name - one of {'string', 'array', 'numeric', 'object', 'null', 'undefined', 'callable', 'emptyString'}
* - `{Object}`: instance (eg. Regex) - checks instanceof
*
* @param {*} val
* @param {String|Object|Array.<string|Object>} types
* @returns {Boolean}
*/
function is(val, types) {
if (!_is.array(types)) {
types = [types];
}
for (let i = 0; i < types.length; i++) {
const type = types[i];
if (_is.string(type)) {
if (_is[type](val)) {
return true;
}
} else if (_is.object(type)) {
if (_is.instance(val, type)) {
return true;
}
}
}
return false;
}
is.string = _is.string;
is.array = _is.array;
is.numeric = _is.numeric;
is.object = _is.object;
is.null = _is.null;
is.undefined = _is.undefined;
is.callable = _is.callable;
is.emptyString = _is.emptyString;
is.instance = _is.instance;
module.exports = is;
const isInstance = require('./is-instance');
/**
* Returns `true` is value is a function; `false` otherwise
*
* @param {*} val
* @returns {Boolean}
*/
function isCallable(val) {
return isInstance(val, Function);
}
module.exports = isCallable;
/**
* Returns `true` if value is an empty string; `false` otherwise
*
* @param {*} val
* @returns {Boolean}
*/
function isEmptyString(val) {
return val === '';
}
module.exports = isEmptyString;
/**
* Returns `true` is val is instanceof prototype
*
* @param {*} val
* @param {*} prototype
* @returns {Boolean}
*/
function isInstance(val, prototype) {
return val instanceof prototype;
}
module.exports = isInstance;
/**
* Returns `true` if val is null; `false` otherwise
*
* @param {*} val
* @returns {Boolean}
*/
function isNull(val) {
return val == null;
}
module.exports = isNull;
/**
* Returns `true` if val is numeric; `false` otherwise
*
* @param {*} val
* @returns {boolean}
*/
function isNumeric(val) {
return !isNaN(Number(val));
}
module.exports = isNumeric;
const isInstance = require('./is-instance');
/**
* Returns `true` is val is Object; `false` otherwise
*
* @param {*} val
* @returns {Boolean}
*/
function isObject(val) {
return isInstance(val, Object);
}
module.exports = isObject;
/**
* Returns `true` if val is a string; `false` otherwise
*
* @param {*} val
* @returns {boolean}
*/
function isString(val) {
return typeof val == 'string';
}
module.exports = isString;
/**
* Returns `true` if value is undefined; `false` otherwise
*
* @param {*} val
* @returns {Boolean}
*/
function isUndefined(val) {
return val === undefined;
}
module.exports = isUndefined;
/**
* Returns `true` if val is a string; `false` otherwise
*
* @param {*} val
* @returns {boolean}
*/
function isString(val) {
return typeof val == 'string';
}
/**
* Returns `true` if val is an array; `false` otherwise
*
* @param {*} val
* @returns {boolean}
*/
function isArray(val) {
return Array.isArray(val);
}
/**
* Returns `true` if val is numeric; `false` otherwise
*
* @param {*} val
* @returns {boolean}
*/
function isNumeric(val) {
return !isNaN(Number(val));
}
/**
* Returns `true` is val is instanceof prototype
*
* @param {*} val
* @param {*} prototype
* @returns {Boolean}
*/
function isInstance(val, prototype) {
return val instanceof prototype;
}
/**
* Returns `true` is val is Object; `false` otherwise
*
* @param {*} val
* @returns {Boolean}
*/
function isObject(val) {
return isInstance(val, Object);
}
/**
* Returns `true` if val is null; `false` otherwise
*
* @param {*} val
* @returns {Boolean}
*/
function isNull(val) {
return val == null;
}
/**
* Returns `true` if value is undefined; `false` otherwise
*
* @param {*} val
* @returns {Boolean}
*/
function isUndefined(val) {
return val === undefined;
}
/**
* Returns `true` is value is a function; `false` otherwise
*
* @param {*} val
* @returns {Boolean}
*/
function isFunction(val) {
return isInstance(val, Function);
}
const _is = {
string: isString,
array: isArray,
numeric: isNumeric,
instance: isInstance,
object: isObject,
null: isNull,
undefined: isUndefined,
function: isFunction
};
/**
* Checks if value is of one of valid types specified by `types` array.
* valid items in `types` are:
* - `{string}`: type name (eg. 'numeric') - checks type
* - `{Object}`: instance (eg. Regex) - checks instanceof
*
* @param {*} val
* @param {Array.<string|Object>} types
* @returns
*/
function is(val, types) {
if (!_is.array(types)) throw TypeError('Types is not an array');
for (let i = 0; i < types.length; i++) {
const type = types[i];
if (isString(type)) {
if (_is[type](val)) {
return true;
}
} else if (isObject(type)) {
if (isInstance(val, type)) {
return true;
}
}
}
return false;
}
is.string = isString;
is.array = isArray;
is.numeric = isNumeric;
is.instance = isInstance;
is.object = isObject;
is.null = isNull;
is.undefined = isUndefined;
is.function = isFunction;
module.exports = is;
const is = require('../index');
// console.log([
// is.array(['foo', 'bar']),
// is.emptyString(''),
// is.callable(() => null),
// is.instance([], Object),
// is.null(null),
// is.numeric('42'),
// is.object({ foo: 'bar' }),
// is.string('halo'),
// is.undefined(undefined)
// ]);
console.log(is.array(['', '2', ''], 'emptyString'));
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment