Source

number/convertNumberBetweenMeasurementUnits.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var convertLength = function (value, from, to) {
    // From: cm
    if (from === 'cm') {
        if (to === 'm')
            return value * 0.01;
        if (to === 'in')
            return value / 2.54;
        if (to === 'ft')
            return value / 30.48;
    }
    // From: m
    if (from === 'm') {
        if (to === 'cm')
            return value * 100;
        if (to === 'in')
            return value * 39.3701;
        if (to === 'ft')
            return value * 3.28084;
    }
    // From: in
    if (from === 'in') {
        if (to === 'cm')
            return value * 2.54;
        if (to === 'm')
            return value / 39.3701;
        if (to === 'ft')
            return value / 12;
    }
    // From: ft
    if (from === 'ft') {
        if (to === 'cm')
            return value * 30.48;
        if (to === 'm')
            return value / 3.28084;
        if (to === 'in')
            return value * 12;
    }
    return null;
};
var convertMassWeight = function (value, from, to) {
    // From g
    if (from === 'g') {
        if (to === 'kg')
            return value / 1000;
        if (to === 'oz')
            return value / 28.35;
        if (to === 'lb')
            return value / 454;
    }
    // From kg
    if (from === 'kg') {
        if (to === 'g')
            return value * 1000;
        if (to === 'oz')
            return value * 35.274;
        if (to === 'lb')
            return value * 2.205;
    }
    // From oz
    if (from === 'oz') {
        if (to === 'g')
            return value * 28.35;
        if (to === 'kg')
            return value / 35.274;
        if (to === 'lb')
            return value / 16;
    }
    // From lb
    if (from === 'lb') {
        if (to === 'g')
            return value * 454;
        if (to === 'kg')
            return value / 2.205;
        if (to === 'oz')
            return value * 16;
    }
    return null;
};
/**
 * Convert number between units, e.g. from grams to kilograms
 * @param value the number to convert
 * @param from Source unit
 * @param to Destination unit
 * @category number
 * @module convertNumberBetweenMeasurementUnits
 */
var convertNumberBetweenMeasurementUnits = function (value, from, to) {
    var lengthValue = convertLength(value, from, to);
    if (lengthValue)
        return lengthValue;
    var massWeightValue = convertMassWeight(value, from, to);
    if (massWeightValue)
        return massWeightValue;
    return value;
};
exports.default = convertNumberBetweenMeasurementUnits;