@openhps/core
Version:
Open Hybrid Positioning System - Core component
64 lines • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DerivedUnit = void 0;
const tslib_1 = require("tslib");
const Unit_1 = require("./Unit");
const decorators_1 = require("../../data/decorators");
/**
* Derived Unit
* @category Unit
*/
let DerivedUnit = class DerivedUnit extends Unit_1.Unit {
constructor() {
super(...arguments);
this._units = new Map();
this._unitPower = new Map();
}
addUnit(unit, power) {
if (this._units.has(unit.baseName)) {
throw new Error(`A unit with base name '${unit.baseName}' already exists for this unit!`);
}
this._units.set(unit.baseName, unit);
this._unitPower.set(unit.baseName, power);
return this;
}
swap(subunits, options) {
if (Unit_1.Unit.UNITS.has(options.name)) {
return Unit_1.Unit.UNITS.get(options.name);
}
const UnitConstructor = Object.getPrototypeOf(this).constructor;
const unit = new UnitConstructor();
unit._name = options.name;
unit._baseName = this.baseName;
unit._aliases = options.aliases ? options.aliases : [];
const definition = {
unit: this.name,
toUnit: undefined,
fromUnit: undefined,
};
subunits.forEach((subunit) => {
const currentUnit = this._units.get(subunit.baseName);
const unitPower = this._unitPower.get(subunit.baseName);
const newDefinition = subunit.createDefinition(currentUnit);
const newToFn = (value) => Math.pow(newDefinition.toUnit(value), unitPower);
const newFromFn = (value) => Math.pow(newDefinition.fromUnit(value), unitPower);
const existingToFn = definition.toUnit;
const existingFromFn = definition.fromUnit;
if (existingToFn && existingFromFn) {
definition.toUnit = (value) => existingToFn(newToFn(value));
definition.fromUnit = (value) => existingFromFn(newFromFn(value));
}
else {
definition.toUnit = newToFn;
definition.fromUnit = newFromFn;
}
});
unit._definitions.set(this.name, definition);
return Unit_1.Unit.registerUnit(unit);
}
};
exports.DerivedUnit = DerivedUnit;
exports.DerivedUnit = DerivedUnit = tslib_1.__decorate([
(0, decorators_1.SerializableObject)()
], DerivedUnit);
//# sourceMappingURL=DerivedUnit.js.map