1.0.8 • Published 8 months ago

@pgds/utils v1.0.8

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

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 pack

Adding 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.map and .d.ts for the folder to package.json's files array, e.g. utils/**/*.js, utils/**/*.js.map and utils/**/*.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 Unit enum. Supported right now: celsius / fahrenheit, gramsPerCubicMeter / grainsPerCubicFoot and gramsPerKilogram / grainsPerPound.
  • ConversionType: The type of conversion to perform. The available conversion types are defined and can be exported from the ConversionType enum. Right now that is temperature, absoluteHumidity.
  • MeasurementSystem: The measurement system to convert from/to. The available measurement systems are defined and can be exported from the MeasurementSystem enum. Supported right now: metric and imperial.

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

1.0.8

8 months ago

1.0.7

8 months ago

1.0.6

8 months ago

1.0.5

9 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago