transmogrifier
   A JavaScript Utility
for Transforming Objects
trans·mog·ri·fy
•It’s a real word
   trăns-mŏg'rə-fī', trănz-
   tr.v, -fied, -fy·ing, -fies.

   To change into a different
   shape or form, especially one
   that is fantastic or bizarre.
   http://www.answers.com/transmogrify
trans·mog·ri·fy
•It’s a real word
   trăns-mŏg'rə-fī', trănz-
   tr.v, -fied, -fy·ing, -fies.

   To change into a different
   shape or form, especially one
   that is fantastic or bizarre.
   http://www.answers.com/transmogrify
what’s it for?
what’s it for?
really, what’s it for?


•To sanitize and simplify complex
  objects, like those returned by
  MLB’s services
•Inspired by getDeepValue and
  setDeepValue
•Implemented in $Q for transforming
  objects as defined in .select method
wywinwyg
wywinwyg
 what you want is not what you get
what you need
{

   "id":

   "type":

   "status":

   "venue":

   "teams": {

   
    "away": {

   
    
    "id":

   
    
    "name":

   
    
    "code":

   
    },

   
    "home": {

   
    
    "id":

   
    
    "name":

   
    
    "code":

   
    }

   },

   "pitchers": [],

   "apple":

   "banana":

   "yellow":
}
what you ge...
{

        "game_id": "2009/09/24/cinmlb-pitmlb-1",

        "game_pk": "246475",

        "game_time_is_tbd": false,

        "game_venue": "PNC Park",

        "game_time": "1253810100000",

        "game_time_offset_eastern": "-4",

        "pitcher": {

        
        "loss_id": "450203",

        
        "win_id": "276520",

        
        "win": "Arroyo",

        
        "loss_stat": "4-9",

        
        "win_stat": "14-13",

        
        "save": "Cordero",

        
        "save_stat": "39",

        
        "loss": "Morton",

        
        "save_id": "150188"

        },

        "game_location": "Pittsburgh",

        "home": {

        
        "file_code": "pit",

        
        "probable_stat": "5-9",

        
        "tv": null,

        
        "id": "134",

        
        "split": false,

        
        "probable_name_display_first_last": "Charlie Morton",

        
        "probable_report": "Morton showed some improved command in his last start, a sign that maybe he's beginning to figure out how
to use the movement he can get on his pitches to his benefit. The consistency of his command has been the biggest problem for Morton all
season, but after allowing an early run and getting out of an early jam, Morton found just that against the Padres. He matched his career high
with seven innings and has now allowed three or fewer runs in four of his past five starts. Morton is 1-1 with a 6.35 ERA in two previous
starts against Cincinnati.",

        
        "recap": "/news/gameday_recap.jsp?ymd=20090924&content_id=7131310&vkey=recap&fext=.jsp&c_id=pit",

        
        "full": "Pirates",

        
        "tickets": null,

        
        "radio": "WPGB-FM 104.7",

        
        "game_time_offset": "-4",

        
        "display_code": "PIT",
...eeeeee...

        
        "preview": "javascript:void(launchGameday({gid:'2009_09_24_cinmlb_pitmlb_1',mode:'preview',lurl:'/news/article.jsp?
ymd=20090923&content_id=7109806&vkey=news_pit&fext=.jsp&c_id=pit'}))",

        
        "wrapup": "/news/wrap.jsp?ymd=20090924&content_id=7131314&vkey=wrapup2005&fext=.jsp&team=home&c_id=pit",

        
        "audio_uri":
"javascript:bam.media.launchPlayer({content_id:'6765579',view_key:'MLBTV_MP_4',media_type:'audio',calendar_event_id:'14-246475-2009-09-24'})",

        
        "probable": "Morton",

        
        "probable_era": "4.55",

        
        "league": "104",

        
        "probable_id": "450203",

        
        "result": "1"

        },

        "division_id": "205",

        "game_time_offset_local": "-4",

        "scheduledTime": null,

        "game_status": "F",

        "mlbtv": true,

        "is_suspension_resumption": false,

        "video_uri":
"javascript:bam.media.launchPlayer({content_id:'6765575',view_key:'MLBTV_MP_4',media_type:'video',calendar_event_id:'14-246475-2009-09-24'})",

        "sport_code": "mlb",

        "venue_id": "31",

        "wrapup": "javascript:void(launchGameday({gid:'2009_09_24_cinmlb_pitmlb_1',mode:'wrap',lurl:'/news/wrap.jsp?
ymd=20090924&content_id=7131314&vkey=wrapup2005&fext=.jsp&c_id=mlb'}))",

        "preview": null,

        "game_type": "R",

        "resumptionTime": null,

        "away": {

        
        "file_code": "cin",

        
        "probable_stat": "15-13",

        
        "tv": null,

        
        "id": "113",

        
        "split": false,

        
        "probable_name_display_first_last": "Bronson Arroyo",

        
        "probable_report": "Arroyo was perfect through three innings Saturday against Florida, but a couple of costly mistakes led to
a loss, most notably a changeup to pinch-hitter Ross Gload that resulted in a two-run home run in the eighth, which lifted the Marlins to a
3-2 victory. Arroyo allowed three runs on six hits. He walked one and struck out six but allowed two home runs. Despite the loss, Arroyo
extended his streak of consecutive quality starts to 11.",

        
        "recap": "/news/gameday_recap.jsp?ymd=20090924&content_id=7131308&vkey=recap&fext=.jsp&c_id=cin",

        
        "full": "Reds",

        
        "tickets": null,
...et

        
        "radio": "WLW 700",

        
        "game_time_offset": "-4",

        
        "display_code": "CIN",

        
        "preview": "javascript:void(launchGameday({gid:'2009_09_24_cinmlb_pitmlb_1',mode:'preview',lurl:'/news/article.jsp?
ymd=20090923&content_id=7109796&vkey=news_cin&fext=.jsp&c_id=cin'}))",

        
        "wrapup": "/news/wrap.jsp?ymd=20090924&content_id=7131314&vkey=wrapup2005&fext=.jsp&team=away&c_id=cin",

        
        "audio_uri":
"javascript:bam.media.launchPlayer({content_id:'6765579',view_key:'MLBTV_MP_4',media_type:'audio',calendar_event_id:'14-246475-2009-09-24'})",

        
        "probable": "Arroyo",

        
        "probable_era": "3.84",

        
        "league": "104",

        
        "probable_id": "276520",

        
        "result": "4"

        },

        "game_dh": null,

        "game_num": "1",

        "fruit": [

        
        {

        
        
        "name": "apple",

        
        
        "color": "green"

        
        },

        
        {

        
        
        "name": "banana",

        
        
        "color": "yellow"

        
        },

        
        {

        
        
        "name": "cherry",

        
        
        "color": "red"

        
        }

        ]
}
and that’s just one!
how it works
var gameTemplate = {
  id:      'game_id',                  // output.id === input.game_id
  type:    'game_type',                // output.type === input.game_type
  status: 'game_status',               // output.status === input.game_status
  venue: 'game_venue',                 // output.venue === input.game_venue
  teams: {                             // output.teams === {}
     away: {                           // output.teams.away === {}
       id:   'away.id',                // output.teams.away.id === input.away.id
       name: 'away.full',              // output.teams.away.name === input.away.full
       code: 'away.file_code'          // output.teams.away.code === input.away.file_code
     },
     home: {                           // output.teams.home === {}
       id:   'home.id',                // output.teams.home.id === input.home.id
       name: 'home.full',              // output.teams.home.name === input.home.full
       code: 'home.file_code'          // output.teams.home.code === input.home.file_code
     }
  },
  time: function convertGameTime (obj) {
     var d = new Date(+obj.game_time),
         e = +obj.game_time_offset_eastern,
         h, m, a;
     d.setHours(d.getUTCHours() + e);
     h = d.getHours();
     m = d.getMinutes();
     h = h % 12 || 12;
     m = m < 10 ? '0' + m : m;
     a = h < 12 ? 'am' : 'pm';
     return h + ':' + m + a;           // output.time === input.game_time (h:mmaa)
  },
  pitchers: [                          // output.pitchers === []
     'pitcher.win',                    // output.pitchers[0] === input.pitcher.win
     'pitcher.loss',                   // output.pitchers[1] === input.pitcher.loss
     'pitcher.save'                    // output.pitchers[2] === input.pitcher.save
  ],
  apple: 'fruit.0',                    // output.apple === input.fruit[0]
  banana: 'fruit[1]',                  // output.banana === input.fruit[1]
  yellow: 'fruit[1].color'             // output.yellow === input.fruit[1].color
};

var gameTransmogrifier = new Transmogrifier(gameTemplate);
gameTransmogrifier.zap(gameObj);

{

   "id": "2009/09/24/cinmlb-pitmlb-1",

   "type": "R",

   "status": "F",

   "venue": "PNC Park",

   "teams": {

   
      "away": {

   
      
      "id": "113",

   
      
      "name": "Reds",

   
      
      "code": "cin"

   
      },

   
      "home": {

   
      
      "id": "134",

   
      
      "name": "Pirates",

   
      
      "code": "pit"

   
      }

   },

   "time": "12:35pm",

   "pitchers": [

   
      "Arroyo",

   
      "Morton",

   
      "Cordero"

   ],

   "apple": {

   
      "name": "apple",

   
      "color": "green"

   },

   "banana": {

   
      "name": "banana",

   
      "color": "yellow"

   },

   "yellow": "yellow"
}
two methods
.zap (objOrArr)
Transforms a single object or array
.nuke (arrOfObjs)
 Applies .zap to members of array
Transmogrify
Transmogrify

Transmogrify

  • 1.
    transmogrifier A JavaScript Utility for Transforming Objects
  • 2.
    trans·mog·ri·fy •It’s a realword trăns-mŏg'rə-fī', trănz- tr.v, -fied, -fy·ing, -fies. To change into a different shape or form, especially one that is fantastic or bizarre. http://www.answers.com/transmogrify
  • 3.
    trans·mog·ri·fy •It’s a realword trăns-mŏg'rə-fī', trănz- tr.v, -fied, -fy·ing, -fies. To change into a different shape or form, especially one that is fantastic or bizarre. http://www.answers.com/transmogrify
  • 4.
  • 5.
  • 6.
    really, what’s itfor? •To sanitize and simplify complex objects, like those returned by MLB’s services
  • 7.
    •Inspired by getDeepValueand setDeepValue •Implemented in $Q for transforming objects as defined in .select method
  • 8.
  • 9.
    wywinwyg what youwant is not what you get
  • 10.
    what you need { "id": "type": "status": "venue": "teams": { "away": { "id": "name": "code": }, "home": { "id": "name": "code": } }, "pitchers": [], "apple": "banana": "yellow": }
  • 11.
    what you ge... { "game_id": "2009/09/24/cinmlb-pitmlb-1", "game_pk": "246475", "game_time_is_tbd": false, "game_venue": "PNC Park", "game_time": "1253810100000", "game_time_offset_eastern": "-4", "pitcher": { "loss_id": "450203", "win_id": "276520", "win": "Arroyo", "loss_stat": "4-9", "win_stat": "14-13", "save": "Cordero", "save_stat": "39", "loss": "Morton", "save_id": "150188" }, "game_location": "Pittsburgh", "home": { "file_code": "pit", "probable_stat": "5-9", "tv": null, "id": "134", "split": false, "probable_name_display_first_last": "Charlie Morton", "probable_report": "Morton showed some improved command in his last start, a sign that maybe he's beginning to figure out how to use the movement he can get on his pitches to his benefit. The consistency of his command has been the biggest problem for Morton all season, but after allowing an early run and getting out of an early jam, Morton found just that against the Padres. He matched his career high with seven innings and has now allowed three or fewer runs in four of his past five starts. Morton is 1-1 with a 6.35 ERA in two previous starts against Cincinnati.", "recap": "/news/gameday_recap.jsp?ymd=20090924&content_id=7131310&vkey=recap&fext=.jsp&c_id=pit", "full": "Pirates", "tickets": null, "radio": "WPGB-FM 104.7", "game_time_offset": "-4", "display_code": "PIT",
  • 12.
    ...eeeeee... "preview": "javascript:void(launchGameday({gid:'2009_09_24_cinmlb_pitmlb_1',mode:'preview',lurl:'/news/article.jsp? ymd=20090923&content_id=7109806&vkey=news_pit&fext=.jsp&c_id=pit'}))", "wrapup": "/news/wrap.jsp?ymd=20090924&content_id=7131314&vkey=wrapup2005&fext=.jsp&team=home&c_id=pit", "audio_uri": "javascript:bam.media.launchPlayer({content_id:'6765579',view_key:'MLBTV_MP_4',media_type:'audio',calendar_event_id:'14-246475-2009-09-24'})", "probable": "Morton", "probable_era": "4.55", "league": "104", "probable_id": "450203", "result": "1" }, "division_id": "205", "game_time_offset_local": "-4", "scheduledTime": null, "game_status": "F", "mlbtv": true, "is_suspension_resumption": false, "video_uri": "javascript:bam.media.launchPlayer({content_id:'6765575',view_key:'MLBTV_MP_4',media_type:'video',calendar_event_id:'14-246475-2009-09-24'})", "sport_code": "mlb", "venue_id": "31", "wrapup": "javascript:void(launchGameday({gid:'2009_09_24_cinmlb_pitmlb_1',mode:'wrap',lurl:'/news/wrap.jsp? ymd=20090924&content_id=7131314&vkey=wrapup2005&fext=.jsp&c_id=mlb'}))", "preview": null, "game_type": "R", "resumptionTime": null, "away": { "file_code": "cin", "probable_stat": "15-13", "tv": null, "id": "113", "split": false, "probable_name_display_first_last": "Bronson Arroyo", "probable_report": "Arroyo was perfect through three innings Saturday against Florida, but a couple of costly mistakes led to a loss, most notably a changeup to pinch-hitter Ross Gload that resulted in a two-run home run in the eighth, which lifted the Marlins to a 3-2 victory. Arroyo allowed three runs on six hits. He walked one and struck out six but allowed two home runs. Despite the loss, Arroyo extended his streak of consecutive quality starts to 11.", "recap": "/news/gameday_recap.jsp?ymd=20090924&content_id=7131308&vkey=recap&fext=.jsp&c_id=cin", "full": "Reds", "tickets": null,
  • 13.
    ...et "radio": "WLW 700", "game_time_offset": "-4", "display_code": "CIN", "preview": "javascript:void(launchGameday({gid:'2009_09_24_cinmlb_pitmlb_1',mode:'preview',lurl:'/news/article.jsp? ymd=20090923&content_id=7109796&vkey=news_cin&fext=.jsp&c_id=cin'}))", "wrapup": "/news/wrap.jsp?ymd=20090924&content_id=7131314&vkey=wrapup2005&fext=.jsp&team=away&c_id=cin", "audio_uri": "javascript:bam.media.launchPlayer({content_id:'6765579',view_key:'MLBTV_MP_4',media_type:'audio',calendar_event_id:'14-246475-2009-09-24'})", "probable": "Arroyo", "probable_era": "3.84", "league": "104", "probable_id": "276520", "result": "4" }, "game_dh": null, "game_num": "1", "fruit": [ { "name": "apple", "color": "green" }, { "name": "banana", "color": "yellow" }, { "name": "cherry", "color": "red" } ] }
  • 14.
  • 15.
  • 16.
    var gameTemplate ={ id: 'game_id', // output.id === input.game_id type: 'game_type', // output.type === input.game_type status: 'game_status', // output.status === input.game_status venue: 'game_venue', // output.venue === input.game_venue teams: { // output.teams === {} away: { // output.teams.away === {} id: 'away.id', // output.teams.away.id === input.away.id name: 'away.full', // output.teams.away.name === input.away.full code: 'away.file_code' // output.teams.away.code === input.away.file_code }, home: { // output.teams.home === {} id: 'home.id', // output.teams.home.id === input.home.id name: 'home.full', // output.teams.home.name === input.home.full code: 'home.file_code' // output.teams.home.code === input.home.file_code } }, time: function convertGameTime (obj) { var d = new Date(+obj.game_time), e = +obj.game_time_offset_eastern, h, m, a; d.setHours(d.getUTCHours() + e); h = d.getHours(); m = d.getMinutes(); h = h % 12 || 12; m = m < 10 ? '0' + m : m; a = h < 12 ? 'am' : 'pm'; return h + ':' + m + a; // output.time === input.game_time (h:mmaa) }, pitchers: [ // output.pitchers === [] 'pitcher.win', // output.pitchers[0] === input.pitcher.win 'pitcher.loss', // output.pitchers[1] === input.pitcher.loss 'pitcher.save' // output.pitchers[2] === input.pitcher.save ], apple: 'fruit.0', // output.apple === input.fruit[0] banana: 'fruit[1]', // output.banana === input.fruit[1] yellow: 'fruit[1].color' // output.yellow === input.fruit[1].color }; var gameTransmogrifier = new Transmogrifier(gameTemplate);
  • 18.
    gameTransmogrifier.zap(gameObj); { "id": "2009/09/24/cinmlb-pitmlb-1", "type": "R", "status": "F", "venue": "PNC Park", "teams": { "away": { "id": "113", "name": "Reds", "code": "cin" }, "home": { "id": "134", "name": "Pirates", "code": "pit" } }, "time": "12:35pm", "pitchers": [ "Arroyo", "Morton", "Cordero" ], "apple": { "name": "apple", "color": "green" }, "banana": { "name": "banana", "color": "yellow" }, "yellow": "yellow" }
  • 19.
  • 20.
    .zap (objOrArr) Transforms asingle object or array
  • 21.
    .nuke (arrOfObjs) Applies.zap to members of array