smart-mapper

mapping

mapping(template,data)

templatejson object that contain 2 arrays: mappings: json object that contain in keys the destination, and in values the path in the specific object to extract rules: json object that contains methods to execute on a particular field during the transformation

datadata to transform

transform data to another data according the template


      var persons = [{
        name: 'Matt',
        age: 30
      }, {
        name: 'Edouard',
        age: 28
      }, {
        name: 'Guillaume',
        age: 29
      }, {
        name: 'Roger',
        age: 51
      }];
  
  
      var template = {
        mappings: {
          FirstName: 'name'
        }
      };
  
  
      let outData = SmartMapper.mapping(template, persons);

      => produces
      outData = [
        {FirstName:"Matt"},
        {FirstName:"Edouard"},
        {FirstName:"Roger"},
      ]
  


      var entryData = [{
        person: {
          id: "1",
          firstname: "Jean",
          lastname: "Valjean",
          age: 50,
          jobs: [{
            title: "arboriste",
            type: "first"
          }, {
            title: "braconnier",
            type: "after"
          }, {
            title: "maire",
            type: "LATEST"
          }],
          friends: ["Cosette", "Fauchelevent"],
          address: {
            street: "rue de l'Ouest",
            zip: "75006",
            city: "Paris",
            country: {
              code: "FR"
            }
          }
        }
      }, {
        person: {
          id: "2",
          firstname: "Emmanuel",
          lastname: "Macron",
          age: 40,
          jobs: [{
            title: "President",
            type: "LATEST"
          }, {
            title: "Banquier",
            type: "first"
          }],
          friends: [
            "Castaner",
            "Brigitte",
            "Trump",
            "Francois",
            "Matt"
          ],
          address: {
            street: "Rue de l'Elysée",
            zip: "59000",
            city: "Paris",
            country: {
              code: "FR"
            }
          }
        }
      }, {
        person: {
          id: "3",
          firstname: "Mario",
          lastname: "",
          age: 35,
          jobs: [{
            title: "Plombier",
            type: "first"
          }],
          friends: ["Luigi", "Sonic"]
        }
      }]
  
  
      var template = {
        mappings: {
          id: "person.id",
          lastname: "person.lastname",
          firstname: "person.firstname",
          age: "person.age",
          job: "person.jobs",
          nbFriends: "person.friends",
          address: "person.address"
        },
        rules: [{
          on: "address",
          execute: address => {
            if (address != null) return [
              address.street,
              address.zip,
              address.city,
              address.country.code
            ].join(",");
            return "inconnue";
          }
        }, {
          on: "job",
          execute: jobs => {
            var latest = jobs.find(x => x.type === "LATEST");
            if (latest != null) return latest.title;
            return "inconnu";
          }
        }, {
          on: "nbFriends",
          execute: friends => friends.length
        }]
      }

      let outData = SmartMapper.mapping(template, entryData);

      => produces 
      outData = [
        {"id":"1","lastname":"Valjean","firstname":"Jean","age":50,
          "job":"maire","nbFriends":2,"address":"rue de l'Ouest,75006,Paris,FR"}
        ,{"id":"2","lastname":"Macron","firstname":"Emmanuel","age":40,
          "job":"President","nbFriends":5,"address":"Rue de l'Elysée,59000,Paris,FR"}
        ,{"id":"3","lastname":"","firstname":"Mario","age":35,
          "job":"inconnu","nbFriends":2,"address":"inconnue"}
      ];