Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function movies(input) {
- moviesSheet = new Map();
- const propertiesMapper = {
- onDate: "date",
- directedBy: "director",
- };
- function findComand(commandLine) {
- return commandLine.startsWith("addMovie") ? "addMovie" : "addPropertyToMovie";
- };
- const actions = {
- split: function(string,key) {
- return string.split(key).map(e => e.trim());
- },
- addMovie: function(commanLine) {
- const [_, movieName] = this.split(commanLine, "addMovie");
- if (!moviesSheet.has(movieName))
- moviesSheet.set(movieName, {});
- },
- addPropertyToMovie: function(commanLine) {
- Object.keys(propertiesMapper).map((key) => {
- if (commanLine.includes(key)) {
- const [movieName, propertyValue] = this.split(commanLine, key);
- const movie = moviesSheet.get(movieName);
- if (movie) {
- const property = propertiesMapper[key];
- movie[property] = propertyValue;
- }
- }
- });
- }
- };
- input.map((commandLine) => {
- actions[findComand(commandLine)](commandLine);
- });
- for (let [key, value] of moviesSheet) {
- if (Object.keys(value).length === Object.keys(propertiesMapper).length) {
- console.log(JSON.stringify({ name: key, ...value }));
- }
- }
- }
- movies([
- "addMovie Fast and Furious",
- "addMovie Godfather",
- "Inception directedBy Christopher Nolan",
- "Godfather directedBy Francis Ford Coppola",
- "Godfather onDate 29.07.2018",
- "Fast and Furious onDate 30.07.2018",
- "Batman onDate 01.08.2018",
- "Fast and Furious directedBy Rob Cohen",
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement