Advertisement
felix_de_suza

Catalog

Jul 18th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.17 KB | None | 0 0
  1. function solve(){
  2. var module = (function () {
  3. var item,
  4. validator,
  5. book,
  6. media,
  7. catalog,
  8. bookCatalog,
  9. mediaCatalog;
  10.  
  11. function indexOfElementWithIdInCollection(collection, id) {
  12. var i, len;
  13.  
  14. for (i = 0, len = collection.length; i < len; i += 1) {
  15. if (collection[i].id == id) {
  16. return i;
  17. }
  18. }
  19.  
  20. return -1;
  21. }
  22.  
  23. validator = {
  24. validateString: function(val, name) {
  25. if(typeof val !== 'string') {
  26. throw new Error(name + ' should be a valid string');
  27. }
  28. },
  29. validateEmptyString: function(val, name) {
  30. if(val.length == 0) {
  31. throw new Error(name + ' should be non empty');
  32. }
  33. },
  34. validateLength: function(min, max, val, name) {
  35. if(val.length < min || val.length > max) {
  36. throw new Error(name + ' is not in the range between ' + min + ' and ' + max);
  37. }
  38. },
  39. validateContainingOnlyDigits: function(val, name) {
  40. for (var i = 0; i < val.length; i++) {
  41. if(!parseInt(+val[i])) {
  42. if(+val[i] === 0) {
  43. continue;
  44. }
  45.  
  46. throw new Error(name + ' should contain only digits');
  47. }
  48. }
  49. },
  50. validateNumber: function(val, name) {
  51. if(typeof val !== 'number') {
  52. throw new Error(name + ' shoud be a valid number');
  53. }
  54. },
  55. validateNumberRange: function(min, max, val, name) {
  56. if(val < min || val > max) {
  57. throw new Error(name + ' should be in range' + min + ' and ' + max);
  58. }
  59. }
  60. };
  61.  
  62. item = (function () {
  63. var item = Object.create({}),
  64. currentItemId = 0;
  65.  
  66. Object.defineProperty(item, 'init', {
  67. value: function(name, desciption) {
  68. this.name = name;
  69. this.description = desciption;
  70. this._id = ++currentItemId;
  71.  
  72. return this;
  73. }
  74. });
  75.  
  76. Object.defineProperty(item, 'name', {
  77. get: function() {
  78. return this._name;
  79. },
  80. set: function(val) {
  81. validator.validateString(val, 'item name');
  82. validator.validateEmptyString(val, 'item name');
  83.  
  84. this._name = val;
  85. }
  86. });
  87.  
  88. Object.defineProperty(item, 'description', {
  89. get: function() {
  90. return this._description;
  91. },
  92. set: function(val) {
  93. validator.validateString(val, 'item description');
  94. validator.validateLength(2, 40, val, 'item description');
  95.  
  96. this._description = val;
  97. }
  98. });
  99.  
  100. Object.defineProperty(item, 'id', {
  101. get: function() {
  102. return this._id;
  103. }
  104. });
  105.  
  106. return item;
  107. }());
  108.  
  109. book = (function (parent) {
  110. book = Object.create(parent);
  111.  
  112. Object.defineProperty(book, 'init', {
  113. value: function(name, isbn, genre, description) {
  114. parent.init.call(this, name, description);
  115.  
  116. this.isbn = isbn;
  117. this.genre = genre;
  118.  
  119. return this;
  120. }
  121. });
  122.  
  123. Object.defineProperty(book, 'isbn', {
  124. get: function() {
  125. return this._isbn;
  126. },
  127. set: function(val) {
  128. validator.validateString(val, 'book isbn');
  129. validator.validateContainingOnlyDigits(val, 'book isbn');
  130.  
  131. if(val.length != 13 && val.length != 10) {
  132. throw new Error('book isbn must be 10 or 13 characters');
  133. }
  134.  
  135. this._isbn = val;
  136. }
  137. });
  138.  
  139. Object.defineProperty(book, 'genre', {
  140. get: function() {
  141. return this._genre;
  142. },
  143. set: function(val) {
  144. validator.validateString(val, 'book genre');
  145. validator.validateLength(2, 20, val, 'book genre');
  146.  
  147. this._genre = val;
  148. }
  149. });
  150.  
  151. return book;
  152. }(item));
  153.  
  154. media = (function (parent) {
  155. media = Object.create(parent);
  156.  
  157. Object.defineProperty(media, 'init', {
  158. value: function(name, rating, duration, description) {
  159. parent.init.call(this, name, description);
  160.  
  161. this.rating = rating;
  162. this.duration = duration;
  163.  
  164. return this;
  165. }
  166. });
  167.  
  168. Object.defineProperty(media, 'rating', {
  169. get: function() {
  170. return this._rating;
  171. },
  172. set: function(val) {
  173. validator.validateNumber(val, 'media rating')
  174. validator.validateNumberRange(1, 5, val, 'media rating')
  175.  
  176. this._rating = val;
  177. }
  178. });
  179.  
  180. Object.defineProperty(media, 'duration', {
  181. get: function() {
  182. return this._duration;
  183. },
  184. set: function(val) {
  185. validator.validateNumber(val, 'media duration');
  186. if(val < 1) {
  187. throw new Error('media duration must be greater than 0');
  188. }
  189.  
  190. this._duration = val;
  191. }
  192. });
  193.  
  194. return media;
  195. }(item));
  196.  
  197. catalog = (function () {
  198. var catalog = Object.create({}),
  199. currentCatalogId = 0;
  200.  
  201. Object.defineProperty(catalog, 'init', {
  202. value: function(name) {
  203. this.name = name;
  204. this._items = [];
  205. this._id = ++currentCatalogId;
  206.  
  207. return this;
  208. }
  209. });
  210.  
  211. Object.defineProperty(catalog, 'name', {
  212. get: function() {
  213. return this._name;
  214. },
  215. set: function(val) {
  216. validator.validateString(val, 'catalog name');
  217. validator.validateLength(2, 40, val, 'catalog number');
  218.  
  219. this._name = val;
  220. }
  221. });
  222.  
  223. Object.defineProperty(catalog, 'id', {
  224. get: function() {
  225. return this._id;
  226. }
  227. });
  228.  
  229. Object.defineProperty(catalog, 'add', {
  230. value: function() {
  231. var items,
  232. i,
  233. len;
  234.  
  235. //console.log(arguments[0].slice(0));
  236.  
  237. if(arguments.length > 1) {
  238. items = [].slice.call(arguments);
  239. }
  240. else if(arguments.length === 1) {
  241. items = arguments[0].slice();
  242. }
  243. else if(arguments.length < 1){
  244. throw new Error('Invalid items passed to catalog');
  245. }
  246.  
  247. if(items.length === 0) {
  248. throw new Error('Invalid items passed to catalog');
  249. }
  250.  
  251. for (i = 0, len = items.length; i < len; i+=1) {
  252. //console.log(Object.getPrototypeOf(items[i]) == book || Object.getPrototypeOf(items[i]) == media);
  253.  
  254. if(!(Object.getPrototypeOf(items[i]) == book || Object.getPrototypeOf(items[i]) == media)) {
  255. throw new Error('Objects must be of type item');
  256. }
  257.  
  258. this._items.push(items[i]);
  259. }
  260.  
  261. return this;
  262. }
  263. });
  264.  
  265. Object.defineProperty(catalog, 'find', {
  266. value: function(val) {
  267.  
  268. if(typeof val !== 'object' && typeof val !== 'number') {
  269. throw new Error('Id is not a number');
  270. }
  271.  
  272. if(typeof val === 'undefined') {
  273. throw new Error('arguments for id must be passed');
  274. }
  275.  
  276. if(typeof val === 'number') {
  277. var foundIndex = indexOfElementWithIdInCollection(this._items, val);
  278.  
  279. if(foundIndex < 0) {
  280. return null;
  281. }
  282.  
  283. return this._items[foundIndex];
  284. }
  285. else if(typeof val === 'object') {
  286. var id = val.id,
  287. name = val.name,
  288. i,
  289. len,
  290. foundItems = [];
  291.  
  292. for (i = 0, len = this._items.length; i < len; i+=1) {
  293. name = name || '';
  294.  
  295. if(this._items[i].id === id || this._items[i].name.toLowerCase() === name.toLowerCase()) {
  296. foundItems.push(this._items[i]);
  297. }
  298. }
  299.  
  300. return foundItems;
  301. }
  302. }
  303. });
  304.  
  305. return catalog;
  306. }());
  307.  
  308. bookCatalog = (function (parent) {
  309. var bookCatalog = Object.create(parent);
  310.  
  311. Object.defineProperty(bookCatalog, 'init', {
  312. value: function(name) {
  313. parent.init.call(this, name);
  314.  
  315. return this;
  316. }
  317. });
  318.  
  319. Object.defineProperty(bookCatalog, 'add', {
  320. value: function() {
  321. parent.add.call(this, arguments);
  322. }
  323. });
  324.  
  325. return bookCatalog;
  326. }(catalog));
  327.  
  328. mediaCatalog = (function (parent) {
  329. var mediaCatalog = Object.create(parent);
  330.  
  331. Object.defineProperty(mediaCatalog, 'init', {
  332. value: function(name) {
  333. parent.init.call(this, name);
  334.  
  335. return this;
  336. }
  337. });
  338.  
  339. return mediaCatalog;
  340. }(catalog));
  341.  
  342. return {
  343. getBook: function (name, isbn, genre, description) {
  344. return Object.create(book).init(name, isbn, genre, description);
  345. },
  346. getMedia: function (name, rating, duration, description) {
  347. return Object.create(media).init(name, rating, duration, description);
  348. },
  349. getBookCatalog: function (name) {
  350. return Object.create(bookCatalog).init(name);
  351. },
  352. getMediaCatalog: function (name) {
  353. return Object.create(mediaCatalog).init(name);
  354. },
  355. getCatalog: function(name) {
  356. return Object.create(catalog).init(name);
  357. }
  358. };
  359. }());
  360.  
  361. return module;
  362. }
  363.  
  364. var module = solve();
  365.  
  366. var book = module.getBook('The secrets of the JavaScript Ninja', '1234567890', 'IT', 'A book about JavaScript');
  367. var book2 = module.getBook('The secrets of the JavaScript Ninja', '1234567890', 'IT', 'A book about JavaScript');
  368. var media = module.getMedia('some media', 3, 144, 'some description');
  369. var catalog = module.getCatalog('catalog');
  370.  
  371. //var bookCatalog = module.getBookCatalog('book catalog');
  372.  
  373. //console.log(bookCatalog);
  374. //bookCatalog.add(book, book2);
  375. //console.log(bookCatalog.items);
  376. //var book3 = {sssss: 'aaaaaa'};
  377.  
  378. catalog.add(book, media, book2);
  379. ////console.log(catalog.find({name: 'SOME meDia', id: 0}));
  380. //console.log(catalog.find({name: 'The secrets of the JavaScript Ninja'}));
  381. console.log(catalog);
  382.  
  383. //var catalog = module.getBookCatalog('John\'s catalog');
  384. //
  385. //var book1 = module.getBook('The secrets of the JavaScript Ninja', '1234567890', 'IT', 'A book about JavaScript');
  386. //var book2 = module.getBook('JavaScript: The Good Parts', '0123456789', 'IT', 'A good book about JS');
  387. //catalog.add(book1);
  388. //catalog.add(book2);
  389. //
  390. //console.log(catalog.find(book1.id));
  391. ////returns book1
  392. //
  393. //console.log(catalog.find({id: book2.id, genre: 'IT'}));
  394. ////returns book2
  395. //
  396. //console.log(catalog.search('js'));
  397. //// returns book2
  398. //
  399. //console.log(catalog.search('javascript'));
  400. ////returns book1 and book2
  401.  
  402. //console.log(catalog.search('Te sa zeleni'))
  403. //returns []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement