// Number utilities

// Converts a value (string, number, etc.) to an integer number
// Assumes radix base 10
const toInteger = function (value) {
  let defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : NaN;
  const integer = parseInt(value, 10);
  return isNaN(integer) ? defaultValue : integer;
};

// Converts a value (string, number, etc.) to a number
const toFloat = function (value) {
  let defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : NaN;
  const float = parseFloat(value);
  return isNaN(float) ? defaultValue : float;
};

// Converts a value (string, number, etc.) to a string
// representation with `precision` digits after the decimal
// Returns the string 'NaN' if the value cannot be converted
const toFixed = (val, precision) => toFloat(val).toFixed(toInteger(precision, 0));

export { toFixed, toFloat, toInteger };
