Skip to content

Instantly share code, notes, and snippets.

@turingmachine
Created October 16, 2019 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turingmachine/2f2438d239c9c63d5d38e29a70e28501 to your computer and use it in GitHub Desktop.
Save turingmachine/2f2438d239c9c63d5d38e29a70e28501 to your computer and use it in GitHub Desktop.
const transformFoodDeclarations = apiFoodDeclaration => {
const boldTextRegex = /\$([^\$]+)\$/g
const producing_country =
_.get(apiFoodDeclaration, 'producing_country', null) === '-'
? null
: _.replace(
_.get(apiFoodDeclaration, 'producing_country', null),
'DE: ',
''
)
const shelf_producing_country = _.get(
apiFoodDeclaration,
'shelf_data.gmaa.producing_country',
null
)
try {
return {
description: _.get(
apiFoodDeclaration,
'regulated_description',
''
).replace(boldTextRegex, '<strong>$1</strong>'),
origin: _.replace(_.get(apiFoodDeclaration, 'orign', null), 'DE: ', ''),
producing_country:
shelf_producing_country !== 'Herkunft siehe Verpackung'
? shelf_producing_country
: producing_country,
ingredients: striptags(_.get(apiFoodDeclaration, 'ingredients', null)),
allergens: _.replace(
_.get(apiFoodDeclaration, 'allergen_text', null),
'DE: ',
''
),
nutrients: transformNutrients(
_.get(apiFoodDeclaration, 'nutrition_facts.standard', null)
),
generalInformation: _.get(apiFoodDeclaration, 'general_information', [])
.map(info => {
return {
title: info.label,
text: _.replace(info.value, 'DE: ', ''),
}
})
.filter(generalInformation => {
return _.every([
generalInformation.title !== 'Auslobung',
generalInformation.title !== 'Haltbarkeit',
generalInformation.title !== 'Text Datierung',
generalInformation.text !== '-',
])
}),
}
} catch (error) {
console.log(error)
Raven.captureException(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment