jizy-utils
Version:
A simple JS utility library.
58 lines (40 loc) • 2.02 kB
JavaScript
const Types = {};
Types.isNull = (x) => x === null;
Types.isUndefined = (x) => x === undefined;
Types.isNil = (x) => Types.isNull(x) || Types.isUndefined(x);
// check for strings and string literal type. e.g: 's', "s", `str`, new String()
Types.isString = (x) => !Types.isNil(x) && (typeof x === 'string' || x instanceof String);
// check for number or number literal type. e.g: 12, 30.5, new Number()
Types.isNumber = (x) => !Types.isNil(x) && ((!isNaN(x) && isFinite(x) && typeof x === 'number') || x instanceof Number);
// check for boolean or boolean literal type. e.g: true, false, new Boolean()
Types.isBoolean = (x) => !Types.isNil(x) && (typeof x === 'boolean' || x instanceof Boolean);
Types.isArray = (x) => !Types.isNil(x) && Array.isArray(x);
// check for object or object literal type. e.g: {}, new Object(), Object.create(null)
Types.isObject = (x) => !Types.isNil(x) && ({}).toString.call(x) === '[object Object]';
// check for provided type instance
Types.is = (x, X) => !Types.isNil(x) && x instanceof X;
Types.isSet = (x) => Types.is(x, Set);
Types.isMap = (x) => Types.is(x, Map);
Types.isDate = (x) => Types.is(x, Date);
// check for provided type
Types.isA = (x, X) => !Types.isNil(x) && typeof x === X;
Types.isFunction = (x) => Types.isA(x, 'function');
Types.isWindow = (x) => {
return Types.isObject(x) && x == x.window;
}
Types.isDocument = (x) => {
return Types.isObject(x) && x.nodeType == x.DOCUMENT_NODE;
}
Types.isPlainObject = (x) => {
return Types.isObject(x) && !Types.isWindow(x) && Object.getPrototypeOf(x) == Object.prototype;
}
Types.isEmptyObject = (x) => {
return Types.isObject(x) && Object.keys(x).length === 0;
}
Types.likeArray = (x) => {
const length = !!x && 'length' in x && x.length;
return !Types.isFunction(x) && !Types.isWindow(x) && (
Array.isArray(x) || length === 0 || (Types.isNumber(length) && length > 0 && (length - 1) in x)
);
}
export default Types;