UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

76 lines 1.97 kB
import { __decorate, __metadata } from "tslib"; import { SerializableMember, SerializableObject } from '../../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; } }; __decorate([SerializableMember({ name: 'value' }), __metadata("design:type", Object)], UnitValue.prototype, "_value", void 0); __decorate([SerializableMember({ name: 'unit' }), __metadata("design:type", Object)], UnitValue.prototype, "_unit", void 0); UnitValue = __decorate([SerializableObject(), __metadata("design:paramtypes", [Object, Object])], UnitValue); export { UnitValue };