UNPKG

@dvcol/common-utils

Version:

Typescript library for common utility functions and constants

204 lines (187 loc) 9.21 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;// lib/common/utils/logger.utils.ts var LogLevel = /* @__PURE__ */ ((LogLevel2) => { LogLevel2[LogLevel2["Silent"] = -1] = "Silent"; LogLevel2[LogLevel2["Error"] = 0] = "Error"; LogLevel2[LogLevel2["Warn"] = 1] = "Warn"; LogLevel2[LogLevel2["Info"] = 2] = "Info"; LogLevel2[LogLevel2["Debug"] = 3] = "Debug"; LogLevel2[LogLevel2["Trace"] = 4] = "Trace"; return LogLevel2; })(LogLevel || {}); var toLogLevel = (logLevel) => { const _logLevel = logLevel.charAt(0).toUpperCase() + logLevel.slice(1).toLowerCase(); return _nullishCoalesce(LogLevel[_logLevel], () => ( -1)) /* Silent */; }; var TimeStampFormat = { ISO: "ISO", Date: "Date", Time: "Time", Local: "Local" }; var getTimestamp = (scope, format = TimeStampFormat.Time) => { let date; if (format === TimeStampFormat.ISO) date = (/* @__PURE__ */ new Date()).toISOString(); else if (format === TimeStampFormat.Date) date = (/* @__PURE__ */ new Date()).toLocaleDateString().split("T").at(0); else if (format === TimeStampFormat.Local) date = (/* @__PURE__ */ new Date()).toLocaleString(); else date = (/* @__PURE__ */ new Date()).toLocaleTimeString("en", { hour12: false, timeZone: "UTC" }); return scope ? `[${date} - ${scope}]` : `[${date}]`; }; var LoggerColor = { Debug: "#9e9e9e", Info: "#5bd4f9", Warn: "#f2c61a", Error: "#ff5549", Success: "#00ff00" }; var ConsoleFormat = { /** * Formats the value as a string * @see ConsoleFormat */ String: "%s", /** * Formats the value as an integer * @see ConsoleFormat */ Integer: "%i", /** * Formats the value as a floating point value * @see ConsoleFormat */ Float: "%f", /** * Formats the value as an expandable DOM element * @see ConsoleFormat */ DOM: "%o", /** * Formats the value as an expandable JavaScript object * @see ConsoleFormat */ Object: "%O", /** * Applies CSS style rules to the output string as specified by the second parameter * @see ConsoleFormat */ Style: "%c" }; var colorize = (color, ...args) => { if (!_optionalChain([args, 'optionalAccess', _ => _.some, 'call', _2 => _2((arg) => typeof arg === "string")])) return args; let prefix = "%c"; const _args = [`color: ${color}`]; args.forEach((arg, i) => { const space = i ? " " : ""; if (arg instanceof Element) { if (i < args.length - 1) prefix += `${space}${ConsoleFormat.DOM}`; } else if (typeof arg === "object") { if (i < args.length - 1) prefix += `${space}${ConsoleFormat.Object}`; } else if (typeof arg === "string") { prefix += `${space}${ConsoleFormat.String}`; } else if (typeof arg === "number") { prefix += `${space}${ConsoleFormat.Float}`; } _args.push(arg); }); _args.unshift(prefix); return _args; }; var proxyLoggerFilter = { trace: (logLevel) => logLevel >= 4 /* Trace */, debug: (logLevel) => logLevel >= 3 /* Debug */, info: (logLevel) => logLevel >= 2 /* Info */, warn: (logLevel) => logLevel >= 1 /* Warn */, error: (logLevel) => logLevel >= 0 /* Error */ }; var ProxyLogger = (_class = class _ProxyLogger { constructor({ logger = console, debug = false, proxy = proxyLoggerFilter, logLevel = 1 /* Warn */, timeFormat = TimeStampFormat.Time } = {}) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this); this._logger = logger; this._proxy = proxy; this._debug = debug; this._logLevel = logLevel; this._timeFormat = timeFormat; } get timeFormat() { return typeof this._timeFormat === "function" ? this._timeFormat() : this._timeFormat; } get isDebug() { return typeof this._debug === "function" ? this._debug() : this._debug; } get logLevel() { return typeof this._logLevel === "function" ? this._logLevel() : this._logLevel; } set logLevel(level) { if (this._debug) console.debug("[ProxyLogger] - Log level changed:", LogLevel[level]); if (typeof this._logLevel === "function") throw new Error("Cannot set log level when logLevel is a function"); this._logLevel = level; } __init() {this.null = (...args) => { if (this.isDebug) console.debug("[ProxyLogger] - Log suppressed:", ...args); }} get trace() { if (!_optionalChain([this, 'access', _3 => _3._proxy, 'optionalAccess', _4 => _4.trace, 'optionalCall', _5 => _5(this.logLevel)])) return this.null; return this._logger.trace.bind(this._logger); } get debug() { if (!_optionalChain([this, 'access', _6 => _6._proxy, 'optionalAccess', _7 => _7.debug, 'optionalCall', _8 => _8(this.logLevel)])) return this.null; return this._logger.debug.bind(this._logger); } get info() { if (!_optionalChain([this, 'access', _9 => _9._proxy, 'optionalAccess', _10 => _10.info, 'optionalCall', _11 => _11(this.logLevel)])) return this.null; return this._logger.info.bind(this._logger); } get warn() { if (!_optionalChain([this, 'access', _12 => _12._proxy, 'optionalAccess', _13 => _13.warn, 'optionalCall', _14 => _14(this.logLevel)])) return this.null; return this._logger.warn.bind(this._logger); } get error() { if (!_optionalChain([this, 'access', _15 => _15._proxy, 'optionalAccess', _16 => _16.error, 'optionalCall', _17 => _17(this.logLevel)])) return this.null; return this._logger.error.bind(this._logger); } static __initStatic() {this.logger = new _ProxyLogger()} static __initStatic2() {this.timestamp = getTimestamp} static __initStatic3() {this.colorize = exports.colorize = colorize} __init2() {this.color = { debug: (...args) => this.debug(..._ProxyLogger.colorize(LoggerColor.Debug, ...args)), info: (...args) => this.info(..._ProxyLogger.colorize(LoggerColor.Info, ...args)), warn: (...args) => this.warn(..._ProxyLogger.colorize(LoggerColor.Warn, ...args)), error: (...args) => this.error(..._ProxyLogger.colorize(LoggerColor.Error, ...args)), success: (...args) => this.info(..._ProxyLogger.colorize(LoggerColor.Success, ...args)) }} /** * Logger with timestamp but looses stack trace origin */ __init3() {this.time = { debug: (...args) => this.debug(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), info: (...args) => this.info(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), warn: (...args) => this.warn(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), error: (...args) => this.error(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), color: { debug: (...args) => this.color.debug(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), info: (...args) => this.color.info(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), warn: (...args) => this.color.warn(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), error: (...args) => this.color.error(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args), success: (...args) => this.color.success(_ProxyLogger.timestamp(void 0, this.timeFormat), ...args) } }} __init4() {this.scope = (_scope) => ({ debug: (...args) => this.debug(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), info: (...args) => this.info(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), warn: (...args) => this.warn(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), error: (...args) => this.error(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), color: { debug: (...args) => this.color.debug(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), info: (...args) => this.color.info(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), warn: (...args) => this.color.warn(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), error: (...args) => this.color.error(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args), success: (...args) => this.color.success(_ProxyLogger.timestamp(_scope, this.timeFormat), ...args) } })} __init5() {this.colored = {}} }, _class.__initStatic(), _class.__initStatic2(), _class.__initStatic3(), _class); exports.LogLevel = LogLevel; exports.toLogLevel = toLogLevel; exports.TimeStampFormat = TimeStampFormat; exports.getTimestamp = getTimestamp; exports.LoggerColor = LoggerColor; exports.ConsoleFormat = ConsoleFormat; exports.colorize = colorize; exports.proxyLoggerFilter = proxyLoggerFilter; exports.ProxyLogger = ProxyLogger;