UNPKG

@jovian/type-tools

Version:

TypeTools is a Typescript library for providing extensible tooling runtime validations and type helpers.

209 lines 8.1 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClassSettings = exports.dphead = exports.dp4 = exports.dp3 = exports.dp2 = exports.dp = exports.dpa = exports.dpt = exports.ansiColorMap = exports.dpColor = exports.debugLogTail = exports.debugLogHead = exports.isNodeJs = void 0; var stack_util_1 = require("./stack.util"); exports.isNodeJs = (typeof process !== 'undefined') && (process.release.name === 'node'); exports.debugLogHead = "\n-------------------------------- DEBUG_LOG_START --------------------------------\n"; exports.debugLogTail = "\n-------------------------------- DEBUG_LOG_END --------------------------------\n"; var colorWhite = '\x1b[37m%s\x1b[0m'; var colorYellow = '\x1b[33m%s\x1b[0m'; var colorGray = '\x1b[90m%s\x1b[0m'; var colorEnd = "\u001B[0m"; exports.dpColor = { colorEnabled: false, color: colorWhite, metaColorEnabled: exports.isNodeJs, metaColor: colorGray, end: colorEnd, }; exports.ansiColorMap = { white: { fg: 37, bg: 47, normal: colorWhite }, yellow: { fg: 33, bg: 43, normal: colorYellow }, }; function dpDisplay(source, color, sectioned, args) { var _a; var activeColor; if (color && !activeColor) { activeColor = (_a = exports.ansiColorMap === null || exports.ansiColorMap === void 0 ? void 0 : exports.ansiColorMap[color]) === null || _a === void 0 ? void 0 : _a.normal; } if (!activeColor) { activeColor = exports.dpColor.color; } sectioned ? console.group() : null; if (!stack_util_1.StackUtilSourceMapEnv.isBrowser) { exports.dpColor.metaColorEnabled ? console.log(exports.dpColor.metaColor, "".concat(sectioned ? exports.debugLogHead : '', "src: ").concat(source).concat(sectioned ? '\n' : ''), exports.dpColor.end) : console.log("".concat(sectioned ? exports.debugLogHead : '', "src: ").concat(source).concat(sectioned ? '\n' : '')); console.group(); } else { console.group("".concat(sectioned ? exports.debugLogHead : '', "src: ").concat(source).concat(sectioned ? '\n' : '')); } exports.dpColor.colorEnabled ? console.log.apply(console, __spreadArray(__spreadArray([activeColor], args, false), [exports.dpColor.end], false)) : console.log.apply(console, args); console.groupEnd(); exports.dpColor.metaColorEnabled ? console.log(exports.dpColor.metaColor, "".concat(sectioned ? exports.debugLogTail : ''), exports.dpColor.end) : sectioned ? console.log(exports.debugLogTail) : null; sectioned ? console.groupEnd() : null; } var dptRegistry = {}; function dpt(name, iterationCount) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; } if (!name) { name = 'unnamed'; } var t = Date.now(); if (!dptRegistry[name]) { dptRegistry[name] = { start: t, iterationCount: iterationCount }; args.unshift("dpt timer '".concat(name, "' started")); dpDisplay((0, stack_util_1.spotfull)(new Error, 2), null, false, args); } else { var reg = dptRegistry[name]; var startT = reg.start; var iterationCount_1 = reg.iterationCount; delete dptRegistry[name]; var delta = t - startT; var perItemDuration = iterationCount_1 ? " (".concat(delta * 1000 / iterationCount_1, " us/count)") : ''; args.unshift("dpt timer '".concat(name, "' ended; total ").concat(delta, " ms taken").concat(perItemDuration)); dpDisplay((0, stack_util_1.spotfull)(new Error, 2), null, false, args); } } exports.dpt = dpt; function dpa() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } dpDisplay(new Error().stack, null, false, args); } exports.dpa = dpa; function dp() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } dpDisplay((0, stack_util_1.spotfull)(new Error, 2), null, false, args); } exports.dp = dp; function dp2() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } dpDisplay((0, stack_util_1.spotfull)(new Error, 3), null, false, args); } exports.dp2 = dp2; function dp3() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } dpDisplay((0, stack_util_1.spotfull)(new Error, 4), null, false, args); } exports.dp3 = dp3; function dp4() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } dpDisplay((0, stack_util_1.spotfull)(new Error, 5), null, false, args); } exports.dp4 = dp4; function dphead(headerSectionName) { var sectionDivider = '================================================================'; dpDisplay((0, stack_util_1.spotfull)(new Error, 2), null, false, [ colorYellow, "".concat(sectionDivider, "\n\n ").concat(headerSectionName, "\n\n").concat(sectionDivider), colorEnd, ]); } exports.dphead = dphead; var displayClassSettings = false; var showClassSettingsStack = false; var withinClassSettingsCall = false; var classSettingsSources = []; exports.ClassSettings = { display: function () { displayClassSettings = true; var e = new Error("[ClassSetting] display"); showClassSettingsStack ? dp2(e) : dp2(e.message); }, showStack: function () { displayClassSettings = true; showClassSettingsStack = true; var e = new Error("[ClassSettings] showStack"); classSettingsSources.push(e); dp2(e); }, set: function (fn) { if (withinClassSettingsCall) { throw new Error("[ClassSettings] Cannot ClassSettings.set within ClassSettings.set"); } withinClassSettingsCall = true; if (displayClassSettings) { var e = new Error("[ClassSettings] modified: ".concat(fn)); classSettingsSources.push(e); showClassSettingsStack ? dp2(e) : dp2(e.message); } var settingError; try { fn(); } catch (e) { settingError = e; } withinClassSettingsCall = false; if (settingError) { throw settingError; } }, protect: function (type) { var settings = []; for (var _i = 1; _i < arguments.length; _i++) { settings[_i - 1] = arguments[_i]; } var protectedList = []; if (settings.length === 0) { settings = Object.keys(type); } for (var _a = 0, settings_1 = settings; _a < settings_1.length; _a++) { var setting = settings_1[_a]; var settingValue = type[setting]; if (settingValue && settingValue.call && settingValue.apply) { continue; } protectSetting(type, setting); protectedList.push(setting); } return protectedList; } }; Object.freeze(exports.ClassSettings); function protectSetting(type, setting) { var settingValue = type[setting]; var typename = type.name; var settingPath = typename ? "".concat(typename, ".").concat(String(setting)) : setting; Object.defineProperty(type, setting, { get: function () { return settingValue; }, set: function (newValue) { if (!withinClassSettingsCall) { throw new Error("[ClassSettings] Cannot change protected config '".concat(String(settingPath), "' outside ClassSettings.set")); } settingValue = newValue; }, }); } //# sourceMappingURL=env.util.js.map