react-kiwi-dropdown
Version:
| Name | Type | Description | | --------------------------- | -------------------- | ----------- | | options | array | | selectedOption | string | | onCha
221 lines (217 loc) • 5.86 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
/**
* Returns the object type of the given payload
*
* @param {*} payload
* @returns {string}
*/
function getType(payload) {
return Object.prototype.toString.call(payload).slice(8, -1);
}
/**
* Returns whether the payload is undefined
*
* @param {*} payload
* @returns {payload is undefined}
*/
function isUndefined(payload) {
return getType(payload) === 'Undefined';
}
/**
* Returns whether the payload is null
*
* @param {*} payload
* @returns {payload is null}
*/
function isNull(payload) {
return getType(payload) === 'Null';
}
/**
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is {[key: string]: any}}
*/
function isPlainObject(payload) {
if (getType(payload) !== 'Object')
return false;
return (payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype);
}
/**
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is {[key: string]: any}}
*/
function isObject(payload) {
return isPlainObject(payload);
}
/**
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
*
* @param {*} payload
* @returns {payload is {[key: string]: any}}
*/
function isAnyObject(payload) {
return getType(payload) === 'Object';
}
/**
* Returns whether the payload is an object like a type passed in < >
*
* Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop.
*
* @template T this must be passed in < >
* @param {*} payload
* @returns {payload is T}
*/
function isObjectLike(payload) {
return isAnyObject(payload);
}
/**
* Returns whether the payload is a function
*
* @param {*} payload
* @returns {payload is Function}
*/
function isFunction(payload) {
return getType(payload) === 'Function';
}
/**
* Returns whether the payload is an array
*
* @param {*} payload
* @returns {payload is undefined}
*/
function isArray(payload) {
return getType(payload) === 'Array';
}
/**
* Returns whether the payload is a string
*
* @param {*} payload
* @returns {payload is string}
*/
function isString(payload) {
return getType(payload) === 'String';
}
/**
* Returns whether the payload is a string, BUT returns false for ''
*
* @param {*} payload
* @returns {payload is string}
*/
function isFullString(payload) {
return isString(payload) && payload !== '';
}
/**
* Returns whether the payload is ''
*
* @param {*} payload
* @returns {payload is string}
*/
function isEmptyString(payload) {
return payload === '';
}
/**
* Returns whether the payload is a number
*
* This will return false for NaN
*
* @param {*} payload
* @returns {payload is number}
*/
function isNumber(payload) {
return (getType(payload) === 'Number' && !isNaN(payload));
}
/**
* Returns whether the payload is a boolean
*
* @param {*} payload
* @returns {payload is boolean}
*/
function isBoolean(payload) {
return getType(payload) === 'Boolean';
}
/**
* Returns whether the payload is a regular expression
*
* @param {*} payload
* @returns {payload is RegExp}
*/
function isRegExp(payload) {
return getType(payload) === 'RegExp';
}
/**
* Returns whether the payload is a date, and that the date is Valid
*
* @param {*} payload
* @returns {payload is Date}
*/
function isDate(payload) {
return (getType(payload) === 'Date' && !isNaN(payload));
}
/**
* Returns whether the payload is a Symbol
*
* @param {*} payload
* @returns {payload is Symbol}
*/
function isSymbol(payload) {
return (getType(payload) === 'Symbol');
}
/**
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
*
* @param {*} payload
* @returns {*}
*/
function isPrimitive(payload) {
return (isBoolean(payload) ||
isNull(payload) ||
isUndefined(payload) ||
isNumber(payload) ||
isString(payload) ||
isSymbol(payload));
}
/**
* Does a generic check to check that the given payload is of a given type.
* In cases like Number, it will return true for NaN as NaN is a Number (thanks javascript!);
* It will, however, differentiate between object and null
*
* @template T
* @param {*} payload
* @param {T} type
* @throws {TypeError} Will throw type error if type is an invalid type
* @returns {payload is T}
*/
function isType(payload, type) {
if (!(type instanceof Function)) {
throw new TypeError('Type must be a function');
}
if (!type.hasOwnProperty('prototype')) {
throw new TypeError('Type is not a class');
}
// Classes usually have names (as functions usually have names)
var name = type.name;
return (getType(payload) === name) || Boolean(payload && (payload.constructor === type));
}
exports.getType = getType;
exports.isUndefined = isUndefined;
exports.isNull = isNull;
exports.isPlainObject = isPlainObject;
exports.isObject = isObject;
exports.isAnyObject = isAnyObject;
exports.isObjectLike = isObjectLike;
exports.isFunction = isFunction;
exports.isArray = isArray;
exports.isString = isString;
exports.isFullString = isFullString;
exports.isEmptyString = isEmptyString;
exports.isNumber = isNumber;
exports.isBoolean = isBoolean;
exports.isRegExp = isRegExp;
exports.isDate = isDate;
exports.isSymbol = isSymbol;
exports.isPrimitive = isPrimitive;
exports.isType = isType;