Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- const getNextId = (function* () {
- let counter = 1;
- while (true) {
- yield counter++;
- }
- })();
- const Validator = {
- validateString(value, message) {
- if (typeof value !== "string") {
- throw new Error(message);
- }
- },
- validateNumberInRange(value, min, max, message) {
- if (typeof value !== "number" || value < min || max < value) {
- throw new Error(message);
- }
- },
- validatePlayable(playable) {
- if (!(playable instanceof Playable)
- && (!playable.hasOwnProperty("title")
- || !playable.hasOwnProperty("author")
- || !playable.hasOwnProperty("id")
- )) {
- return false;
- }
- return true;
- }
- }
- class Player {
- constructor(name) {
- this.name = name;
- this._id = getNextId.next().value;
- this._playlists = [];
- }
- get name() {
- return this._name;
- }
- set name(value) {
- validateString(value);
- this._name = value;
- }
- get id() {
- return this._id;
- }
- addPlaylist(playlistToAdd) {
- if (!playlistToAdd instanceof PlayList) {
- throw new Error("playlistToAdd is not a PlayList");
- }
- this._playlists.push(playlistToAdd);
- return this;
- }
- getPlaylistById(id) {
- return this._playlists.find(p => p.id === id) || null;
- }
- removePlaylist(by) {
- let id;
- if (by instanceof PlayList) {
- id = by.id;
- }
- else {
- id = by;
- }
- let index = this._playlists.findIndex(p => p.id === id);
- if (index < 0) {
- throw new Error("Playlist with that id does not exist");
- }
- this._playlists.splice(index, 1);
- return this;
- }
- listPlaylists(page, size) {
- if (page < 0 || siza <= 0 || this._playlists.length <= page * size) {
- throw new Error("Invalid input");
- }
- return this._playlists.slice()
- .sort((x, y) => {
- if (x.name === y.name) {
- return x.id - y.id;
- }
- return x.name.localeCompare(y.name);
- })
- .slice(page * size, (page + 1) * size);
- }
- contains(playable, playlist) {
- let currentPlayList = this.getPlaylistById(playlist.id);
- let doesContain = currentPlayList.getPlayableById(playable.id);
- return doesContain !== null ? true : false;
- }
- search(pattern) {
- return this._playlists.filter(p => p._playables.some(playable => {
- return (playable.title.indexOf(pattern) > -1);
- })).map(playlist => ({ id: playlist.id, name: playlist.name }));
- }
- }
- class PlayList {
- constructor(name) {
- this.name = name;
- this._id = getNextId.next().value;
- this._playables = [];
- }
- get name() {
- return this._name;
- }
- set name(value) {
- validateString(value);
- this._name = value;
- }
- get id() {
- return this._id;
- }
- get playables(){
- return this._playables.slice();
- }
- addPlayable(playableToAdd) {
- if (!playableToAdd instanceof Playable) {
- throw new Error("playableToAdd is not a Playable");
- }
- this._playables.push(playableToAdd);
- return this;
- }
- getPlayableById(id) {
- return this._playables.find(p => p.id === id) || null;
- }
- removePlayable(by) {
- let id;
- if (validatePlayable(by)) {
- id = by.id;
- }
- else {
- id = by;
- }
- let index = this._playables.findIndex(p => p.id === id);
- if (index < 0) {
- throw new Error("Playable with that id does not exist");
- }
- this._playables.splice(index, 1);
- return this;
- }
- listPlayables(page, size) {
- if (page < 0 || size <= 0 || this._playables.length <= page * size) {
- throw new Error("Invalid input");
- }
- return this._playables.slice()
- .sort((x, y) => {
- if (x.title === y.title) {
- return x.id - y.id;
- }
- return x.title.localeCompare(y.title);
- })
- .slice(page * size, (page + 1) * size);
- }
- }
- class Playable {
- constructor(title, author) {
- this.title = title;
- this.author = author;
- this._id = getNextId.next().value;
- }
- get title() {
- return this._title;
- }
- set title(value) {
- validateString(value);
- this._title = value;
- }
- get author() {
- return this._author;
- }
- set author(value) {
- validateString(value);
- this._author = value;
- }
- get id() {
- return this._id;
- }
- play() {
- return ("[" + this.id + "]. [" + this.title + "] - [" + this.author + "]");
- }
- }
- class Audio extends Playable {
- constructor(title, author, length) {
- super(title, author);
- this.length = length;
- }
- get length() {
- return this._length;
- }
- set length(value) {
- Validator.validateNumberInRange(value, 1, Infinity, "length must be number greater than 0")
- this._length = value;
- }
- play() {
- return (super.play() + " - [" + this.length + "]");
- }
- }
- class Video extends Playable {
- constructor(title, author, imdbRating) {
- super(title, author);
- this.imdbRating = imdbRating;
- }
- get imdbRating() {
- return this._imdbRating;
- }
- set imdbRating(value) {
- if (typeof value !== "number" || value < 1 || 5 < value) {
- throw new Error("imdbRating must be between 1 and 5");
- }
- this._imdbRating = value;
- }
- play() {
- return (super.play() + " - [" + this.imdbRating + "]");
- }
- }
- const module = {
- getPlayer: function (name) {
- return new Player(name);
- },
- getPlaylist: function (name) {
- return new PlayList(name);
- },
- getAudio: function (title, author, length) {
- return new Audio(title, author, length);
- },
- getVideo: function (title, author, imdbRating) {
- return new Video(title, author, imdbRating);
- }
- };
- return module;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement