ravendb
Version:
RavenDB client for Node.js
114 lines • 3.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypeUtil = void 0;
class TypeUtil {
static MAX_INT32 = 2147483647;
static MIN_INT32 = -2147483648;
// eslint-disable-next-line @typescript-eslint/no-empty-function
static NOOP = () => {
// empty by design
};
static ASYNC_NOOP = () => Promise.resolve(undefined);
static isNullOrUndefined(value) {
return ("undefined" === (typeof value)) || value === null;
}
static isUndefined(value) {
return typeof value === "undefined";
}
static isString(value) {
return typeof (value) === "string";
}
static isNumber(value) {
return typeof (value) === "number";
}
static isPrimitive(value) {
return TypeUtil.isNumber(value)
|| TypeUtil.isString(value)
|| TypeUtil.isBool(value);
}
static isPrimitiveType(type) {
return type === Number ||
type === String ||
type === Boolean;
}
static isArray(value) {
return Array.isArray(value);
}
static isObject(value) {
return value
&& typeof (value) === "object"
&& !this.isArray(value);
}
// eslint-disable-next-line @typescript-eslint/ban-types
static isFunction(value) {
return typeof (value) === "function";
}
static isDate(value) {
return value && value.constructor && value.constructor.name === "Date";
}
static isBool(value) {
return value === true || value === false;
}
static isClass(value) {
return this.isFunction(value) && ("name" in value) && value.name !== ""
&& ("Object" !== value.name)
&& (!!value.prototype && !!value.prototype.constructor.name);
}
static isObjectTypeDescriptor(value) {
return !!value
&& typeof value !== "string"
&& (this.isClass(value) || this.isObjectLiteralTypeDescriptor(value));
}
static isType(obj, typeDescriptor) {
if (!typeDescriptor) {
return false;
}
return obj
&& (typeDescriptor.isType
&& typeDescriptor.isType(obj))
|| (obj && obj.constructor.name === typeDescriptor.name);
}
static isObjectLiteralTypeDescriptor(typeDescriptor) {
return typeDescriptor
&& !this.isClass(typeDescriptor)
&& typeof typeDescriptor.isType === "function";
}
static findType(obj, typeDescriptors) {
if (!obj) {
return null;
}
if (TypeUtil.isClass(obj.constructor)) {
return obj.constructor;
}
for (const typeDescriptor of typeDescriptors) {
if (!TypeUtil.isObjectLiteralTypeDescriptor(typeDescriptor)) {
return;
}
if (TypeUtil.isType(obj, typeDescriptor)) {
return typeDescriptor;
}
}
return null;
}
static isInstanceOf(type, typeToCheck) {
return TypeUtil.isClass(type)
&& TypeUtil.isClass(typeToCheck)
&& type instanceof typeToCheck;
}
static isSet(obj) {
return obj
&& obj.constructor
&& obj.constructor.name === "Set";
}
static isMap(obj) {
return obj
&& obj.constructor
&& obj.constructor.name === "Map";
}
static isDocumentType(obj) {
return obj && (this.isString(obj) ||
this.isObjectTypeDescriptor(obj));
}
}
exports.TypeUtil = TypeUtil;
//# sourceMappingURL=TypeUtil.js.map