Browse Source

Change unit parser: Show short version insted of verbose one

Tatiana Inama 7 years atrás
parent
commit
bfa68851c3

+ 2 - 1
ktchn/src/recipes/scrape/index.ts

@@ -4,6 +4,7 @@ import Sources from './sources';
 import { ScrapingSource } from './sources/index';
 import { IIngredient } from '../model';
 import { parse as parseIngredient } from 'recipe-ingredient-parser';
+import { units } from './service';
 
 function selectSourceAsync(url: string): Promise<ScrapingSource> {
   const foundSource = Sources.find((source) => {
@@ -23,7 +24,7 @@ export const parseIngredients = (rawIngredient: string): IIngredient => {
   return {
     name: parsed.ingredient,
     quantity: Number(parsed.quantity) || 0,
-    unit: parsed.unit || '',
+    unit: units(parsed.unit),
     _original: rawIngredient,
   };
 };

+ 29 - 1
ktchn/src/recipes/scrape/service.ts

@@ -15,4 +15,32 @@ export const getText =
 
 export const rmBreakLines = (str: string): string => str.replace(/(\n|\t|\s{2,})/g, ' ').trim();
 
-export const rmEquivalence = (str: string): string => str.replace(/\([0-9.]+ \w+\) ?/, '');
+export const rmEquivalence = (str: string): string => str.replace(/\([0-9.]+ \w+\) ?/, '');
+
+export const units = (unit: string|null) => {
+  const units: { [unit: string]: string,} = {
+    cup: 'cup',
+    gallon: 'gal',
+    ounce: 'oz',
+    pint: 'pt',
+    pound: 'lb',
+    quart: 'qt',
+    tablespoon: 'tbsp',
+    teaspoon: 'tsp',
+    gram: 'g',
+    kilogram: 'kg',
+    liter: 'l',
+    milligram: 'mg',
+    milliliter: 'ml',
+    package: 'package',
+    stick: 'stick',
+    clove: 'clove',
+    bag: 'bag',
+    box: 'box',
+    pinch: 'pinch',
+    can: 'can',
+    slice: 'slice'
+  }
+
+  return unit ? units[unit] : ''
+};

+ 1 - 10
ktchn/src/recipes/scrape/sources/laura-in-the-kitchen.ts

@@ -1,5 +1,5 @@
 import { Recipe, RecipeDetails, IIngredient, ISubRecipe, Author } from '../../model';
-import { parse as parseIngredient } from "recipe-ingredient-parser";
+import { parseIngredients } from '../index';
 import { getText, getTextList, getAttr } from "./../service";
 
 const LITK_AUTHOR_DATA: Author = {
@@ -26,15 +26,6 @@ function getRecipeDetails($: CheerioSelector): RecipeDetails {
 }
 
 function getIngredients($: CheerioSelector): ISubRecipe[] {
-  const parseIngredients = (rawIngredient: string): IIngredient => {
-    let parsed = parseIngredient(rawIngredient);
-    return {
-      name: parsed.ingredient,
-      quantity: Number(parsed.quantity) || 0,
-      unit: parsed.unit || '',
-      _original: rawIngredient,
-    };
-  };
   const ingredientsScrape = $(SELECTORS.INGREDIENTS).children();
   const isNewIngredientList = (tagName: string): boolean => tagName === 'span';