Created
December 3, 2014 11:06
-
-
Save chuhlomin/1c9c344a7340d36afb9c to your computer and use it in GitHub Desktop.
task1.js
This file contains 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
function BookStore() { | |
this._last_id = 0; | |
this._books = []; | |
} | |
var _p = BookStore.prototype; | |
_p._isValidBook = function(book) { | |
if (book === null) { | |
return false; | |
} | |
if (typeof book !== 'object') { | |
return false; | |
} | |
if (book.name === undefined) { | |
return false; | |
} | |
return true; | |
}; | |
_p._getBookIndexByName = function(name) { | |
for (var i = 0; i < this._books.length; i++) { | |
if (this._books[i].name.toUpperCase() === name.toUpperCase()) { | |
return i; | |
} | |
} | |
return false; | |
}; | |
_p._getBookIndexById = function(id) { | |
for (var i = 0; i < this._books.length; i++) { | |
if (this._books[i].id === id) { | |
return i; | |
} | |
} | |
return false; | |
}; | |
_p._getBookIndexByObject = function(book) { | |
return this._getBookIndexByName(book.name) | |
}; | |
_p._getBookIndex = function(book, allowSearchById, allowSearchByName, allowSearchByObject) { | |
var allowSearchById = allowSearchById || true; | |
var allowSearchByName = allowSearchByName || true; | |
var allowSearchByObject = allowSearchByObject || true; | |
if (typeof book === 'number' && allowSearchById) { | |
return this._getBookIndexById(book); | |
} | |
if (typeof book === 'string' && allowSearchByName) { | |
return this._getBookIndexByName(book); | |
} | |
if (typeof book === 'object' && allowSearchByObject) { | |
return this._getBookIndexByObject(book); | |
} | |
return false; | |
}; | |
_p.add = function() { | |
var result = []; | |
var book; | |
var book_index; | |
for (var i = 0; i < arguments.length; i++) { | |
book = arguments[i]; | |
if (!this._isValidBook(book)) { | |
continue; | |
} | |
book_index = this._getBookIndex(book, false, false, true); | |
if (book_index !== false) { | |
continue; | |
} | |
this._last_id++; | |
book.id = this._last_id; | |
this._books.push(book); | |
result.push(book); | |
} | |
if (result.length == 0) { | |
return 0; | |
} | |
return result; | |
}; | |
_p.remove = function() { | |
var result = []; | |
var book_index; | |
for (var i = 0; i < arguments.length; i++) { | |
book_index = this._getBookIndex(arguments[i], true, false, true); | |
if (book_index === false) { | |
continue; | |
} | |
result.push(this._books[book_index]); | |
this._books.splice(book_index, 1); | |
} | |
return result; | |
}; | |
_p.find = function() { | |
var result = []; | |
var book_index; | |
for (var i = 0; i < arguments.length; i++) { | |
book_index = this._getBookIndex(arguments[i], true, true, false); | |
if (book_index === false) { | |
continue; | |
} | |
result.push(this._books[book_index]); | |
} | |
return result; | |
} | |
_p.all = function() { | |
return this._books; | |
} | |
var store = new BookStore(); | |
var book1 = {name: 'Pride and Prejudice'}; | |
var book2 = {name: 'A Tale of Two Cities'}; | |
var book3 = {name: 'Treasure Island'}; | |
var book4 = {name: 'Dracula'}; | |
store.add(book1); | |
store.add('book', false, null); | |
store.add(book2, book3); | |
store.add(book4, book1); | |
store.remove(book1); | |
store.remove(book2, book3) | |
store.remove(book4, book1); | |
store.add(book1, book2, book3, book4); | |
store.remove(book3.id); | |
store.remove(book2, 'Treasure Island'); | |
console.log(store.all()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment