@ace-util/core
Version:
Utils.
130 lines • 3.32 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.equals = exports.isPrimitive = exports.objectIs = exports.isFunction = exports.isDate = exports.isPlainObject = exports.isObject = exports.isArray = exports.isUndef = exports.isUndefined = exports.isNull = void 0;
/**
* check if value is null
* @param val value
* @returns true/false
*/
function isNull(val) {
return val === null;
}
exports.isNull = isNull;
/**
* check if value is undefined
* @param val value
* @returns true/false
*/
function isUndefined(val) {
return typeof val === 'undefined';
}
exports.isUndefined = isUndefined;
/**
* alias to isUndefined
* @deprecated Use `isUndefined` instead. This alias may cause confusion due to inconsistent naming conventions.
* @param val value
* @returns true/false
*/
exports.isUndef = isUndefined;
/**
* check if value is a array
* @param val value
* @returns true/false
*/
function isArray(val) {
return Array.isArray(val);
}
exports.isArray = isArray;
/**
* check if value is an object
* @param val value
* @returns true/false
*/
function isObject(val) {
return val !== null && typeof val === 'object';
}
exports.isObject = isObject;
var toString = function (x) { return Object.prototype.toString.call(x); };
/**
* check if value is a plain object
* @param val value
* @returns true/false
*/
function isPlainObject(val) {
return toString(val) === '[object Object]';
}
exports.isPlainObject = isPlainObject;
/**
* check if value is a date
* @param val value
* @returns true/false
*/
function isDate(val) {
return toString(val) === '[object Date]';
}
exports.isDate = isDate;
/**
* check if value is a function
* @param val value
* @returns true/false
*/
function isFunction(val) {
return typeof val === 'function';
}
exports.isFunction = isFunction;
/**
* Inlined Object.is polyfill.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
*/
function objectIs(x, y) {
if (x === y) {
return x !== 0 || 1 / x === 1 / y;
}
// eslint-disable-next-line
return x !== x && y !== y;
}
exports.objectIs = objectIs;
/**
* check if value is primitive
* https://developer.mozilla.org/en-US/docs/Glossary/Primitive
* @param val value
* @returns true/false
*/
function isPrimitive(val) {
return (typeof val === 'string' ||
typeof val === 'number' ||
typeof val === 'symbol' ||
typeof val === 'boolean' ||
typeof val === 'bigint' ||
typeof val === 'undefined' ||
val === null);
}
exports.isPrimitive = isPrimitive;
/**
* compare two values are fully equal
* @param x value x
* @param y value y
* @returns true/false
*/
function equals(x, y) {
var f1 = x instanceof Object;
var f2 = y instanceof Object;
if (!f1 || !f2) {
return x === y;
}
if (Object.keys(x).length !== Object.keys(y).length) {
return false;
}
var newX = Object.keys(x);
for (var p in newX) {
p = newX[p];
var a = x[p] instanceof Object;
var b = y[p] instanceof Object;
if ((a && b && !equals(x[p], y[p])) || (!(a && b) && x[p] !== y[p])) {
return false;
}
}
return true;
}
exports.equals = equals;
//# sourceMappingURL=types.js.map
;