From 544fef788747f29ccb8cfb325b3b299eacea0173 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Fri, 10 Nov 2023 16:43:49 -0500 Subject: [PATCH] Remove sprintf-js dependency (#626) Signed-off-by: jsetton --- lambda/alexa/smarthome/unitOfMeasure.js | 25 ++++---------- lambda/openhab/index.js | 19 ++++++----- lambda/package-lock.json | 43 +++++++++---------------- lambda/package.json | 1 - 4 files changed, 31 insertions(+), 57 deletions(-) diff --git a/lambda/alexa/smarthome/unitOfMeasure.js b/lambda/alexa/smarthome/unitOfMeasure.js index c720f28a..1e3e43eb 100644 --- a/lambda/alexa/smarthome/unitOfMeasure.js +++ b/lambda/alexa/smarthome/unitOfMeasure.js @@ -11,7 +11,6 @@ * SPDX-License-Identifier: EPL-2.0 */ -import { sprintf } from 'sprintf-js'; import { Dimension, UnitSymbol, UnitSystem } from '#openhab/constants.js'; /** @@ -492,28 +491,16 @@ class UnitsOfMeasure { * @return {Object} */ static getUnitOfMeasure({ dimension, unitSymbol, statePresentation, system = UnitSystem.METRIC }) { - // Determine symbol using item unit symbol or item state presentation pattern - const symbol = unitSymbol ?? this.getUnitSymbol(statePresentation); + // Determine symbol using item unit symbol or matching item state presentation with supported list + const symbol = + unitSymbol ?? + Object.values(UnitSymbol).find((symbol) => + new RegExp(`%\\d*(?:\\.\\d+)?[df]\\s*[%]?${symbol}$`).test(statePresentation) + ); // Return unit of measure using symbol/dimension or fallback to default value using dimension/system return ( this.#UOMS.find((uom) => uom.symbol === symbol && (!dimension || uom.dimension === dimension)) || this.#UOMS.find((uom) => uom.default && uom.dimension === dimension && (!uom.system || uom.system === system)) ); } - - /** - * Returns unit symbol based on given item state presentation pattern - * @param {String} pattern - * @return {String} - */ - static getUnitSymbol(pattern) { - try { - // Use a random number to format the item state presentation - const presentation = sprintf(pattern, Math.random()); - // Return symbol based on the supported list matching the formatted item state presentation - return Object.values(UnitSymbol).find((symbol) => new RegExp(`\\d\\s*${symbol}$`).test(presentation)); - } catch { - return undefined; - } - } } diff --git a/lambda/openhab/index.js b/lambda/openhab/index.js index 3af2d91e..937da26f 100644 --- a/lambda/openhab/index.js +++ b/lambda/openhab/index.js @@ -14,7 +14,6 @@ import fs from 'node:fs'; import axios from 'axios'; import { HttpsAgent } from 'agentkeepalive'; -import { sprintf } from 'sprintf-js'; import { validate as uuidValidate } from 'uuid'; import { ItemType, ItemValue } from './constants.js'; @@ -243,17 +242,17 @@ export default class OpenHAB { * @return {String} */ static formatItemState(item) { - const format = item.stateDescription?.pattern?.match(/%(?:[.0]\d+)?[df]/)?.[0] || '%f'; const state = item.state; - const type = item.groupType || item.type; + const type = (item.groupType || item.type).split(':')[0]; - switch (type.split(':')[0]) { - case ItemType.DIMMER: - case ItemType.NUMBER: - case ItemType.ROLLERSHUTTER: - return sprintf(format, parseFloat(state)); - default: - return state; + if (type === ItemType.DIMMER || type === ItemType.NUMBER || type === ItemType.ROLLERSHUTTER) { + const { precision, specifier } = + item.stateDescription?.pattern?.match(/%\d*(?:\.(?\d+))?(?[df])/)?.groups || {}; + const value = parseFloat(state); + + return specifier === 'd' ? value.toFixed() : precision <= 16 ? value.toFixed(precision) : value.toString(); } + + return state; } } diff --git a/lambda/package-lock.json b/lambda/package-lock.json index c31f63a5..a1ce9887 100644 --- a/lambda/package-lock.json +++ b/lambda/package-lock.json @@ -11,7 +11,6 @@ "dependencies": { "agentkeepalive": "^4.5.0", "axios": "^1.5.1", - "sprintf-js": "^1.1.3", "uuid": "^9.0.1", "winston": "^3.10.0" }, @@ -2841,11 +2840,6 @@ "node": ">=0.3.1" } }, - "node_modules/sprintf-js": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.3.tgz", - "integrity": "sha512-Oo+0REFV59/rz3gfJNKQiBlwfHaSESl1pcGyABQsnnIfWOFt6JNj5gCog2U6MLZ//IGYD+nA8nI+mTShREReaA==" - }, "node_modules/stack-trace": { "version": "0.0.10", "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", @@ -2854,14 +2848,6 @@ "node": "*" } }, - "node_modules/string_decoder": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", - "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", - "dependencies": { - "safe-buffer": "~5.2.0" - } - }, "node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", @@ -2876,6 +2862,14 @@ "node": ">=8" } }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, "node_modules/strip-ansi": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", @@ -5296,24 +5290,11 @@ } } }, - "sprintf-js": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.3.tgz", - "integrity": "sha512-Oo+0REFV59/rz3gfJNKQiBlwfHaSESl1pcGyABQsnnIfWOFt6JNj5gCog2U6MLZ//IGYD+nA8nI+mTShREReaA==" - }, "stack-trace": { "version": "0.0.10", "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", "integrity": "sha512-KGzahc7puUKkzyMt+IqAep+TVNbKP+k2Lmwhub39m1AsTSkaDutx56aDCo+HLDzf/D26BIHTJWNiTG1KAJiQCg==" }, - "string_decoder": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", - "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", - "requires": { - "safe-buffer": "~5.2.0" - } - }, "string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", @@ -5325,6 +5306,14 @@ "strip-ansi": "^6.0.1" } }, + "string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "requires": { + "safe-buffer": "~5.2.0" + } + }, "strip-ansi": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", diff --git a/lambda/package.json b/lambda/package.json index f0bba7b5..8170e309 100644 --- a/lambda/package.json +++ b/lambda/package.json @@ -29,7 +29,6 @@ "dependencies": { "agentkeepalive": "^4.5.0", "axios": "^1.5.1", - "sprintf-js": "^1.1.3", "uuid": "^9.0.1", "winston": "^3.10.0" },