dino-core
Version:
A dependency injection framework for NodeJS applications
76 lines • 2.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectHelper = void 0;
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
class ObjectHelper {
/**
* Validate if the provided value is defined, not null and not undefined
* @param {any} value the value to validate
* @returns true if the value is defined, false otherwise
* @public
* @static
*/
static isDefined(value) {
const defined = value !== null && value !== undefined;
if (typeof value === 'string') {
return defined && value.length > 0;
}
return defined;
}
/**
* Validate if the provided value is not defined, not null and not undefined
* @param {any} value the value to validate
* @returns true if the value is not defined, false otherwise
* @public
* @static
*/
static isNotDefined(value) {
return !ObjectHelper.isDefined(value);
}
/**
* Convert a class to plain object
* @param {object} clazz the class to be converted to plain object
* @public
* @static
*/
static classToPlainObject(clazz) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const entries = Object.entries(clazz);
return Object.fromEntries(entries);
}
static isObject(obj) {
return ObjectHelper.isDefined(obj) && typeof obj === 'object';
}
/**
* Evaluate if an obj is an instance of a type
*
* @param {Any} obj the object to be verified
* @param {Any} type the type the object will be verified against
* @returns {Boolean} true if obj is instance of type, false otherwise
*/
static instanceOf(obj, type) {
return (
// eslint-disable-next-line no-prototype-builtins
Boolean(type.isPrototypeOf(obj)) ||
obj instanceof type ||
// eslint-disable-next-line no-prototype-builtins
(obj.hasOwnProperty('prototype') !== undefined && obj.prototype instanceof type) ||
// eslint-disable-next-line no-prototype-builtins
obj.isPrototypeOf(type));
}
static invoke(obj, key, def = undefined) {
if (obj === undefined) {
return def;
}
let method = obj[key];
if (ObjectHelper.isNotDefined(method) && obj.prototype !== undefined) {
method = obj.prototype[key];
}
if (ObjectHelper.isDefined(method)) {
return method();
}
return def;
}
}
exports.ObjectHelper = ObjectHelper;
//# sourceMappingURL=object.helper.js.map