Skip to content

Commit 6313749

Browse files
committed
fix(series import): don't truncate a price that contains a space.
Fix #1221
1 parent 1ed05c1 commit 6313749

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/main/java/ru/mystamps/web/feature/series/importing/SeriesInfoExtractorServiceImpl.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public class SeriesInfoExtractorServiceImpl implements SeriesInfoExtractorServic
6464
private static final Pattern MICHEL_NUMBERS_REGEXP =
6565
Pattern.compile("#[ ]?(?<begin>[1-9][0-9]{0,3})-(?<end>[1-9][0-9]{0,3})");
6666

67+
// Regular expression matches prices that have a space between digits.
68+
private static final Pattern PRICE_WITH_SPACES = Pattern.compile("([0-9]) ([0-9])");
69+
6770
// Regular expression that matches Rubles (Russian currency).
6871
private static final Pattern RUB_CURRENCY_REGEXP = Pattern.compile("[0-9][ ]?руб");
6972

@@ -367,14 +370,24 @@ public SeriesExtractedInfo extract(String pageUrl, RawParsedDataDto data) {
367370
return url;
368371
}
369372

370-
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
373+
@SuppressWarnings({
374+
"PMD.AvoidInstantiatingObjectsInLoops",
375+
"PMD.AvoidReassigningParameters",
376+
"checkstyle:parameterassignment"
377+
})
371378
/* default */ BigDecimal extractPrice(String fragment) {
372379
if (StringUtils.isBlank(fragment)) {
373380
return null;
374381
}
375382

376383
log.debug("Determine price from '{}'", fragment);
377384

385+
// "1 000" -> "1000"
386+
Matcher matcher = PRICE_WITH_SPACES.matcher(fragment);
387+
if (matcher.find()) {
388+
fragment = matcher.replaceAll("$1$2");
389+
}
390+
378391
String[] candidates = StringUtils.split(fragment, ' ');
379392
for (String candidate : candidates) {
380393
// replace comma with dot to handle 10,5 in the same way as 10.5

src/test/groovy/ru/mystamps/web/feature/series/importing/SeriesInfoExtractorServiceImplTest.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,17 @@ class SeriesInfoExtractorServiceImplTest extends Specification {
618618
'10 руб 16 коп' | _
619619
}
620620

621+
@Unroll
622+
def 'extractPrice() should ignore a space in "#fragment"'(String fragment, BigDecimal result) {
623+
expect:
624+
service.extractPrice(fragment) == result
625+
where:
626+
fragment || result
627+
'10 800,00 руб.' || new BigDecimal('10800')
628+
'1 200' || new BigDecimal('1200')
629+
'1 000 000' || new BigDecimal('1000000')
630+
}
631+
621632
//
622633
// Tests for extractCurrency()
623634
//

0 commit comments

Comments
 (0)