1.0.8 • Published 8 months ago
@pgds/utils v1.0.8
pgds-utils
This utils package can be used for various purposes. It is a collection of utility functions that can be used in any project withing the PGDS platform.
Currently it contains the following functions:
- convertValue
- convertValueByType
- calculateAbsoluteHumidity
Dependencies
You need to run node version 22 or higher.
Publishing
Prerequisites:
- You have to be part of the @hiotlabs organization on npm
- You have to be logged in to npm
Then, in root, run:
// Check out any untracked/ignored files (e.g. previously built files)
yarn clean
// Install dependencies
yarn install
// Build
yarn build
// Make sure tests pass
yarn test
// Publish
npm publish --access public
// Or pack (for testing)
npm packAdding new directories to the project (@pgds/utils)
- Create folder
- Add index.ts file in the folder
- This file will be responsible for exporting functions.
- Add one entry for
.js,.js.mapand.d.tsfor the folder to package.json'sfilesarray, e.g.utils/**/*.js,utils/**/*.js.mapandutils/**/*.d.ts
"files": [
"**/*.d.ts",
"./index.js",
"lib/**/*.js",
"lib/**/*.js.map",
"lib/**/*.d.ts",
"errors/**/*.js",
"errors/**/*.js.map",
"errors/**/*.d.ts",
"constants/**/*.js",
"constants/**/*.js.map",
"constants/**/*.d.ts",
"utils/**/*.js",
"utils/**/*.js.map",
"utils/**/*.d.ts"
],Usage
Install pgds-utils as an npm module and save it to your package.json file as a development dependency:
npm install @pdgs/utils
Unit conversion
Converts a value between the given units or by type and measurement system.
Functions
import { Unit, ConversionType, MeasurementSystem, FixedUnitMeasurementType } from "@pgds/utils/constants";
import { convertValue, convertValueByType, getDisplayValueForMeasurementSystem } from "@pgds/utils/unit-conversion";
convertValue(value: number, from: Unit, to: Unit): number;
convertValueByType(value: number, type: ConversionType, from: MeasurementSystem, to: MeasurementSystem): number;
// returns the rounded value as a string with the unit appended
getDisplayValue(value: number, unit: Unit | string, measurementSystem: MeasurementSystem): string;
getUnitByTypeAndMeasurementSystem(type: ConversionType, measurementSystem: MeasurementSystem): string;- Unit: The unit to convert from/to. The available units are defined and can be exported from the
Unitenum. Supported right now:celsius/fahrenheit,gramsPerCubicMeter/grainsPerCubicFootandgramsPerKilogram/grainsPerPound. - ConversionType: The type of conversion to perform. The available conversion types are defined and can be exported from the
ConversionTypeenum. Right now that istemperature,absoluteHumidity. - MeasurementSystem: The measurement system to convert from/to. The available measurement systems are defined and can be exported from the
MeasurementSystemenum. Supported right now:metricandimperial.
Calculations
Calculates the absolute humidity based on the given temperature and relative humidity.
Functions
import { calculateAbsoluteHumidity } from "@pgds/utils/calculations";
calculateAbsoluteHumidity(humidity: number, temperature: number): number;
calculateDewPoint( humidity: number, temperature: number): number;Logging
Utils for logging in a consistent manner. Will pick up the request id from the request context (through parent's @pgds/api-interface dependency) if available.
// app.ts
import { setupLogger } from "@pgds/utils/logging";
setupLogger("things-api");
// Anywhere in your app
import { logger } from "@pgds/utils/logging";
logger.info("Hello world", { hello: "world" });Output will be:
2023-10-01T12:00:00.000Z INFO things-api: Hello world { hello: "world" } (req_id=1234567890)configs
LOG_LEVEL- see https://github.com/trentm/node-bunyan?tab=readme-ov-file#levels. Default isinfo, meaning it will log everything frominfoand above (i.e.info,warn,error,fatal).