From d690dd9d6872b7155a48b7853d1c092e8f084d3d Mon Sep 17 00:00:00 2001 From: Christiaan Baartse Date: Fri, 22 May 2020 11:34:03 +0200 Subject: [PATCH] Change Book.coverUrl to return null when there is no cover CoverUrl now returns an undefined when there is no cover. However not having a cover is expected behavior for an epub without a cover. --- documentation/md/API.md | 4 ++-- src/book.js | 21 +++++++++++---------- test/book.js | 2 +- types/book.d.ts | 2 +- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/documentation/md/API.md b/documentation/md/API.md index cfad86d..60a447b 100644 --- a/documentation/md/API.md +++ b/documentation/md/API.md @@ -134,9 +134,9 @@ Set headers request should use ### coverUrl -Get the cover url +Get the cover url if there is a cover -Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>** Promise resolves with url string +Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[?string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>** Promise resolves with maybe a url string ### getRange diff --git a/src/book.js b/src/book.js index c75c58d..f5fd332 100644 --- a/src/book.js +++ b/src/book.js @@ -661,19 +661,20 @@ class Book { /** * Get the cover url - * @return {Promise} coverUrl + * @return {Promise} coverUrl */ coverUrl() { - var retrieved = this.loaded.cover. - then(() => { - if(this.archived && this.cover) { - return this.archive.createUrl(this.cover); - }else{ - return this.cover; - } - }); + return this.loaded.cover.then(() => { + if (!this.cover) { + return null; + } - return retrieved; + if (this.archived) { + return this.archive.createUrl(this.cover); + } else { + return this.cover; + } + }); } /** diff --git a/test/book.js b/test/book.js index b839f56..41e2753 100644 --- a/test/book.js +++ b/test/book.js @@ -61,7 +61,7 @@ describe('Book', function() { }); it('should have a empty coverUrl', async function() { let coverUrl = await book.coverUrl() - assert.equal(coverUrl, undefined, "cover url should be undefined" ); + assert.equal(coverUrl, null, "cover url should be null" ); }); }); }); diff --git a/types/book.d.ts b/types/book.d.ts index 6f4a2ee..f1326b2 100644 --- a/types/book.d.ts +++ b/types/book.d.ts @@ -65,7 +65,7 @@ export default class Book { canonical(path: string): string; - coverUrl(): Promise; + coverUrl(): Promise; destroy(): void;