@devgrid/common
Version:
Some useful primitives
221 lines • 9.36 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.isObject = exports.isPrimitive = exports.isSymbol = exports.isSet = exports.isRegexp = exports.isMap = exports.isError = exports.isDate = exports.isArrayBufferView = exports.isArrayBuffer = exports.isBoolean = exports.isSuffix = exports.isPrefix = exports.isSubstring = exports.isNegativeZero = exports.isFloat = exports.isEven = exports.isOdd = exports.isInfinite = exports.isNumeralInteger = exports.isNumeralBigInt = exports.isBigInt = exports.isNumeral = exports.isEmptyString = exports.isNil = exports.isExist = exports.isSafeInteger = exports.isInteger = exports.isFinite = exports.isNan = exports.isClass = exports.isUndefined = exports.isNull = exports.isPropertyOwned = exports.isPlainObject = exports.isBuffer = exports.isNumber = exports.isString = exports.isFunction = exports.isArray = exports.isNodejs = exports.aix = exports.sunos = exports.darwin = exports.openbsd = exports.freebsd = exports.linux = exports.isWindows = exports.getTagSimple = exports.getTag = void 0;
exports.isPromise = exports.isAsyncFunction = exports.isPropertyDefined = exports.isEmptyObject = void 0;
const objectProto = Object.prototype;
const { hasOwnProperty } = objectProto;
const { toString } = objectProto;
const funcToString = Function.prototype.toString;
const objectCtorString = funcToString.call(Object);
const symToStringTag = Symbol.toStringTag;
const getTag = (value) => {
if (value == null) {
return value === undefined ? "[object Undefined]" : "[object Null]";
}
if (symToStringTag && symToStringTag in Object(value)) {
return toString.call(value);
}
return toString.call(value);
};
exports.getTag = getTag;
const getTagSimple = (value) => {
const rawTag = toString.call(value);
if (value === null) {
return "null";
}
return rawTag.substring(8, rawTag.length - 1).toLowerCase();
};
exports.getTagSimple = getTagSimple;
exports.isWindows = process.platform === "win32";
exports.linux = process.platform === "linux";
exports.freebsd = process.platform === "freebsd";
exports.openbsd = process.platform === "openbsd";
exports.darwin = process.platform === "darwin";
exports.sunos = process.platform === "sunos";
exports.aix = process.platform === "aix";
exports.isNodejs = Object.prototype.toString.call(typeof process !== "undefined" ? process : 0) === "[object process]";
exports.isArray = Array.isArray;
const isFunction = (value) => typeof value === "function";
exports.isFunction = isFunction;
const isString = (value) => typeof value === "string" || value instanceof String;
exports.isString = isString;
const isNumber = (value) => typeof value === "number";
exports.isNumber = isNumber;
const isBuffer = (obj) => obj != null &&
((Boolean(obj.constructor) && typeof obj.constructor.isBuffer === "function" && obj.constructor.isBuffer(obj)) ||
Boolean(obj._isBuffer));
exports.isBuffer = isBuffer;
const isPlainObject = (value) => {
if (!(value != null && typeof value === "object") || (0, exports.getTag)(value) !== "[object Object]") {
return false;
}
const proto = Object.getPrototypeOf(value);
if (proto === null) {
return true;
}
const Ctor = hasOwnProperty.call(proto, "constructor") && proto.constructor;
return typeof Ctor === "function" && Ctor instanceof Ctor && funcToString.call(Ctor) === objectCtorString;
};
exports.isPlainObject = isPlainObject;
const isPropertyOwned = (obj, field) => hasOwnProperty.call(obj, field);
exports.isPropertyOwned = isPropertyOwned;
const isNull = (value) => value === null;
exports.isNull = isNull;
const isUndefined = (value) => value === undefined;
exports.isUndefined = isUndefined;
const isClass = (value) => (0, exports.isFunction)(value) &&
(0, exports.isPropertyOwned)(value, "prototype") &&
value.prototype &&
(0, exports.isPropertyOwned)(value.prototype, "constructor") &&
value.prototype.constructor.toString().substring(0, 5) === "class";
exports.isClass = isClass;
exports.isNan = Number.isNaN;
exports.isFinite = Number.isFinite;
exports.isInteger = Number.isInteger;
exports.isSafeInteger = Number.isSafeInteger;
const isExist = (value) => value != null;
exports.isExist = isExist;
const isNil = (value) => value == null;
exports.isNil = isNil;
const isEmptyString = (str) => typeof str === "string" && /^\s*$/.test(str);
exports.isEmptyString = isEmptyString;
const isNumeral = (value) => {
const tag = (0, exports.getTagSimple)(value);
if (tag !== "number" && tag !== "string") {
return false;
}
if ((0, exports.isEmptyString)(value)) {
return false;
}
try {
value = Number(value);
}
catch (e) {
return false;
}
return (0, exports.isFinite)(value);
};
exports.isNumeral = isNumeral;
const isBigInt = (value) => typeof value === "bigint";
exports.isBigInt = isBigInt;
const isNumeralBigInt = (value) => {
if (typeof value !== 'string')
return false;
return /^-?\d+n$/.test(value) && value !== 'n' && value !== '-n';
};
exports.isNumeralBigInt = isNumeralBigInt;
const isNumeralInteger = (value) => {
const tag = (0, exports.getTagSimple)(value);
if (tag !== "number" && tag !== "string") {
return false;
}
if ((0, exports.isEmptyString)(value)) {
return false;
}
try {
value = Number(value);
}
catch (error) {
return false;
}
return Number.isInteger(value);
};
exports.isNumeralInteger = isNumeralInteger;
const isInfinite = (val) => val === +1 / 0 || val === -1 / 0;
exports.isInfinite = isInfinite;
const isOdd = (val) => (0, exports.isInteger)(val) && val % 2 === 1;
exports.isOdd = isOdd;
const isEven = (val) => (0, exports.isInteger)(val) && val % 2 === 0;
exports.isEven = isEven;
const isFloat = (val) => (0, exports.isNumber)(val) && val !== Math.floor(val);
exports.isFloat = isFloat;
const isNegativeZero = (val) => val === 0 && Number.NEGATIVE_INFINITY === 1 / val;
exports.isNegativeZero = isNegativeZero;
const isSubstring = (substr, str, offset) => {
if (typeof substr !== "string" || typeof str !== "string") {
return false;
}
if (substr === "") {
return true;
}
const { length } = str;
if (length === 0) {
return false;
}
let normalizedOffset = 0;
if (offset !== undefined) {
normalizedOffset = Number(offset);
if (!Number.isFinite(normalizedOffset)) {
normalizedOffset = 0;
}
}
if (normalizedOffset < 0) {
normalizedOffset = length + normalizedOffset;
}
if (normalizedOffset < 0) {
normalizedOffset = 0;
}
if (normalizedOffset >= length) {
return false;
}
const result = str.indexOf(substr, normalizedOffset);
return result !== -1;
};
exports.isSubstring = isSubstring;
const isPrefix = (prefix, str) => {
if (typeof str !== 'string' || typeof prefix !== 'string')
return false;
return str.startsWith(prefix);
};
exports.isPrefix = isPrefix;
const isSuffix = (suffix, str) => {
if (typeof str !== 'string' || typeof suffix !== 'string')
return false;
return str.endsWith(suffix);
};
exports.isSuffix = isSuffix;
const isBoolean = (value) => value === true || value === false;
exports.isBoolean = isBoolean;
const isArrayBuffer = (x) => objectProto.toString.call(x) === "[object ArrayBuffer]";
exports.isArrayBuffer = isArrayBuffer;
const isArrayBufferView = (x) => ArrayBuffer.isView(x);
exports.isArrayBufferView = isArrayBufferView;
const isDate = (x) => (0, exports.getTagSimple)(x) === "date";
exports.isDate = isDate;
const isError = (value) => (0, exports.getTagSimple)(value) === "error";
exports.isError = isError;
const isMap = (value) => (0, exports.getTagSimple)(value) === "map";
exports.isMap = isMap;
const isRegexp = (value) => (0, exports.getTagSimple)(value) === "regexp";
exports.isRegexp = isRegexp;
const isSet = (value) => (0, exports.getTagSimple)(value) === "set";
exports.isSet = isSet;
const isSymbol = (value) => (0, exports.getTagSimple)(value) === "symbol";
exports.isSymbol = isSymbol;
const isPrimitive = (value) => (0, exports.isNil)(value) || (0, exports.isNumber)(value) || typeof value === "string" || (0, exports.isBoolean)(value) || (0, exports.isSymbol)(value);
exports.isPrimitive = isPrimitive;
const isObject = (value) => !(0, exports.isPrimitive)(value);
exports.isObject = isObject;
const isEmptyObject = (obj) => (0, exports.isObject)(obj) && Object.keys(obj).length === 0;
exports.isEmptyObject = isEmptyObject;
const isPropertyDefined = (obj, path) => {
if (!path || typeof path !== 'string')
return false;
if (!(0, exports.isObject)(obj))
return false;
let context = obj;
const keys = path.split(".");
for (const key of keys) {
if (!(0, exports.isObject)(context) || !(key in context)) {
return false;
}
context = context[key];
}
return true;
};
exports.isPropertyDefined = isPropertyDefined;
const isAsyncFunction = (fn) => fn && toString.call(fn).slice(8, -1) === "AsyncFunction";
exports.isAsyncFunction = isAsyncFunction;
const isPromise = (obj) => !(0, exports.isNil)(obj) && (0, exports.isFunction)(obj.then);
exports.isPromise = isPromise;
//# sourceMappingURL=predicates.js.map
;