1.2.9 • Published 7 months ago

@jclind/ingredient-parser v1.2.9

Weekly downloads
-
License
ISC
Repository
github
Last release
7 months ago

@jclind/ingredient-parser

A package for parsing ingredient strings and retrieving data about the ingredient.

About

This package is built with recipe-ingredient-parser-v3 and also returns ingredient data along with parsing the ingredient. If you don't need the ingredient data, just the parsed ingredient, I recommend using recipe-ingredient-parser-v3.

Installation:

npm install @jclind/ingredient-parser

Usage

import { parseIngredient } from '@jclind/ingredient-parser';

const ingredientString = '1 cup rice, washed';
const apiKey = 'YOUR_API_KEY';
const options = { returnNutritionData: true };
parseIngredient(ingredientString, apiKey, options);

Returns an object {id: (randomly generated id unique to every request), parsedIngredient, ingredientData} with the following properties/values.

parsedIngredient:

{
    quantity: 1,
    unit: 'cup',
    unitPlural: 'cups',
    symbol: 'c',
    ingredient: 'rice',
    originalIngredientString: '1 cup rice, washed'
    minQty: 1,
    maxQty: 1,
    comment: 'washed'
}

ingredientData:

{
    _id: '63caeabf4762c87be39c3795',    
    ingredientId: 20444,
    originalName: 'rice',
    name: 'rice',
    amount: 1,
    possibleUnits: [ 'g', 'oz', 'cup' ],
    consistency: 'solid',
    shoppingListUnits: [ 'ounces', 'pounds' ],
    aisle: 'Pasta and Rice',
    image: 'uncooked-white-rice.png',
    imagePath: 'https://spoonacular.com/cdn/ingredients_100x100/uncooked-white-rice.png',
    nutrition?: {
      nutrients: [Array],
      properties: [Array],
      flavonoids: [Array],
      caloricBreakdown: [Object],
      weightPerServing: [Object]
    },
    names: [ 'rice' ],
    dateAdded: 1674242751000,
    totalPriceUSACents: 75.71
}

API

parseIngredient(ingredientString: string, SPOONACULAR_API_KEY: string) => { parsedIngredient, IngredientData } Takes an ingredient string and a spoonacular API key and returns the parsed ingredient and the ingredient data.

  • ingredientString (string) required : preferably formatted as such: (quantity) (unit) (ingredient), (comment separated by a comment) i.e. 2 cups onions, diced
  • SPOONACULAR_API_KEY (string) required : your unique spoonacular API key.
  • options (object) optional : an object containing additional options. Currently, the only supported option is returnNutritionData, which, if set to true, will include nutrition data in the ingredientData object.

Note: You need to sign up for a free API key from spoonacular website.

Error Handling

If the passed ingredient isn't formatted correctly or the ingredient is unknown, an error will be returned with the original object. With invalid text:

parseIngredient('Invalid Text', YOUR_API_KEY)
/*
{
  error: { message: 'No Data Found, unknown ingredient: invalid text' },
  ingredientData: null,
  parsedIngredient: {
    quantity: 0,
    unit: 'q.b.',
    unitPlural: 'q.b.',
    symbol: null,
    ingredient: 'Invalid Text',
    originalIngredientString: 'Invalid Text',
    minQty: 0,
    maxQty: 0,
    comment: null
  }
}
*/

With an inavlid API_KEY:

parseIngredient('1 cup rice', INVALID_API_KEY)
/*
{
  error: { message: 'API Key Not Valid' },
  ingredientData: null,
  parsedIngredient: {
    quantity: 1,
    unit: 'cup',
    unitPlural: 'cups',
    symbol: 'c',
    ingredient: 'rice',
    originalIngredientString: '1 cup rice'
    minQty: 1,
    maxQty: 1,
    comment: null
  }
}
*/

Typescript

Typescript definitions are also included in the ingredient-parser package:

import { parseIngredient, ParsedIngredientType, IngredientDataType, IngredientResponseType, } from '@jclind/ingredient-parser';

const ingredientString: string = '1 cup rice, washed';
const apiKey: string = 'YOUR_API_KEY';

const parsed: IngredientResponseType = parseIngredient(ingredientString, apiKey);

const parsedIngredient: ParsedIngredientType = parsed.parsedIngredient
const ingredientData: IngredientDataType = parsed.ingredientData
1.2.8

7 months ago

1.2.7

7 months ago

1.2.6

7 months ago

1.2.5

7 months ago

1.2.4

7 months ago

1.2.3

7 months ago

1.2.2

7 months ago

1.2.9

7 months ago

1.2.0

1 year ago

1.2.1

1 year ago

1.0.22

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.39

1 year ago

1.0.38

1 year ago

1.1.2

1 year ago

1.0.40

1 year ago

1.0.42

1 year ago

1.0.41

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago