-
-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 773 - new method "getProductNameBrandQuantity" #844
Changes from all commits
8fc14ce
026fcd1
8920956
0444539
0f88a3c
79dc521
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,6 +115,18 @@ class Product extends JsonObject { | |
includeIfNull: false) | ||
Map<OpenFoodFactsLanguage, String>? genericNameInLanguages; | ||
|
||
/// Abbreviated product name. | ||
@JsonKey(name: 'abbreviated_product_name', includeIfNull: false) | ||
String? abbreviatedName; | ||
|
||
/// Localized abbreviated product name. | ||
@JsonKey( | ||
name: 'abbreviated_product_name_in_languages', | ||
fromJson: LanguageHelper.fromJsonStringMap, | ||
toJson: LanguageHelper.toJsonStringMap, | ||
includeIfNull: false) | ||
Map<OpenFoodFactsLanguage, String>? abbreviatedNameInLanguages; | ||
|
||
@JsonKey(name: 'brands', includeIfNull: false) | ||
String? brands; | ||
@JsonKey(name: 'brands_tags', includeIfNull: false) | ||
|
@@ -610,6 +622,11 @@ class Product extends JsonObject { | |
result.genericNameInLanguages ??= {}; | ||
result.genericNameInLanguages![language] = label; | ||
break; | ||
case ProductField.ABBREVIATED_NAME_IN_LANGUAGES: | ||
case ProductField.ABBREVIATED_NAME_ALL_LANGUAGES: | ||
result.abbreviatedNameInLanguages ??= {}; | ||
result.abbreviatedNameInLanguages![language] = label; | ||
break; | ||
case ProductField.INGREDIENTS_TEXT_IN_LANGUAGES: | ||
case ProductField.INGREDIENTS_TEXT_ALL_LANGUAGES: | ||
result.ingredientsTextInLanguages ??= {}; | ||
|
@@ -692,6 +709,7 @@ class Product extends JsonObject { | |
switch (productField) { | ||
case ProductField.NAME_IN_LANGUAGES: | ||
case ProductField.GENERIC_NAME_IN_LANGUAGES: | ||
case ProductField.ABBREVIATED_NAME_IN_LANGUAGES: | ||
case ProductField.INGREDIENTS_TEXT_IN_LANGUAGES: | ||
case ProductField.PACKAGING_TEXT_IN_LANGUAGES: | ||
setLanguageString(productField, language, json[key]); | ||
|
@@ -951,4 +969,75 @@ class Product extends JsonObject { | |
} | ||
_nutriments = nutriments; | ||
} | ||
|
||
/// Returns the best version of a product name. | ||
/// | ||
/// cf. openfoodfacts-server/lib/ProductOpener/Products.pm | ||
String getBestProductName(final OpenFoodFactsLanguage language) { | ||
String? tmp; | ||
if ((tmp = productNameInLanguages?[language])?.isNotEmpty == true) { | ||
return tmp!; | ||
} | ||
if ((tmp = productName)?.isNotEmpty == true) { | ||
return tmp!; | ||
} | ||
if ((tmp = genericNameInLanguages?[language])?.isNotEmpty == true) { | ||
return tmp!; | ||
} | ||
if ((tmp = genericName)?.isNotEmpty == true) { | ||
return tmp!; | ||
} | ||
if ((tmp = abbreviatedNameInLanguages?[language])?.isNotEmpty == true) { | ||
return tmp!; | ||
} | ||
if ((tmp = abbreviatedName)?.isNotEmpty == true) { | ||
return tmp!; | ||
} | ||
Comment on lines
+978
to
+995
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we do a tmp ??= x; return tmp; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @M123-dev Looks tempting but we check "not null and not empty", not just "not null", therefore your suggestion would potentially return empty strings. |
||
return ''; | ||
} | ||
|
||
/// Returns the first of all brands. | ||
String? getFirstBrand() { | ||
if (brands == null) { | ||
return null; | ||
} | ||
final List<String> items = brands!.split(','); | ||
if (items.isEmpty) { | ||
return null; | ||
} | ||
return items.first; | ||
} | ||
|
||
/// Returns a combo of the best product name and the first brand. | ||
/// | ||
/// cf. openfoodfacts-server/lib/ProductOpener/Products.pm | ||
String getProductNameBrand( | ||
final OpenFoodFactsLanguage language, | ||
final String separator, | ||
) { | ||
final String bestProductName = getBestProductName(language); | ||
final String? firstBrand = getFirstBrand(); | ||
if (firstBrand == null) { | ||
return bestProductName; | ||
} | ||
return '$bestProductName$separator$firstBrand'; | ||
} | ||
|
||
/// Returns a combo of best product name, first brand and quantity. | ||
/// | ||
/// cf. openfoodfacts-server/lib/ProductOpener/Products.pm | ||
String getProductNameBrandQuantity( | ||
final OpenFoodFactsLanguage language, | ||
final String separator, | ||
) { | ||
final String productNameBrand = getProductNameBrand(language, separator); | ||
if (quantity?.isNotEmpty != true) { | ||
return productNameBrand; | ||
} | ||
if (productNameBrand.contains(quantity!)) { | ||
return productNameBrand; | ||
} | ||
// quantity: put non breaking spaces between numbers and units | ||
return '$productNameBrand$separator${quantity!.replaceAll(' ', '\u{00A0}')}'; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to return a
null
value if there is no match; otherwise you force checking an empty string