diff --git a/CHANGELOG.md b/CHANGELOG.md index cb09a59..d7d7e37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ Date format: DD/MM/YYYY +## [next] + +- Use `DateTime.tryParse` to parse the date ([#18](https://github.com/bdlukaa/books_finder/issues/18)) + ## 4.3.0 - [01/05/2022] - **MINOR BREAKING** Renamed `BookInfo.industryIdentifier` to `BookInfo.industryIdentifiers` diff --git a/lib/src/scripts/books.dart b/lib/src/scripts/books.dart index d076e21..b3c6fd1 100644 --- a/lib/src/scripts/books.dart +++ b/lib/src/scripts/books.dart @@ -213,35 +213,7 @@ class BookInfo { Map json, { bool reschemeImageLinks = false, }) { - final publishedDateArray = - ((json['publishedDate'] as String?) ?? '0000-00-00').split('-'); - - // initialize datetime variable - DateTime? publishedDate; - if (publishedDateArray.isNotEmpty) { - // initialize date - int year = int.parse(publishedDateArray[0]); - int month = 1; - int day = 1; - - // now test the date string - if (publishedDateArray.length == 1) { - // assume we have only the year - year = int.parse(publishedDateArray[0]); - } - if (publishedDateArray.length == 2) { - // assume we have the year and maybe the month (this could be just a speculative case) - year = int.parse(publishedDateArray[0]); - month = int.parse(publishedDateArray[1]); - } - if (publishedDateArray.length == 3) { - // assume we have year-month-day - year = int.parse(publishedDateArray[0]); - month = int.parse(publishedDateArray[1]); - day = int.parse(publishedDateArray[2]); - } - publishedDate = DateTime(year, month, day); - } + DateTime? publishedDate = _getDateTime(json['publishedDate']); final imageLinks = {}; (json['imageLinks'] as Map?)?.forEach((key, value) { @@ -357,4 +329,38 @@ class BookInfo { infoLink.hashCode ^ canonicalVolumeLink.hashCode; } + + static DateTime _getDateTime(String? encoded) { + encoded ??= '0000-00-00'; + DateTime? publishedDate = DateTime.tryParse(encoded); + + if (publishedDate != null) return publishedDate; + + final publishedDateArray = encoded.split('-'); + if (publishedDateArray.isNotEmpty) { + // initialize date + int year = int.parse(publishedDateArray[0]); + int month = 1; + int day = 1; + // now test the date string + if (publishedDateArray.length == 1) { + // assume we have only the year + year = int.parse(publishedDateArray[0]); + } + if (publishedDateArray.length == 2) { + // assume we have the year and maybe the month (this could be just a speculative case) + year = int.parse(publishedDateArray[0]); + month = int.parse(publishedDateArray[1]); + } + if (publishedDateArray.length == 3) { + // assume we have year-month-day + year = int.parse(publishedDateArray[0]); + month = int.parse(publishedDateArray[1]); + day = int.parse(publishedDateArray[2]); + } + publishedDate = DateTime(year, month, day); + } + + return publishedDate ?? DateTime(0); + } } diff --git a/test/book_finder_test.dart b/test/book_finder_test.dart index 8000480..78eff56 100644 --- a/test/book_finder_test.dart +++ b/test/book_finder_test.dart @@ -128,6 +128,7 @@ void main() { /// .inauthor books = await queryBooks( 'J. K. Rowling', + langRestrict: 'en', queryType: QueryType.inauthor, maxResults: 1, printType: PrintType.books, @@ -141,7 +142,7 @@ void main() { /// .inpublisher books = await queryBooks( 'Scholastic inc', - queryType: QueryType.inauthor, + queryType: QueryType.inpublisher, maxResults: 1, printType: PrintType.books, orderBy: OrderBy.relevance,