nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.
57 lines (56 loc) • 1.51 kB
JavaScript
;
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.$Mass = void 0;
const base_1 = require("./base");
const constants_1 = require("./constants");
class $Mass extends base_1.$BaseConverter {
static #factors = {
microgram: 1e-9,
milligram: 1e-6,
gram: 0.001,
kilogram: 1,
tonne: 1000,
ounce: 0.028349523125,
pound: 0.45359237,
stone: 6.35029318,
'short-ton': 907.18474,
'long-ton': 1016.0469088,
};
constructor(value, unit) {
super(value, unit);
}
#toKilograms() {
return this.value * _a.#factors[this.unit];
}
to(target) {
const base = this.#toKilograms();
return base / _a.#factors[target];
}
toAll() {
const base = this.#toKilograms();
const result = {};
for (const unit of constants_1.UNITS.mass) {
result[unit] = base / _a.#factors[unit];
}
return result;
}
formatTo(target, options) {
const value = this.to(target);
const shortLabels = {
microgram: 'µg',
milligram: 'mg',
gram: 'g',
kilogram: 'kg',
tonne: 't',
ounce: 'oz',
pound: 'lb',
stone: 'st',
'short-ton': 't (US)',
'long-ton': 't (UK)',
};
return this.$formatTo(value, target, shortLabels, options);
}
}
exports.$Mass = $Mass;
_a = $Mass;