New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

map-factory

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

map-factory

Object mapping tool

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3K
decreased by-38.36%
Maintainers
1
Weekly downloads
 
Created
Source

map-factory

Coverage Status Build Deps devDependency Status

A simple utility to map data from an existing object to a new one. This is an alternative interface for the excellent object-mapper.

See Change Log for changes from previous versions.

Map a source field to the same object structure

Mapping is explicit so unmapped fields are discarded.

const createMapper = require("map-factory");

const source = {
  "fieldName": "name1",
  "fieldId": "123",
  "fieldDescription": "description"
};

const map = createMapper();

map("fieldName");
map("fieldId");

const result = map.execute(source);
console.log(result);

/*
  {
    "fieldName": "name1",
    "fieldId": "123"
  }
*/

Map a source field to a different object structure

Of course, we probably want a different structure for our target object.

const createMapper = require("map-factory");

const source = {
  "fieldName": "name1",
  "fieldId": "123",
  "fieldDescription": "description"
};

const map = createMapper();

map("fieldName").to("field.name");
map("fieldId").to("field.id");

const result = map.execute(source);
console.log(result);

/*
  {
    "field": {
      "name": "name1",
      "id": "123"
    }
  }
*/

Supports deep references for source and target objects

const source = {
  "person": {
    "name": "John",
    "email": "john@someplace.com",
    "phone": "(712) 123 4567"
  },
  "account": {
    "id": "abc123",
    "entitlements": [{
      "id": 1,
      "name": "game-1"
    },
      {
        "id": 2,
        "name": "game-2"
      }]
  }
};

const map = createMapper();

map("person.email").to("user.login");
map("account.id").to("user.accountId");
map("account.entitlements.[].name").to("user.entitlements");

const result = map.execute(source);
console.log(result);

/*
  {
    "user": {
      "login": "john@someplace.com",
      "accountId": "abc123",
      "entitlements": ["game-1", "game-2"]
    }
  }
*/

You can also reference specific items in an array.

const createMapper = require("map-factory");

const source = {
  "articles": [
    {
      "id": 1,
      "title": "Top Article",
      "author": "Joe Doe",
      "body": "..."
    },
    {
      "id": 2,
      "title": "Second Article",
      "author": "Joe Doe",
      "body": "..."
    }
  ]
};

const map = createMapper();

map("articles.[0]").to("topStory");

const result = map.execute(source);
console.log(result);

/*
{
  "topStory": {
    "id": 1,
    "title": "Top Article",
    "author": "Joe Doe",
    "body": "..."
  }
}
*/

More complicated transformations can be handled by providing a function.

const createMapper = require("map-factory");

const source = {
  "articles": [
    {
      "id": 1,
      "title": "Top Article",
      "author": "Joe Doe",
      "body": "..."
    },
    {
      "id": 2,
      "title": "Second Article",
      "author": "Joe Doe",
      "body": "..."
    }
  ]
};

const map = createMapper();

map("articles.[0]").to("topStory");
map("articles").to("otherStories", articles => {

  // We don't want to include the top story
  articles.shift();

  return articles;

});

const result = map.execute(source);
console.log(result);

/*
  {
    "topStory": {
      "id": 1,
      "title": "Top Article",
      "author": "Joe Doe",
      "body": "..."
    },
    "otherStories": [
      {
        "id": 2,
        "title": "Second Article",
        "author": "Joe Doe",
        "body": "..."
      }
    ]
  }
*/

An existing object can be provided as the target object.

const createMapper = require("map-factory");

const source = {
     "fieldName": "name1",
     "fieldId": "123",
     "fieldDescription": "description"
   };

const destination = {
 "existing": "data"
};

const map = createMapper();

map("fieldName").to("field.name");
map("fieldId").to("field.id");

const result = map.execute(source, destination);
console.log(result);

/*
  {
    "existing": "data",
    "field": {
        "name": "name1",
        "id": "123"
    }
  }
*/

Select from multiple sources at once

You can also provide an array or source fields and they can be extracted together. You must provide a transform for the target field.

const createMapper = require("map-factory");

const source = {
  "apples": {
    "count": 3
  },
  "oranges": {
    "count": 4
  }
};

const map = createMapper();

map(["apples.count", "oranges.count"]).to("fruit.count", (appleCount, orangeCount) => {

  return appleCount + orangeCount;

});

const result = map.execute(source);
console.log(result);

/*
  {
    fruit: {
    count: 7
    }
  }
*/

Keywords

FAQs

Package last updated on 18 Jul 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc