_.each(ingredients, function (subItem) {
   var listItem;
   if(!subItem.quantity && !subItem.fraction) {
     listItem = {_id: subItem._id, listName: subItem.listName, suffix: subItem.suffix};
   }
   else if (subItem.quantity && subItem.fraction){
     let quantity = math.fraction(subItem.quantity);
     let fraction = math.fraction(subItem.fraction);
     let sum = math.add(quantity, fraction);
     listItem = {_id: subItem._id, quantity: sum, measurement: subItem.measurement, listName: subItem.listName, suffix: subItem.suffix};
   }
   else if (!subItem.quantity && subItem.fraction){
     let fraction = math.fraction(subItem.fraction);
     let sum = math.add(0, fraction);
     listItem = {_id: subItem._id, quantity: sum, measurement: subItem.measurement, listName: subItem.listName, suffix: subItem.suffix};
   }
   else {
     let quantity = math.fraction(subItem.quantity);
     listItem = {_id: subItem._id, quantity: quantity, measurement: subItem.measurement, listName: subItem.listName, suffix: subItem.suffix};
   }
   var existingItem = _.findWhere(shoppingList, {_id: subItem._id});
   if(existingItem) {
     console.log(existingItem);
     console.log(listItem);
     var newItem = existingItem;
     if (existingItem.measurement === listItem.measurement) {
       let sum = math.add(existingItem.quantity, listItem.quantity);
       newItem.quantity = sum;
     }
     else {
       if (listItem.quantity) {
       newItem.addQuantity = math.fraction(listItem.quantity);
       newItem.addMeasurement = listItem.measurement;
       }
     }
     _.extend(_.findWhere(shoppingList, { _id: subItem._id }), newItem);
     return;
   }
   else {
     shoppingList.push(listItem);
   }
 });
const decimalToFractionString = function (number) {
  let fractionString;
  let wholeNumber = Math.floor(Number(number));
  let fraction = math.fraction(number-wholeNumber);
  if (fraction.n > 0 && wholeNumber > 0) {
    fractionString = wholeNumber + ' ' + fraction.n + '/' + fraction.d;
  }
  else if (fraction.n > 0) {
    fractionString = fraction.n + '/' + fraction.d;
  }
  else {
    fractionString = wholeNumber.toString();
  }
  return fractionString;
}
示例#3
0
文件: main.js 项目: marciof/eon
'use strict';

let mathjs = require('mathjs');
mathjs.config({number: 'Fraction'});

module.exports = {
    readNumber(text) {
        return mathjs.fraction(text);
    },

    writeNumber(number) {
        return mathjs.format(number, {fraction: 'decimal', notation: 'fixed'});
    },

    isNumberEqual(x, y, ...rest) {
        for (let n of [y, ...rest]) {
            if (!mathjs.equal(x, n)) {
                return false;
            }
        }
        return true;
    },

    divide(x, y, ...rest) {
        return [x, y, ...rest].reduce(
            (result, value) => mathjs.divide(result, value));
    },

    multiply(x, y, ...rest) {
        return mathjs.multiply(x, y, ...rest);
    },