replicake
Version:
A javascript library for the creation and manipulate of recipes.
252 lines (221 loc) • 6.15 kB
JavaScript
'use strict';
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
/**
* Represents commonly used units in recipes.
*/
var Units = /*#__PURE__*/function () {
function Units(options) {
_classCallCheck(this, Units);
var defaults = {
name: '',
symbol: ''
};
var actual = Object.assign({}, defaults, options);
this.name = actual.name;
this.symbol = actual.symbol;
if (actual.plural) this.plural = actual.plural;
}
/**
* Returns a string with the units name.
* @returns {string} The units name.
*/
_createClass(Units, [{
key: "toString",
value: function toString() {
return this.name;
}
/**
* Find and returns a Units constant given a string name or symbol. Must be exact match.
* @param {string} str Unit name or symbol to lookup.
* @returns {Units} Matching Units object, or undefined if not found.
*/
}], [{
key: "Lookup",
value: function Lookup(str) {
Object.values(UNITS).forEach(function (unit) {
if (str === unit.name || // Match the unit name
str === unit.name + 's' || // Match the plural unit name
str === unit.symbol || // Match the symbol (matches '' to UNITS.count)
unit.plural && str === unit.plural) {
// Match a custom plural name
return unit;
}
});
return undefined;
}
/**
* Units constant for a count (a unit-less number).
* For example, this would be used for the number of eggs.
*/
}, {
key: "COUNT",
get: function get() {
return UNITS.count;
}
/**
* Units constant for grams (g).
*/
}, {
key: "GRAMS",
get: function get() {
return UNITS.grams;
}
}]);
return Units;
}(); // Create 'singleton' constants for each pre-defined unit type
var UNITS = Object.freeze({
count: Object.freeze(new Units({
name: 'count',
symbol: ''
})),
grams: Object.freeze(new Units({
name: 'gram',
symbol: 'g'
}))
});
/**
* Represent an amount with a specific {@link Units} (e.g '30 grams').
*/
var Quantity = function Quantity(options) {
_classCallCheck(this, Quantity);
var defaults = {
amount: 0,
units: ''
};
var actual = Object.assign({}, defaults, options);
this.amount = actual.amount;
this.units = Units.Lookup(actual.units);
};
var CALORIES_PER_GRAM_FAT = 9;
var CALORIES_PER_GRAM_CARB = 4;
var CALORIES_PER_GRAM_PROTEIN = 4;
/**
* All values are assumed to be in the units indicated.
*/
var Nutrition = /*#__PURE__*/function () {
function Nutrition(options) {
_classCallCheck(this, Nutrition);
var defaults = {
size: '1 serving',
quantity: {},
fat: 0,
// g
saturated: 0,
// g
trans: 0,
// g
cholesterol: 0,
// mg
sodium: 0,
// mg
carbs: 0,
// g
fiber: 0,
// g
sugar: 0,
// g
protein: 0 // g
};
var actual = Object.assign({}, defaults, options);
this.size = actual.size;
this.quantity = new Quantity(actual.quantity);
this.fat = actual.fat;
this.saturated = actual.saturated;
this.trans = actual.trans;
this.cholesterol = actual.cholesterol;
this.sodium = actual.sodium;
this.carbs = actual.carbs;
this.fiber = actual.fiber;
this.sugar = actual.sugar;
this.protein = actual.protein; // Calories is optional and is estimated from nutrition macros if omitted
if (actual.calories) this._calories = actual.calories;
}
/**
* The number of calories, either as provided or computed based on nutrition macros.
*/
_createClass(Nutrition, [{
key: "calculateCalories",
/**
* Gets the approximate number of calories based on nutrition macros.
*/
value: function calculateCalories() {
return this.fat * CALORIES_PER_GRAM_FAT + this.carbs * CALORIES_PER_GRAM_CARB + this.protein * CALORIES_PER_GRAM_PROTEIN;
}
}, {
key: "calories",
get: function get() {
if (this._calories) return this._calories;
return this.calculateCalories();
},
set: function set(value) {
this._calories = value;
}
}]);
return Nutrition;
}();
/**
* An ingredient in a recipe, usually with a provided {@link Quantity}.
*/
var Ingredient = function Ingredient(options) {
_classCallCheck(this, Ingredient);
var defaults = {
name: '',
quantity: {},
nutrition: {}
};
var actual = Object.assign({}, defaults, options);
if (actual.id) this.id = actual.id;
this.name = actual.name;
this.quantity = new Quantity(actual.quantity);
this.nutrition = new Nutrition(actual.nutrition);
};
var Recipe = function Recipe(options) {
_classCallCheck(this, Recipe);
var defaults = {
name: '',
author: '',
description: '',
ingredients: {},
serves: 1
};
var actual = Object.assign({}, defaults, options);
if (actual.id) this.id = actual.id;
this.name = actual.name;
this.author = actual.author;
this.description = actual.description;
this.ingredients = actual.ingredients;
this.serves = actual.serves;
};
var Replicake = {
/**
* Create a new {@link Recipe} with the provided options.
* @param {*} options Options to create recipe with.
*/
Create: function Create(options) {
return new Recipe(options);
},
Ingredient: Ingredient,
Nutrition: Nutrition,
Quantity: Quantity,
Recipe: Recipe,
Units: Units
};
module.exports = Replicake;