Last active
March 18, 2020 22:40
-
-
Save Elergy/f79952ffa36fbf84877c7599221127cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| addBook (author, title) { | |
| const book = this._findExisting(author, title); | |
| if (book) { | |
| book.total += 1; | |
| book.available += 1; | |
| } else { | |
| this._books.push({ | |
| info: { | |
| author: author, | |
| title: title, | |
| }, | |
| total: 1, | |
| available: 1 | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const book = this._findExisting(author, title); | |
| if (!book || !book.available) { | |
| return null; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Library { | |
| constructor () { | |
| this._books = []; | |
| } | |
| addBook (author, title) { | |
| const book = this._findExisting(author, title); | |
| if (book) { | |
| book.total += 1; | |
| book.available += 1; | |
| } else { | |
| this._books.push({ | |
| author: author, | |
| title: title, | |
| total: 1, | |
| available: 1 | |
| }); | |
| } | |
| } | |
| takeBook (author, title) { | |
| const book = this._findExisting(author, title); | |
| if (!book || !book.available) { | |
| return null; | |
| } | |
| book.available -= 1; | |
| return book; | |
| } | |
| _findExisting (author, title) { ... } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test( | |
| '.takeBook should return return `null` ' + | |
| 'if we have the book in the library, ' + | |
| 'but there are no available copies', () => { | |
| // prepare | |
| const library = new Library(); | |
| library._books = [ | |
| { | |
| author: 'King', title: 'It', | |
| available: 0, total: 1 | |
| }, | |
| { | |
| author: 'King', title: 'Carrie', | |
| available: 1, total: 1 | |
| }, | |
| { | |
| author: 'Orwell', title: '1984', | |
| available: 1, total: 1 | |
| } | |
| ]; | |
| // act | |
| const book = library.takeBook('King', 'It'); | |
| // verify | |
| isNull(bookId) | |
| } | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library._books = [ | |
| { | |
| author: 'King', title: 'It', | |
| available: 0, total: 1 // no avaiable copies | |
| }, | |
| { | |
| author: 'King', title: 'Carrie', | |
| available: 1, total: 1 | |
| }, | |
| { | |
| author: 'Orwell', title: '1984', | |
| available: 1, total: 1 | |
| } | |
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test( | |
| '.takeBook should return return `null` ' + | |
| 'if we have the book in the library, ' + | |
| 'but there are no available copies', () => { | |
| // prepare | |
| const library = new Library(); | |
| library.addBook('King', 'It'); | |
| library.addBook('King', 'Carrie'); | |
| library.addBook('Orwell', '1984'); | |
| // remove one available copy of "It" | |
| library.takeBook('King', 'It'); | |
| // act | |
| const book = library.takeBook('King', 'It'); | |
| // verify | |
| isNull(bookId) | |
| } | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| takeBook (author, title) { | |
| const book = this._findExisting(author, title); | |
| if (!book) { | |
| return null; | |
| } | |
| book.available -= 1; | |
| return book; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment