Commit 51aee719 authored by Milan Wikarski's avatar Milan Wikarski
Browse files

added is.keyValuePair

parent 16eca243
......@@ -6,6 +6,7 @@ const _is = {
undefined: require('./src/is-undefined'),
callable: require('./src/is-callable'),
emptyString: require('./src/is-empty-string'),
keyValuePair: require('./src/is-key-value-pair'),
instance: require('./src/is-instance')
};
......@@ -53,7 +54,7 @@ _is.array = function isArray(val, typeCheck = null) {
/**
* 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'}
* - `{string}`: type name - one of {'string', 'array', 'numeric', 'object', 'null', 'undefined', 'callable', 'emptyString', 'keyValuePair'}
* - `{Object}`: instance (eg. Regex) - checks instanceof
*
* @param {*} val
......@@ -89,6 +90,7 @@ is.null = _is.null;
is.undefined = _is.undefined;
is.callable = _is.callable;
is.emptyString = _is.emptyString;
is.keyValuePair = _is.keyValuePair;
is.instance = _is.instance;
......
const isObject = require('./is-object');
/**
* Returns `true` if val is an key-value pair (object with one key and one value); `false` otherwise
*
* @param {*} val
* @returns {Boolean}
*/
function isKeyValuePair(val) {
return isObject(val) && Object.entries(val).length == 1;
}
module.exports = isKeyValuePair;
......@@ -12,4 +12,7 @@ const { is, force } = require('../index');
// is.undefined(undefined)
// ]);
console.log(is.array(['', '2', ''], 'emptyString'));
// console.log(is.array(['', '2', ''], 'emptyString'));
console.log(is.keyValuePair({ key: 'value' }));
console.log(is.keyValuePair({ key: 'value', key2: 'value2' }));
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