nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.
53 lines (52 loc) • 1.38 kB
JavaScript
var _a;
import { $BaseConverter } from './base.js';
import { UNITS } from './constants.js';
export class $Mass extends $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 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);
}
}
_a = $Mass;