measure-convert
Version:
JS/TS package for managing units of measurement. Convert, add, subtract, multiply, divide, and compare units of measurement.
118 lines (117 loc) • 5.22 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Measurement = void 0;
const UnitAngle_1 = require("./units/UnitAngle");
const UnitFuelEfficiency_1 = require("./units/UnitFuelEfficiency");
const UnitTemperature_1 = require("./units/UnitTemperature");
class Measurement {
constructor(value, unit) {
this.value = value;
this.unit = unit;
}
// Adjusted to handle specialized conversions
converted(targetUnit) {
if (this.unit.constructor !== targetUnit.constructor) {
throw new Error(`Cannot convert from ${this.unit.name} (${this.unit.constructor.name}) to ${targetUnit.name} (${targetUnit.constructor.name}): incompatible unit types.`);
}
// Handle specialized conversions if necessary
if (this.unit instanceof UnitFuelEfficiency_1.UnitFuelEfficiency && targetUnit instanceof UnitFuelEfficiency_1.UnitFuelEfficiency) {
const convertedValue = this.unit.convert(this.value, targetUnit);
return new Measurement(convertedValue, targetUnit);
}
if (this.unit instanceof UnitTemperature_1.UnitTemperature && targetUnit instanceof UnitTemperature_1.UnitTemperature) {
const convertedValue = this.unit.convert(this.value, targetUnit);
return new Measurement(convertedValue, targetUnit);
}
// Default linear conversion for all other units
const conversionFactor = this.unit.baseUnitConversionFactor / targetUnit.baseUnitConversionFactor;
return new Measurement(this.value * conversionFactor, targetUnit);
}
// Instance method for addition
add(other) {
return Measurement.add(this, other);
}
// Static method for addition
static add(measurement1, measurement2) {
if (measurement1.unit === measurement2.unit) {
return new Measurement(measurement1.value + measurement2.value, measurement1.unit);
}
else {
const convertedMeasurement2 = measurement2.converted(measurement1.unit);
return new Measurement(measurement1.value + convertedMeasurement2.value, measurement1.unit);
}
}
// Instance method for subtraction
subtract(other) {
return Measurement.subtract(this, other);
}
// Static method for subtraction
static subtract(measurement1, measurement2) {
if (measurement1.unit === measurement2.unit) {
return new Measurement(measurement1.value - measurement2.value, measurement1.unit);
}
else {
const convertedMeasurement2 = measurement2.converted(measurement1.unit);
return new Measurement(measurement1.value - convertedMeasurement2.value, measurement1.unit);
}
}
// Equality check
equals(other) {
return Measurement.equals(this, other);
}
static equals(measurement1, measurement2) {
const convertedMeasurement2 = measurement2.converted(measurement1.unit);
return measurement1.value === convertedMeasurement2.value;
}
closeTo(other, tolerance) {
return Measurement.closeTo(this, other, tolerance);
}
static closeTo(measurement1, measurement2, tolerance) {
const convertedMeasurement2 = measurement2.converted(measurement1.unit);
if (tolerance < 0) {
throw new Error("Tolerance must be a non-negative number.");
}
return Math.abs(measurement1.value - convertedMeasurement2.value) <= tolerance;
}
// Comparison methods implemented similarly
greaterThan(other) {
return Measurement.greaterThan(this, other);
}
static greaterThan(measurement1, measurement2) {
const convertedMeasurement2 = measurement2.converted(measurement1.unit);
return measurement1.value > convertedMeasurement2.value;
}
lessThan(other) {
return Measurement.lessThan(this, other);
}
static lessThan(measurement1, measurement2) {
const convertedMeasurement2 = measurement2.converted(measurement1.unit);
return measurement1.value < convertedMeasurement2.value;
}
greaterThanOrEqual(other) {
return Measurement.greaterThanOrEqual(this, other);
}
static greaterThanOrEqual(measurement1, measurement2) {
const convertedMeasurement2 = measurement2.converted(measurement1.unit);
return measurement1.value >= convertedMeasurement2.value;
}
lessThanOrEqual(other) {
return Measurement.lessThanOrEqual(this, other);
}
static lessThanOrEqual(measurement1, measurement2) {
const convertedMeasurement2 = measurement2.converted(measurement1.unit);
return measurement1.value <= convertedMeasurement2.value;
}
// Customized short label method for handling degrees specifically
get shortLabel() {
// Check if the unit is 'degrees'
if (this.unit === UnitAngle_1.UnitAngle.degrees) {
return `${this.value}${this.unit.symbol}`; // No space for degrees
}
return `${this.value} ${this.unit.symbol}`; // Default formatting for others
}
get longLabel() {
return `${this.value} ${this.unit.name}`;
}
}
exports.Measurement = Measurement;