UNPKG

@lakutata/core

Version:

Lakutata Framework Core

114 lines 4.68 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseObject = void 0; const DependencyInjection_1 = require("../decorators/DependencyInjection"); const Exception_1 = require("./abstracts/Exception"); const MetadataKeys_1 = require("../constants/MetadataKeys"); let BaseObject = class BaseObject { get classPath() { const cachedModules = require('module')._cache; for (const modulePath of Object.keys(cachedModules)) { if (cachedModules[modulePath] && typeof cachedModules[modulePath] === 'object' && cachedModules[modulePath].exports && typeof exports === 'object') { for (const exportName of Object.keys(cachedModules[modulePath].exports)) { if (cachedModules[modulePath].exports[exportName] === this.constructor) { return modulePath; } } } } return ''; } get className() { return this.constructor.name; } hasMethod(name) { let candidate; if (this.hasProperty(name) || !!this[name]) { candidate = this[name]; } else { return false; } return typeof candidate === 'function'; } hasProperty(name) { return this.hasOwnProperty(name) || this.getConfigurablePropertyNames().includes(name); } setProperty(name, value, mode = 'overwrite') { if (this.hasProperty(name)) { switch (mode) { case 'overwrite': { this[name] = value; return true; } case 'partial': { if (typeof this[name] === typeof value && Array.isArray(value) && Array.isArray(this[name])) { this[name] = Array.from(new Set(this[name].concat(value))); } else if (typeof this[name] === typeof value && typeof this[name] === 'object') { this[name] = Object.assign(this[name], value); } else { this[name] = value; } return true; } default: { return false; } } } this[name] = value; return true; } defineProperty(p, attributes) { Object.defineProperty(this, p, attributes); } getConfigurablePropertyNames() { const configurableProperties = Reflect.getMetadata(MetadataKeys_1.CONFIGURABLE_NAMES, this); if (!configurableProperties) return []; return configurableProperties; } getProperty(name, defaultValue = undefined) { if (this.hasProperty(name)) return this[name]; return defaultValue; } generateException(exceptionConstructorOrArgument, ...args) { const self = this; let exceptionInstance; if (typeof exceptionConstructorOrArgument === 'function') { exceptionInstance = new exceptionConstructorOrArgument(args); exceptionInstance.district = self.className; } else { const exceptionArguments = [exceptionConstructorOrArgument].concat(args); const exceptionName = `${this.className}Exception`; const generatedExceptionConstructor = class extends Exception_1.Exception { constructor() { super(...arguments); this.district = self.className; this.exceptionName = exceptionName; this.err = exceptionName; } get name() { return exceptionName; } }; exceptionInstance = new generatedExceptionConstructor(exceptionArguments); } return exceptionInstance; } }; BaseObject = __decorate([ (0, DependencyInjection_1.Injectable)() ], BaseObject); exports.BaseObject = BaseObject; //# sourceMappingURL=BaseObject.js.map