Skip to content

Commit

Permalink
Remove sprintf-js dependency (#626)
Browse files Browse the repository at this point in the history
Signed-off-by: jsetton <[email protected]>
  • Loading branch information
jsetton authored Nov 10, 2023
1 parent cd82c28 commit 544fef7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 57 deletions.
25 changes: 6 additions & 19 deletions lambda/alexa/smarthome/unitOfMeasure.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* SPDX-License-Identifier: EPL-2.0
*/

import { sprintf } from 'sprintf-js';
import { Dimension, UnitSymbol, UnitSystem } from '#openhab/constants.js';

/**
Expand Down Expand Up @@ -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;
}
}
}
19 changes: 9 additions & 10 deletions lambda/openhab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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*(?:\.(?<precision>\d+))?(?<specifier>[df])/)?.groups || {};
const value = parseFloat(state);

return specifier === 'd' ? value.toFixed() : precision <= 16 ? value.toFixed(precision) : value.toString();
}

return state;
}
}
43 changes: 16 additions & 27 deletions lambda/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit 544fef7

Please sign in to comment.