Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(){
- var module = (function () {
- var item,
- validator,
- book,
- media,
- catalog,
- bookCatalog,
- mediaCatalog;
- function indexOfElementWithIdInCollection(collection, id) {
- var i, len;
- for (i = 0, len = collection.length; i < len; i += 1) {
- if (collection[i].id == id) {
- return i;
- }
- }
- return -1;
- }
- validator = {
- validateString: function(val, name) {
- if(typeof val !== 'string') {
- throw new Error(name + ' should be a valid string');
- }
- },
- validateEmptyString: function(val, name) {
- if(val.length == 0) {
- throw new Error(name + ' should be non empty');
- }
- },
- validateLength: function(min, max, val, name) {
- if(val.length < min || val.length > max) {
- throw new Error(name + ' is not in the range between ' + min + ' and ' + max);
- }
- },
- validateContainingOnlyDigits: function(val, name) {
- for (var i = 0; i < val.length; i++) {
- if(!parseInt(+val[i])) {
- if(+val[i] === 0) {
- continue;
- }
- throw new Error(name + ' should contain only digits');
- }
- }
- },
- validateNumber: function(val, name) {
- if(typeof val !== 'number') {
- throw new Error(name + ' shoud be a valid number');
- }
- },
- validateNumberRange: function(min, max, val, name) {
- if(val < min || val > max) {
- throw new Error(name + ' should be in range' + min + ' and ' + max);
- }
- }
- };
- item = (function () {
- var item = Object.create({}),
- currentItemId = 0;
- Object.defineProperty(item, 'init', {
- value: function(name, desciption) {
- this.name = name;
- this.description = desciption;
- this._id = ++currentItemId;
- return this;
- }
- });
- Object.defineProperty(item, 'name', {
- get: function() {
- return this._name;
- },
- set: function(val) {
- validator.validateString(val, 'item name');
- validator.validateEmptyString(val, 'item name');
- this._name = val;
- }
- });
- Object.defineProperty(item, 'description', {
- get: function() {
- return this._description;
- },
- set: function(val) {
- validator.validateString(val, 'item description');
- validator.validateLength(2, 40, val, 'item description');
- this._description = val;
- }
- });
- Object.defineProperty(item, 'id', {
- get: function() {
- return this._id;
- }
- });
- return item;
- }());
- book = (function (parent) {
- book = Object.create(parent);
- Object.defineProperty(book, 'init', {
- value: function(name, isbn, genre, description) {
- parent.init.call(this, name, description);
- this.isbn = isbn;
- this.genre = genre;
- return this;
- }
- });
- Object.defineProperty(book, 'isbn', {
- get: function() {
- return this._isbn;
- },
- set: function(val) {
- validator.validateString(val, 'book isbn');
- validator.validateContainingOnlyDigits(val, 'book isbn');
- if(val.length != 13 && val.length != 10) {
- throw new Error('book isbn must be 10 or 13 characters');
- }
- this._isbn = val;
- }
- });
- Object.defineProperty(book, 'genre', {
- get: function() {
- return this._genre;
- },
- set: function(val) {
- validator.validateString(val, 'book genre');
- validator.validateLength(2, 20, val, 'book genre');
- this._genre = val;
- }
- });
- return book;
- }(item));
- media = (function (parent) {
- media = Object.create(parent);
- Object.defineProperty(media, 'init', {
- value: function(name, rating, duration, description) {
- parent.init.call(this, name, description);
- this.rating = rating;
- this.duration = duration;
- return this;
- }
- });
- Object.defineProperty(media, 'rating', {
- get: function() {
- return this._rating;
- },
- set: function(val) {
- validator.validateNumber(val, 'media rating')
- validator.validateNumberRange(1, 5, val, 'media rating')
- this._rating = val;
- }
- });
- Object.defineProperty(media, 'duration', {
- get: function() {
- return this._duration;
- },
- set: function(val) {
- validator.validateNumber(val, 'media duration');
- if(val < 1) {
- throw new Error('media duration must be greater than 0');
- }
- this._duration = val;
- }
- });
- return media;
- }(item));
- catalog = (function () {
- var catalog = Object.create({}),
- currentCatalogId = 0;
- Object.defineProperty(catalog, 'init', {
- value: function(name) {
- this.name = name;
- this._items = [];
- this._id = ++currentCatalogId;
- return this;
- }
- });
- Object.defineProperty(catalog, 'name', {
- get: function() {
- return this._name;
- },
- set: function(val) {
- validator.validateString(val, 'catalog name');
- validator.validateLength(2, 40, val, 'catalog number');
- this._name = val;
- }
- });
- Object.defineProperty(catalog, 'id', {
- get: function() {
- return this._id;
- }
- });
- Object.defineProperty(catalog, 'add', {
- value: function() {
- var items,
- i,
- len;
- //console.log(arguments[0].slice(0));
- if(arguments.length > 1) {
- items = [].slice.call(arguments);
- }
- else if(arguments.length === 1) {
- items = arguments[0].slice();
- }
- else if(arguments.length < 1){
- throw new Error('Invalid items passed to catalog');
- }
- if(items.length === 0) {
- throw new Error('Invalid items passed to catalog');
- }
- for (i = 0, len = items.length; i < len; i+=1) {
- //console.log(Object.getPrototypeOf(items[i]) == book || Object.getPrototypeOf(items[i]) == media);
- if(!(Object.getPrototypeOf(items[i]) == book || Object.getPrototypeOf(items[i]) == media)) {
- throw new Error('Objects must be of type item');
- }
- this._items.push(items[i]);
- }
- return this;
- }
- });
- Object.defineProperty(catalog, 'find', {
- value: function(val) {
- if(typeof val !== 'object' && typeof val !== 'number') {
- throw new Error('Id is not a number');
- }
- if(typeof val === 'undefined') {
- throw new Error('arguments for id must be passed');
- }
- if(typeof val === 'number') {
- var foundIndex = indexOfElementWithIdInCollection(this._items, val);
- if(foundIndex < 0) {
- return null;
- }
- return this._items[foundIndex];
- }
- else if(typeof val === 'object') {
- var id = val.id,
- name = val.name,
- i,
- len,
- foundItems = [];
- for (i = 0, len = this._items.length; i < len; i+=1) {
- name = name || '';
- if(this._items[i].id === id || this._items[i].name.toLowerCase() === name.toLowerCase()) {
- foundItems.push(this._items[i]);
- }
- }
- return foundItems;
- }
- }
- });
- return catalog;
- }());
- bookCatalog = (function (parent) {
- var bookCatalog = Object.create(parent);
- Object.defineProperty(bookCatalog, 'init', {
- value: function(name) {
- parent.init.call(this, name);
- return this;
- }
- });
- Object.defineProperty(bookCatalog, 'add', {
- value: function() {
- parent.add.call(this, arguments);
- }
- });
- return bookCatalog;
- }(catalog));
- mediaCatalog = (function (parent) {
- var mediaCatalog = Object.create(parent);
- Object.defineProperty(mediaCatalog, 'init', {
- value: function(name) {
- parent.init.call(this, name);
- return this;
- }
- });
- return mediaCatalog;
- }(catalog));
- return {
- getBook: function (name, isbn, genre, description) {
- return Object.create(book).init(name, isbn, genre, description);
- },
- getMedia: function (name, rating, duration, description) {
- return Object.create(media).init(name, rating, duration, description);
- },
- getBookCatalog: function (name) {
- return Object.create(bookCatalog).init(name);
- },
- getMediaCatalog: function (name) {
- return Object.create(mediaCatalog).init(name);
- },
- getCatalog: function(name) {
- return Object.create(catalog).init(name);
- }
- };
- }());
- return module;
- }
- var module = solve();
- var book = module.getBook('The secrets of the JavaScript Ninja', '1234567890', 'IT', 'A book about JavaScript');
- var book2 = module.getBook('The secrets of the JavaScript Ninja', '1234567890', 'IT', 'A book about JavaScript');
- var media = module.getMedia('some media', 3, 144, 'some description');
- var catalog = module.getCatalog('catalog');
- //var bookCatalog = module.getBookCatalog('book catalog');
- //console.log(bookCatalog);
- //bookCatalog.add(book, book2);
- //console.log(bookCatalog.items);
- //var book3 = {sssss: 'aaaaaa'};
- catalog.add(book, media, book2);
- ////console.log(catalog.find({name: 'SOME meDia', id: 0}));
- //console.log(catalog.find({name: 'The secrets of the JavaScript Ninja'}));
- console.log(catalog);
- //var catalog = module.getBookCatalog('John\'s catalog');
- //
- //var book1 = module.getBook('The secrets of the JavaScript Ninja', '1234567890', 'IT', 'A book about JavaScript');
- //var book2 = module.getBook('JavaScript: The Good Parts', '0123456789', 'IT', 'A good book about JS');
- //catalog.add(book1);
- //catalog.add(book2);
- //
- //console.log(catalog.find(book1.id));
- ////returns book1
- //
- //console.log(catalog.find({id: book2.id, genre: 'IT'}));
- ////returns book2
- //
- //console.log(catalog.search('js'));
- //// returns book2
- //
- //console.log(catalog.search('javascript'));
- ////returns book1 and book2
- //console.log(catalog.search('Te sa zeleni'))
- //returns []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement