Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function cmp (a, b) {
- var timeA = a.title.split(" ");
- var timeB = b.title.split(" ");
- let A = parseInt(timeA[1]) + 31* parseInt( months[timeA[2]] ) ;
- let B = parseInt(timeB[1]) + 31* parseInt( months[timeB[2]] ) ;
- let comparison = 0;
- if (A > B ) {
- comparison = 1;
- } else if ( A < B ) {
- comparison = -1;
- }
- return comparison;
- }
- var months = { "Sep" : 8 , "Oct" : 9 , "Nov" : 10 , "Dec" : 11 };
- var days = [ "Sat" , "Sun" , "Mon" , "Tue" , "Wed" , "Thu" , "Fri" ];
- var offset = { "Sep" : 0 , "Oct" : 2 , "Nov" : 5 , "Dec" : 0 };
- function convertTime ( time ) {
- let str = time.split(":");
- let h = parseInt(str[0]);
- if ( h >= 12 ) {
- h %=12 ;
- if ( h <= 9 ) return "0"+h+":"+str[1]+"PM";
- return h+":"+str[1]+"PM";
- }
- return time +"AM" ;
- }
- var func = ( ListOfMovies ) => {
- let ret = [];
- let usedTitles = new Object();
- let newListOfMovies = [] ;
- let currentDate = new Date() ;
- ListOfMovies.forEach ( function ( movie ){
- let currentMovie = [] ;
- currentMovie.name = movie.name ;
- currentMovie.sessions = [] ;
- let used = [] ;
- JSON.parse(movie.list_timing).forEach( function ( detail ) {
- let date = detail.showdate.split(" ");
- if ( months[date[1]] < currentDate.getMonth() ) return ;
- if ( months[date[1]] == currentDate.getMonth() && parseInt(date[0]) < currentDate.getDate() ) return ;
- let cur = days[ ((parseInt(date[0])%7) + offset[date[1]] ) % 7 ] +" "+ date[0] + " " + date[1] ;
- if ( usedTitles[cur] == null ) {
- usedTitles[cur] = 1 ;
- ret.push ( { "title": cur , "movies": [] } );
- }
- if ( used[cur] == null ) {
- used[cur] = currentMovie.sessions.length ;
- currentMovie.sessions[ used[cur] ] = {"date":cur , "screens" : [] } ;
- }
- let idx = used[cur] ;
- let nw = true ;
- let currentType = detail.cinemaOperatorCode;
- if ( currentType.length > 0 ) currentType = currentType.substr(4);
- if ( currentType.length == 0 ) currentType = "NotDefine" ;
- for ( i =0 ; i < currentMovie.sessions[ idx ].screens.length ; i++ ){
- if ( currentMovie.sessions[ idx ].screens[i].type == currentType ) {
- let toAddTime = new Object();
- toAddTime.time = convertTime( detail.timingShow );
- currentMovie.sessions[ idx ].screens[i].sessions.push(toAddTime);
- nw = false ;
- break;
- }
- }
- if ( nw === true ){
- let nwScreen = { "type" : currentType , "sessions":[] };
- let toAddTime = new Object();
- toAddTime.time = convertTime( detail.timingShow );
- nwScreen.sessions.push(toAddTime);
- currentMovie.sessions[ idx ].screens.push(nwScreen);
- }
- })
- if ( currentMovie.sessions.length > 1 ) {
- currentMovie.sessions[0].date = "Today" ;
- currentMovie.sessions[1].date = "Tomorrow" ;
- }else if ( currentMovie.sessions.length == 1 ) {
- currentMovie.sessions[0].date = "Today" ;
- }
- newListOfMovies.push(currentMovie);
- })
- ret.sort( cmp ) ;
- if ( ret.length > 1 ) {
- ret[0].title = "Today" ;
- ret[1].title = "Tomorrow" ;
- }else if ( ret.length == 1 ) {
- ret[0].title = "Today" ;
- }
- let nwIdx = [];
- for ( i = 0 ; i < ret.length ; i++ ) {
- nwIdx[ ret[i].title ] = i ;
- }
- newListOfMovies.forEach ( function ( movie ) {
- for ( i = 0; i < movie.sessions.length ; i++ ) {
- ret[ nwIdx[movie.sessions[i].date] ].movies.push(movie);
- }
- })
- return ret;
- }
- var temp = [
- {
- "id": 661,
- "name": "BOOKING/EVENT/PREMIERE",
- "list_timing": "[{\"showdate\": \"04 Oct 2019\", \"sessionId\": \"264151\", \"timingShow\": \"10:00\", \"cinemaOperatorCode\": \"VOX STANDARD\"}]"
- },
- {
- "id": 702,
- "name": "The Lion King",
- "list_timing": "[{\"showdate\": \"30 Sep 2019\", \"sessionId\": \"262767\", \"timingShow\": \"18:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"30 Sep 2019\", \"sessionId\": \"262882\", \"timingShow\": \"22:40\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"01 Oct 2019\", \"sessionId\": \"262846\", \"timingShow\": \"13:20\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"01 Oct 2019\", \"sessionId\": \"262771\", \"timingShow\": \"18:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"01 Oct 2019\", \"sessionId\": \"262887\", \"timingShow\": \"22:25\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"02 Oct 2019\", \"sessionId\": \"262851\", \"timingShow\": \"13:20\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"02 Oct 2019\", \"sessionId\": \"262775\", \"timingShow\": \"18:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"02 Oct 2019\", \"sessionId\": \"262892\", \"timingShow\": \"22:25\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"03 Oct 2019\", \"sessionId\": \"264010\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"03 Oct 2019\", \"sessionId\": \"263974\", \"timingShow\": \"17:55\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"03 Oct 2019\", \"sessionId\": \"263976\", \"timingShow\": \"22:35\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"04 Oct 2019\", \"sessionId\": \"264016\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"05 Oct 2019\", \"sessionId\": \"263946\", \"timingShow\": \"10:50\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"05 Oct 2019\", \"sessionId\": \"264022\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"05 Oct 2019\", \"sessionId\": \"263986\", \"timingShow\": \"17:55\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"05 Oct 2019\", \"sessionId\": \"263988\", \"timingShow\": \"22:35\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"06 Oct 2019\", \"sessionId\": \"264028\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"06 Oct 2019\", \"sessionId\": \"263991\", \"timingShow\": \"17:55\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"06 Oct 2019\", \"sessionId\": \"263993\", \"timingShow\": \"22:35\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"07 Oct 2019\", \"sessionId\": \"264034\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"07 Oct 2019\", \"sessionId\": \"263996\", \"timingShow\": \"17:55\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"07 Oct 2019\", \"sessionId\": \"263998\", \"timingShow\": \"22:35\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"08 Oct 2019\", \"sessionId\": \"264040\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"08 Oct 2019\", \"sessionId\": \"264001\", \"timingShow\": \"17:55\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"08 Oct 2019\", \"sessionId\": \"264003\", \"timingShow\": \"22:35\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"09 Oct 2019\", \"sessionId\": \"264046\", \"timingShow\": \"13:45\", \"cinemaOperatorCode\": \"VOX STANDARD\"}, {\"showdate\": \"09 Oct 2019\", \"sessionId\": \"264006\", \"timingShow\": \"17:55\", \"cinemaOperatorCode\": \"VOX KIDS\"}]"
- },
- {
- "id": 698,
- "name": "The Big Trip",
- "list_timing": "[{\"showdate\": \"30 Sep 2019\", \"sessionId\": \"262880\", \"timingShow\": \"18:15\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"01 Oct 2019\", \"sessionId\": \"262885\", \"timingShow\": \"18:15\", \"cinemaOperatorCode\": \"VOX KIDS\"}, {\"showdate\": \"02 Oct 2019\", \"sessionId\": \"262890\", \"timingShow\": \"18:15\", \"cinemaOperatorCode\": \"VOX KIDS\"}]"
- },
- {
- "id": 701,
- "name": "Once Upon A Time in Hollywood (Distraction Free)",
- "list_timing": "[{\"showdate\": \"30 Sep 2019\", \"sessionId\": \"262700\", \"timingShow\": \"18:45\", \"cinemaOperatorCode\": \"\"}, {\"showdate\": \"01 Oct 2019\", \"sessionId\": \"262705\", \"timingShow\": \"18:45\", \"cinemaOperatorCode\": \"\"}, {\"showdate\": \"02 Oct 2019\", \"sessionId\": \"262710\", \"timingShow\": \"18:45\", \"cinemaOperatorCode\": \"\"}, {\"showdate\": \"04 Oct 2019\", \"sessionId\": \"263807\", \"timingShow\": \"11:00\", \"cinemaOperatorCode\": \"\"}, {\"showdate\": \"05 Oct 2019\", \"sessionId\": \"263812\", \"timingShow\": \"11:00\", \"cinemaOperatorCode\": \"\"}]"
- }];
- var result = func( temp );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement