Skip to content

Commit fef243e

Browse files
authored
Merge pull request #233 from metafacture/231-addIsbnFunction
Add `isbn()` Fix function.
2 parents 6c8f884 + ea6d50e commit fef243e

File tree

7 files changed

+140
-0
lines changed

7 files changed

+140
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,20 @@ Deletes duplicate values in an array.
529529
uniq("<sourceField>")
530530
```
531531

532+
#### `isbn`
533+
534+
Extracts an ISBN and replaces the field value with the normalized ISBN; optionally converts and/or validates the ISBN.
535+
536+
Options:
537+
538+
- `to`: ISBN format to convert to (either `ISBN10` or `ISBN13`). (Default: Only normalize ISBN)
539+
- `verify_check_digit`: Whether the check digit should be verified. (Default: `false`)
540+
- `error_string`: Error message as a placeholder if the ISBN couln't be validated. (Default: `null`)
541+
542+
```perl
543+
isbn("<sourceField>"[, to: "<isbnFormat>"][, verify_check_digit: "<boolean>"][, error_string: "<errorValue>"])
544+
```
545+
532546
### Selectors
533547

534548
#### `reject`

metafix/src/main/java/org/metafacture/metafix/FixMethod.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.metafacture.metafix.api.FixFunction;
2020
import org.metafacture.metamorph.api.Maps;
21+
import org.metafacture.metamorph.functions.ISBN;
2122
import org.metafacture.metamorph.functions.Timestamp;
2223
import org.metafacture.metamorph.maps.FileMap;
2324

@@ -359,6 +360,18 @@ public void apply(final Metafix metafix, final Record record, final List<String>
359360
record.transform(params.get(0), s -> String.valueOf(s.indexOf(search))); // TODO: multiple
360361
}
361362
},
363+
isbn {
364+
@Override
365+
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
366+
final ISBN isbn = new ISBN();
367+
368+
withOption(options, "error_string", isbn::setErrorString);
369+
withOption(options, "to", isbn::setTo);
370+
withOption(options, "verify_check_digit", isbn::setVerifyCheckDigit);
371+
372+
record.transform(params.get(0), isbn::process);
373+
}
374+
},
362375
join_field {
363376
@Override
364377
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {

metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,95 @@ public void shouldGetIndexOfSubstring() {
11451145
);
11461146
}
11471147

1148+
@Test
1149+
public void shouldCleanseISBN10() {
1150+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1151+
"isbn(test)"
1152+
),
1153+
i -> {
1154+
i.startRecord("1");
1155+
i.literal("test", "ISBN: 1-.93.3-988-31-2 EUro 17.70");
1156+
i.endRecord();
1157+
},
1158+
o -> {
1159+
o.get().startRecord("1");
1160+
o.get().literal("test", "1933988312");
1161+
o.get().endRecord();
1162+
}
1163+
);
1164+
}
1165+
1166+
@Test
1167+
public void shouldConvertISBN10ToISBN13() {
1168+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1169+
"isbn(test, to: 'isbn13')"
1170+
),
1171+
i -> {
1172+
i.startRecord("1");
1173+
i.literal("test", "1933988312");
1174+
i.endRecord();
1175+
},
1176+
o -> {
1177+
o.get().startRecord("1");
1178+
o.get().literal("test", "9781933988313");
1179+
o.get().endRecord();
1180+
}
1181+
);
1182+
}
1183+
1184+
@Test
1185+
public void shouldConvertISBN13ToISBN10() {
1186+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1187+
"isbn(test, to: 'isbn10')"
1188+
),
1189+
i -> {
1190+
i.startRecord("1");
1191+
i.literal("test", "9781933988313");
1192+
i.endRecord();
1193+
},
1194+
o -> {
1195+
o.get().startRecord("1");
1196+
o.get().literal("test", "1933988312");
1197+
o.get().endRecord();
1198+
}
1199+
);
1200+
}
1201+
1202+
@Test
1203+
public void shouldNotProcessInvalidISBN10() {
1204+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1205+
"isbn(test, verify_check_digit: 'true')"
1206+
),
1207+
i -> {
1208+
i.startRecord("1");
1209+
i.literal("test", "1933988313");
1210+
i.endRecord();
1211+
},
1212+
o -> {
1213+
o.get().startRecord("1");
1214+
o.get().endRecord();
1215+
}
1216+
);
1217+
}
1218+
1219+
@Test
1220+
public void shouldSetInvalidISBN10ToErrorString() {
1221+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1222+
"isbn(test, verify_check_digit: 'true', error_string: 'invalid')"
1223+
),
1224+
i -> {
1225+
i.startRecord("1");
1226+
i.literal("test", "1933988313");
1227+
i.endRecord();
1228+
},
1229+
o -> {
1230+
o.get().startRecord("1");
1231+
o.get().literal("test", "invalid");
1232+
o.get().endRecord();
1233+
}
1234+
);
1235+
}
1236+
11481237
@Test
11491238
public void shouldJoinArrayField() {
11501239
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"test_1" : "9783894018109",
3+
"test_2" : "ungültig",
4+
"test_3" : "9783442151479",
5+
"test_4" : "3821808187"
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"test_1" : "978-3-89401-810-9",
3+
"test_2" : "978-3-89401-810-9",
4+
"test_3" : "3442151473",
5+
"test_4" : "978-3-8218-0818-5"
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
isbn("test_1",to:"clean")
2+
isbn("test_2",to:"clean",verify_check_digit:"true",error_string:"ungültig")
3+
isbn("test_3",to:"ISBN13",verify_check_digit:"true",error_string:"ungültig")
4+
isbn("test_4",to:"ISBN10",verify_check_digit:"true",error_string:"ungültig")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FLUX_DIR + "input.json"
2+
|open-file
3+
|as-records
4+
|decode-json
5+
|fix(FLUX_DIR + "test.fix")
6+
|encode-json(prettyPrinting="true")
7+
|write(FLUX_DIR + "output-metafix.json")
8+
;

0 commit comments

Comments
 (0)