any-unit-converter
Version:
A unit converter package allows users to convert values from one unit of measurement to another. It is designed to provide a convenient and easy-to-use solution for converting a wide range of units across various categories.
94 lines (93 loc) • 5.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convert = void 0;
var unitConfig_1 = require("./assets/unitConfig");
function isDecimalZero(num) {
var decimalRegex = /\.0+$|\.0+e/;
return decimalRegex.test(num.toString());
}
function convertUnit(value, fromUnit, toUnit, unitType) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
if (!((_b = (_a = unitConfig_1.unitConfig[unitType !== null ? unitType : '']) === null || _a === void 0 ? void 0 : _a.unitOption) === null || _b === void 0 ? void 0 : _b.find(function (res) { return res.shortCode === fromUnit; })) ||
!((_d = (_c = unitConfig_1.unitConfig[unitType !== null ? unitType : '']) === null || _c === void 0 ? void 0 : _c.unitOption) === null || _d === void 0 ? void 0 : _d.find(function (res) { return res.shortCode === toUnit; }))) {
throw Error('invalid unit');
}
var fromValue;
if (typeof ((_g = (_f = (_e = unitConfig_1.unitConfig[unitType !== null ? unitType : '']) === null || _e === void 0 ? void 0 : _e.unitOption) === null || _f === void 0 ? void 0 : _f.find(function (res) { return res.shortCode === fromUnit; })) === null || _g === void 0 ? void 0 : _g.conversionFactor) === 'object') {
fromValue = (_l = Object((_k = (_j = (_h = unitConfig_1.unitConfig[unitType !== null ? unitType : '']) === null || _h === void 0 ? void 0 : _h.unitOption) === null || _j === void 0 ? void 0 : _j.find(function (res) { return res.shortCode === fromUnit; })) === null || _k === void 0 ? void 0 : _k.conversionFactor)) === null || _l === void 0 ? void 0 : _l.fromBase(value);
}
else {
fromValue = (_p = (_o = (_m = unitConfig_1.unitConfig[unitType !== null ? unitType : '']) === null || _m === void 0 ? void 0 : _m.unitOption) === null || _o === void 0 ? void 0 : _o.find(function (res) { return res.shortCode === fromUnit; })) === null || _p === void 0 ? void 0 : _p.conversionFactor;
}
var toValue;
if (typeof ((_s = (_r = (_q = unitConfig_1.unitConfig[unitType !== null ? unitType : '']) === null || _q === void 0 ? void 0 : _q.unitOption) === null || _r === void 0 ? void 0 : _r.find(function (res) { return res.shortCode === toUnit; })) === null || _s === void 0 ? void 0 : _s.conversionFactor) === 'object') {
toValue = (_w = Object((_v = (_u = (_t = unitConfig_1.unitConfig[unitType !== null ? unitType : '']) === null || _t === void 0 ? void 0 : _t.unitOption) === null || _u === void 0 ? void 0 : _u.find(function (res) { return res.shortCode === toUnit; })) === null || _v === void 0 ? void 0 : _v.conversionFactor)) === null || _w === void 0 ? void 0 : _w.toBase(fromValue);
}
else {
toValue = (_z = (_y = (_x = unitConfig_1.unitConfig[unitType !== null ? unitType : '']) === null || _x === void 0 ? void 0 : _x.unitOption) === null || _y === void 0 ? void 0 : _y.find(function (res) { return res.shortCode === toUnit; })) === null || _z === void 0 ? void 0 : _z.conversionFactor;
}
if (unitType !== 'temperature' && unitType !== 'fuel-economics') {
var baseValue = (value * fromValue) / toValue;
if (baseValue === 0) {
return baseValue;
}
else if (baseValue > 1000000 || baseValue < 0.0001) {
return baseValue.toExponential(3);
}
else {
if (Number.isInteger(baseValue)) {
return baseValue;
}
else if (baseValue.toString().includes('.')) {
if (String(baseValue).split('.')[1].length > 6) {
return isDecimalZero(baseValue.toFixed(6)) ? Number(baseValue.toFixed(0)) : Number(baseValue.toFixed(6));
}
else {
return baseValue;
}
}
else {
return baseValue;
}
}
}
else if (unitType === 'temperature') {
if (Number.isInteger(parseFloat(toValue))) {
return parseFloat(toValue);
}
else {
return parseFloat(toValue).toFixed(4);
}
}
else {
var baseValue = toUnit !== 'L/100km' ? (value * fromValue) / toValue : (value * toValue) / fromValue;
if (baseValue === 0) {
return baseValue;
}
else if (baseValue > 1000000 || baseValue < 0.0001) {
return baseValue.toExponential(3);
}
else {
if (Number.isInteger(baseValue)) {
return baseValue;
}
else if (baseValue.toString().includes('.')) {
if (String(baseValue).split('.')[1].length > 6) {
return baseValue.toFixed(6);
}
else {
return baseValue;
}
}
else {
return baseValue;
}
}
}
}
function convert(value, fromUnit, toUnit, unitType) {
return convertUnit(value, fromUnit, toUnit, unitType);
}
exports.convert = convert;
var uc = { convert: convert };
exports.default = uc;