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