UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

89 lines 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UnitValue = void 0; const tslib_1 = require("tslib"); const decorators_1 = require("../../data/decorators"); /** * Unit value * * ## Usage * ### Creation * ```typescript * const value = new UnitValue(5, LengthUnit.METER); * ``` * * ### Conversion * ```typescript * const value = new UnitValue(5, LengthUnit.METER); * const converted = value.to(LengthUnit.CENTIMETER); * ``` * @category Unit */ let UnitValue = class UnitValue { constructor(value, unit) { this._value = value; this._unit = unit; } /** * Convert the value to another unit * @param {Unit} unit Target unit * @returns {UnitValue} Converted value */ to(unit) { if (!unit) { throw new Error(`${this.constructor.name} does not have a unit to convert from!`); } const result = this.unit.convert(this.valueOf(), unit); return new this.constructor(result, unit); } /** * Unit this value is in * @returns {Unit} Unit this value is in */ get unit() { return this._unit; } /** * Returns a string representation of an object. * @returns {string} Unit value as string */ toString() { const value = this.valueOf(); return value ? value.toString() : undefined; } /** * Returns the primitive value * @returns {number} Primitive value */ valueOf() { return this._value; } setValue(value) { this._value = value; return this; } clone() { const result = new this.constructor(); result._value = this._value; result._unit = this._unit; return result; } }; exports.UnitValue = UnitValue; tslib_1.__decorate([ (0, decorators_1.SerializableMember)({ name: 'value', }), tslib_1.__metadata("design:type", Object) ], UnitValue.prototype, "_value", void 0); tslib_1.__decorate([ (0, decorators_1.SerializableMember)({ name: 'unit', }), tslib_1.__metadata("design:type", Object) ], UnitValue.prototype, "_unit", void 0); exports.UnitValue = UnitValue = tslib_1.__decorate([ (0, decorators_1.SerializableObject)(), tslib_1.__metadata("design:paramtypes", [Object, Object]) ], UnitValue); //# sourceMappingURL=UnitValue.js.map