From c87ac2f0434e4ed3e23b21592c5d080b83ea0f34 Mon Sep 17 00:00:00 2001 From: Matt Visnovsky Date: Fri, 3 May 2024 11:39:14 -0600 Subject: [PATCH] Move getKey() to util.ts --- server/monitor-types/snmp.js | 12 +----------- src/util.js | 6 +++++- src/util.ts | 9 +++++++++ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/server/monitor-types/snmp.js b/server/monitor-types/snmp.js index 5bdd8a1ab..b14c85953 100644 --- a/server/monitor-types/snmp.js +++ b/server/monitor-types/snmp.js @@ -1,5 +1,5 @@ const { MonitorType } = require("./monitor-type"); -const { UP, DOWN, log } = require("../../src/util"); +const { UP, DOWN, log, getKey } = require("../../src/util"); const snmp = require("net-snmp"); class SNMPMonitorType extends MonitorType { @@ -17,16 +17,6 @@ class SNMPMonitorType extends MonitorType { version: getKey(snmp.Version, monitor.snmpVersion) || snmp.Version2c, }; - /** - * Retrieves the key from the provided object corresponding to the given value. - * @param {object} obj - The object to search. - * @param {*} value - The value to search for. - * @returns {string|null} - The key associated with the value, or null if not found. - */ - function getKey(obj, value) { - return Object.keys(obj).find(key => obj[key] === value) || null; - } - try { const session = snmp.createSession(monitor.hostname, monitor.snmpCommunityString, options); diff --git a/src/util.js b/src/util.js index b3706ddbf..33b3077aa 100644 --- a/src/util.js +++ b/src/util.js @@ -11,7 +11,7 @@ var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.sleep = exports.flipStatus = exports.badgeConstants = exports.CONSOLE_STYLE_BgGray = exports.CONSOLE_STYLE_BgWhite = exports.CONSOLE_STYLE_BgCyan = exports.CONSOLE_STYLE_BgMagenta = exports.CONSOLE_STYLE_BgBlue = exports.CONSOLE_STYLE_BgYellow = exports.CONSOLE_STYLE_BgGreen = exports.CONSOLE_STYLE_BgRed = exports.CONSOLE_STYLE_BgBlack = exports.CONSOLE_STYLE_FgPink = exports.CONSOLE_STYLE_FgBrown = exports.CONSOLE_STYLE_FgViolet = exports.CONSOLE_STYLE_FgLightBlue = exports.CONSOLE_STYLE_FgLightGreen = exports.CONSOLE_STYLE_FgOrange = exports.CONSOLE_STYLE_FgGray = exports.CONSOLE_STYLE_FgWhite = exports.CONSOLE_STYLE_FgCyan = exports.CONSOLE_STYLE_FgMagenta = exports.CONSOLE_STYLE_FgBlue = exports.CONSOLE_STYLE_FgYellow = exports.CONSOLE_STYLE_FgGreen = exports.CONSOLE_STYLE_FgRed = exports.CONSOLE_STYLE_FgBlack = exports.CONSOLE_STYLE_Hidden = exports.CONSOLE_STYLE_Reverse = exports.CONSOLE_STYLE_Blink = exports.CONSOLE_STYLE_Underscore = exports.CONSOLE_STYLE_Dim = exports.CONSOLE_STYLE_Bright = exports.CONSOLE_STYLE_Reset = exports.MIN_INTERVAL_SECOND = exports.MAX_INTERVAL_SECOND = exports.SQL_DATETIME_FORMAT_WITHOUT_SECOND = exports.SQL_DATETIME_FORMAT = exports.SQL_DATE_FORMAT = exports.STATUS_PAGE_MAINTENANCE = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.MAINTENANCE = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isNode = exports.isDev = void 0; -exports.intHash = exports.localToUTC = exports.utcToLocal = exports.utcToISODateTime = exports.isoToUTCDateTime = exports.parseTimeFromTimeObject = exports.parseTimeObject = exports.getMaintenanceRelativeURL = exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.log = exports.debug = exports.ucfirst = void 0; +exports.getKey = exports.intHash = exports.localToUTC = exports.utcToLocal = exports.utcToISODateTime = exports.isoToUTCDateTime = exports.parseTimeFromTimeObject = exports.parseTimeObject = exports.getMaintenanceRelativeURL = exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.log = exports.debug = exports.ucfirst = void 0; const dayjs = require("dayjs"); exports.isDev = process.env.NODE_ENV === "development"; exports.isNode = typeof process !== "undefined" && ((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node); @@ -396,3 +396,7 @@ function intHash(str, length = 10) { return (hash % length + length) % length; } exports.intHash = intHash; +function getKey(obj, value) { + return Object.keys(obj).find(key => obj[key] === value) || null; +} +exports.getKey = getKey; diff --git a/src/util.ts b/src/util.ts index b4a52f508..3b7a091ae 100644 --- a/src/util.ts +++ b/src/util.ts @@ -643,3 +643,12 @@ export function intHash(str : string, length = 10) : number { return (hash % length + length) % length; // Ensure the result is non-negative } +/** + * Retrieves the key from the provided object corresponding to the given value. + * @param {object} obj - The object to search. + * @param {*} value - The value to search for. + * @returns {string|null} - The key associated with the value, or null if not found. + */ +export function getKey(obj: any, value: string) { + return Object.keys(obj).find(key => obj[key] === value) || null; +}