Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.  
  3.     const getNextId = (function* () {
  4.         let counter = 1;
  5.  
  6.         while (true) {
  7.             yield counter++;
  8.         }
  9.     })();
  10.  
  11.     const Validator = {
  12.         validateString(value, message) {
  13.             if (typeof value !== "string") {
  14.                 throw new Error(message);
  15.             }
  16.         },
  17.  
  18.         validateNumberInRange(value, min, max, message) {
  19.             if (typeof value !== "number" || value < min || max < value) {
  20.                 throw new Error(message);
  21.             }
  22.         },
  23.  
  24.         validatePlayable(playable) {
  25.             if (!(playable instanceof Playable)
  26.                 && (!playable.hasOwnProperty("title")
  27.                     || !playable.hasOwnProperty("author")
  28.                     || !playable.hasOwnProperty("id")
  29.                 )) {
  30.                 return false;
  31.             }
  32.             return true;
  33.         }
  34.  
  35.  
  36.     }
  37.  
  38.     class Player {
  39.         constructor(name) {
  40.             this.name = name;
  41.             this._id = getNextId.next().value;
  42.             this._playlists = [];
  43.         }
  44.  
  45.         get name() {
  46.             return this._name;
  47.         }
  48.  
  49.         set name(value) {
  50.             validateString(value);
  51.             this._name = value;
  52.         }
  53.  
  54.         get id() {
  55.             return this._id;
  56.         }
  57.  
  58.         addPlaylist(playlistToAdd) {
  59.             if (!playlistToAdd instanceof PlayList) {
  60.                 throw new Error("playlistToAdd is not a PlayList");
  61.             }
  62.             this._playlists.push(playlistToAdd);
  63.             return this;
  64.         }
  65.  
  66.         getPlaylistById(id) {
  67.             return this._playlists.find(p => p.id === id) || null;
  68.         }
  69.  
  70.         removePlaylist(by) {
  71.             let id;
  72.  
  73.             if (by instanceof PlayList) {
  74.                 id = by.id;
  75.             }
  76.             else {
  77.                 id = by;
  78.             }
  79.             let index = this._playlists.findIndex(p => p.id === id);
  80.             if (index < 0) {
  81.                 throw new Error("Playlist with that id does not exist");
  82.             }
  83.             this._playlists.splice(index, 1);
  84.             return this;
  85.         }
  86.  
  87.         listPlaylists(page, size) {
  88.             if (page < 0 || siza <= 0 || this._playlists.length <= page * size) {
  89.                 throw new Error("Invalid input");
  90.             }
  91.  
  92.             return this._playlists.slice()
  93.                 .sort((x, y) => {
  94.                     if (x.name === y.name) {
  95.                         return x.id - y.id;
  96.                     }
  97.  
  98.                     return x.name.localeCompare(y.name);
  99.                 })
  100.                 .slice(page * size, (page + 1) * size);
  101.         }
  102.  
  103.         contains(playable, playlist) {
  104.             let currentPlayList = this.getPlaylistById(playlist.id);
  105.             let doesContain = currentPlayList.getPlayableById(playable.id);
  106.             return doesContain !== null ? true : false;
  107.         }
  108.  
  109.         search(pattern) {
  110.             return this._playlists.filter(p => p._playables.some(playable => {
  111.                 return (playable.title.indexOf(pattern) > -1);
  112.             })).map(playlist => ({ id: playlist.id, name: playlist.name }));
  113.         }
  114.     }
  115.  
  116.     class PlayList {
  117.         constructor(name) {
  118.             this.name = name;
  119.             this._id = getNextId.next().value;
  120.             this._playables = [];
  121.         }
  122.  
  123.         get name() {
  124.             return this._name;
  125.         }
  126.  
  127.         set name(value) {
  128.             validateString(value);
  129.             this._name = value;
  130.         }
  131.  
  132.         get id() {
  133.             return this._id;
  134.         }
  135.  
  136.         get playables(){
  137.             return this._playables.slice();
  138.         }
  139.  
  140.         addPlayable(playableToAdd) {
  141.             if (!playableToAdd instanceof Playable) {
  142.                 throw new Error("playableToAdd is not a Playable");
  143.             }
  144.             this._playables.push(playableToAdd);
  145.             return this;
  146.         }
  147.  
  148.         getPlayableById(id) {
  149.             return this._playables.find(p => p.id === id) || null;
  150.         }
  151.  
  152.         removePlayable(by) {
  153.             let id;
  154.             if (validatePlayable(by)) {
  155.                 id = by.id;
  156.             }
  157.             else {
  158.                 id = by;
  159.             }
  160.             let index = this._playables.findIndex(p => p.id === id);
  161.             if (index < 0) {
  162.                 throw new Error("Playable with that id does not exist");
  163.             }
  164.             this._playables.splice(index, 1);
  165.             return this;
  166.         }
  167.  
  168.         listPlayables(page, size) {
  169.             if (page < 0 || size <= 0 || this._playables.length <= page * size) {
  170.                 throw new Error("Invalid input");
  171.             }
  172.  
  173.             return this._playables.slice()
  174.                 .sort((x, y) => {
  175.                     if (x.title === y.title) {
  176.                         return x.id - y.id;
  177.                     }
  178.                     return x.title.localeCompare(y.title);
  179.                 })
  180.                 .slice(page * size, (page + 1) * size);
  181.         }
  182.     }
  183.  
  184.     class Playable {
  185.         constructor(title, author) {
  186.             this.title = title;
  187.             this.author = author;
  188.             this._id = getNextId.next().value;
  189.         }
  190.  
  191.         get title() {
  192.             return this._title;
  193.         }
  194.  
  195.         set title(value) {
  196.             validateString(value);
  197.             this._title = value;
  198.         }
  199.  
  200.         get author() {
  201.             return this._author;
  202.         }
  203.  
  204.         set author(value) {
  205.             validateString(value);
  206.             this._author = value;
  207.         }
  208.  
  209.         get id() {
  210.             return this._id;
  211.         }
  212.  
  213.         play() {
  214.             return ("[" + this.id + "]. [" + this.title + "] - [" + this.author + "]");
  215.         }
  216.     }
  217.  
  218.     class Audio extends Playable {
  219.         constructor(title, author, length) {
  220.             super(title, author);
  221.             this.length = length;
  222.         }
  223.  
  224.         get length() {
  225.             return this._length;
  226.         }
  227.  
  228.         set length(value) {
  229.             Validator.validateNumberInRange(value, 1, Infinity, "length must be number greater than 0")
  230.  
  231.             this._length = value;
  232.         }
  233.  
  234.         play() {
  235.             return (super.play() + " - [" + this.length + "]");
  236.         }
  237.     }
  238.  
  239.     class Video extends Playable {
  240.         constructor(title, author, imdbRating) {
  241.             super(title, author);
  242.             this.imdbRating = imdbRating;
  243.         }
  244.  
  245.         get imdbRating() {
  246.             return this._imdbRating;
  247.         }
  248.  
  249.         set imdbRating(value) {
  250.             if (typeof value !== "number" || value < 1 || 5 < value) {
  251.                 throw new Error("imdbRating must be between 1 and 5");
  252.             }
  253.             this._imdbRating = value;
  254.         }
  255.  
  256.         play() {
  257.             return (super.play() + " - [" + this.imdbRating + "]");
  258.         }
  259.     }
  260.  
  261.     const module = {
  262.         getPlayer: function (name) {
  263.             return new Player(name);
  264.         },
  265.         getPlaylist: function (name) {
  266.             return new PlayList(name);
  267.         },
  268.         getAudio: function (title, author, length) {
  269.             return new Audio(title, author, length);
  270.         },
  271.         getVideo: function (title, author, imdbRating) {
  272.             return new Video(title, author, imdbRating);
  273.         }
  274.     };
  275.  
  276.     return module;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement