Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
//convertVolume.js
/*global adventurejs A*/ 
"use strict";

/**
 * Converts a string or number into milliliters. Accepts a number representing volume in milliliters, or a string to be converted to milliliters (ex: "100ml"), in nL, μL, mL, cL, dL, L, daL, hL, kL, ML.
 * @method adventurejs#convertVolume
 * @memberOf adventurejs
 * @param {Number|String} volume In milliliters.
 * @param {String} parent_id The ID of the Vessel containing the substance.
 * @returns {Number}
 * @todo Handle measurements other than milliliters.
 */
adventurejs.convertVolume = function Adventurejs_convertVolume(volume, parent_id) {
  // we use milliliters by default
  // TODO? pints, quarts, gallons

  var newvolume;

  // if it's number-as-string, convert to number
  // we default to milliliters, so there y'go
  if( false === isNaN( volume ) )
  {
    newvolume = Number( volume );
  }

  // if it's a string, check for digits and suffix
  else if( "string" === typeof volume ) 
  { 

    // strip out commas and spaces
    newvolume = volume.replace(/[ ,]/g, "");

    var digits = "";
    var suffix = "";

    // separate digits from suffix
    // we're going to allow different forms of suffixes
    // so we're going to check every character 
    for(var char in volume)
    {
      var digit = ( false === isNaN(volume[char]) || "." == volume[char] );

      if( digit )
      {

        // we only count digits until we start assembling a suffix
        // if this is true, we've encountered a digit in what we 
        // think is the suffix and we don't know what to do with that
        if( 0 > suffix.length  ) break;

        digits = digits + volume[char];
        continue;

      }

      suffix = suffix + volume[char];

    }

    digits = Number( digits );

    // conversion tables
    // https://converterin.com/volume/mililiter-ml-to-megaliter-ml.html
    // https://www.checkyourmath.com/convert/volume/megaliter.php

    switch( suffix )
    {
      case "":
        break;
      case "nL":
        newvolume = digits / 1000000;
        break;
      case "μL":
        newvolume = digits / 1000;
        break;
      case "mL":
        newvolume = digits;
        break;
      case "cL":
        newvolume = digits * 10;
        break;
      case "dL":
        newvolume = digits * 100;
        break;
      case "L":
        newvolume = digits * 1000;
        break;
      case "daL":
        newvolume = digits * 10000;
        break;
      case "hL":
        newvolume = digits * 100000;
        break;
      case "kL":
        newvolume = digits * 1000000;
        break;
      case "ML":
        newvolume = digits * 1000000000;
        break;
      default:
        break;
    }
  }

  if( isNaN( newvolume ) )
  {
    newvolume = 0;
    // let author know we don't know what to do with volume
    // and set newvolume to 0 for a valid return
    var msg = "";
    if( "undefined" !== typeof parent_id ) msg += parent_id + "'s ";
    else msg += "An object's ";
    msg += "volume was set to an invalid value. [MORE...] "
      + "\nconvertVolume received a value of " + volume + ". "
      + "\nAdventure.js defaults to volume in milliliters. "
      + "\nSuffixes are case sensitive. "
      + "\nRecognized suffixes are: "
      + "\n - nL (nanoliters)"
      + "\n - μL (microliters)"
      + "\n - mL (milliliters)"
      + "\n - cL (centiliters)"
      + "\n - dL (deciliters)"
      + "\n - L (liters)"
      + "\n - daL (decaliters)"
      + "\n - hL (hectoliters)"
      + "\n - kL (kiloliters)"
      + "\n - ML (megaliters)"
      + "\nAdventure.js doesn't handle other measurements at this time. ";
    this.game.log( "warn", "critical", msg , 'Utility' );
  }

  // prevent negative values
  newvolume = Math.max( newvolume, 0 );

  return newvolume;

}