UNPKG

3.37 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.formatOutgoingCharacteristicValue = formatOutgoingCharacteristicValue;
4exports.isNumericFormat = isNumericFormat;
5exports.isUnsignedNumericFormat = isUnsignedNumericFormat;
6exports.isIntegerNumericFormat = isIntegerNumericFormat;
7exports.numericLowerBound = numericLowerBound;
8exports.numericUpperBound = numericUpperBound;
9function formatOutgoingCharacteristicValue(value, props) {
10 if (typeof value === "boolean") {
11 return value ? 1 : 0;
12 }
13 else if (typeof value === "number") {
14 if (!props.minStep || props.minStep >= 1) {
15 return value;
16 }
17 const base = props.minValue ?? 0;
18 const inverse = 1 / props.minStep;
19 return Math.round(((Math.round((value - base) * inverse) / inverse) + base) * 10000) / 10000;
20 }
21 return value;
22}
23/**
24 * @group Utils
25 */
26function isNumericFormat(format) {
27 switch (format) {
28 case "int" /* Formats.INT */:
29 case "float" /* Formats.FLOAT */:
30 case "uint8" /* Formats.UINT8 */:
31 case "uint16" /* Formats.UINT16 */:
32 case "uint32" /* Formats.UINT32 */:
33 case "uint64" /* Formats.UINT64 */:
34 return true;
35 default:
36 return false;
37 }
38}
39/**
40 * @group Utils
41 */
42function isUnsignedNumericFormat(format) {
43 switch (format) {
44 case "uint8" /* Formats.UINT8 */:
45 case "uint16" /* Formats.UINT16 */:
46 case "uint32" /* Formats.UINT32 */:
47 case "uint64" /* Formats.UINT64 */:
48 return true;
49 default:
50 return false;
51 }
52}
53/**
54 * @group Utils
55 */
56function isIntegerNumericFormat(format) {
57 switch (format) {
58 case "int" /* Formats.INT */:
59 case "uint8" /* Formats.UINT8 */:
60 case "uint16" /* Formats.UINT16 */:
61 case "uint32" /* Formats.UINT32 */:
62 case "uint64" /* Formats.UINT64 */:
63 return true;
64 default:
65 return false;
66 }
67}
68/**
69 * @group Utils
70 */
71function numericLowerBound(format) {
72 switch (format) {
73 case "int" /* Formats.INT */:
74 return -2147483648;
75 case "float" /* Formats.FLOAT */:
76 return -Number.MAX_VALUE;
77 case "uint8" /* Formats.UINT8 */:
78 case "uint16" /* Formats.UINT16 */:
79 case "uint32" /* Formats.UINT32 */:
80 case "uint64" /* Formats.UINT64 */:
81 return 0;
82 default:
83 throw new Error("Unable to determine numeric lower bound for " + format);
84 }
85}
86/**
87 * @group Utils
88 */
89function numericUpperBound(format) {
90 switch (format) {
91 case "int" /* Formats.INT */:
92 return 2147483647;
93 case "float" /* Formats.FLOAT */:
94 return Number.MAX_VALUE;
95 case "uint8" /* Formats.UINT8 */:
96 return 255;
97 case "uint16" /* Formats.UINT16 */:
98 return 65535;
99 case "uint32" /* Formats.UINT32 */:
100 return 4294967295;
101 case "uint64" /* Formats.UINT64 */:
102 // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
103 return 18446744073709551615; // don't get fooled, javascript uses 18446744073709552000 here
104 default:
105 throw new Error("Unable to determine numeric lower bound for " + format);
106 }
107}
108//# sourceMappingURL=request-util.js.map
\No newline at end of file