Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@arg-def/mapper-js

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@arg-def/mapper-js

Fast, reliable and intuitive object mapping.

  • 0.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-20%
Maintainers
1
Weekly downloads
 
Created
Source

@args-def/mapper-js

Fast, reliable and intuitive object mapping.

NPM Version Build Status Downloads Stats GitHub stars Known Vulnerabilities GitHub issues Awesome

Installation

npm install @arg-def/mapper-js --save
#or
yarn add @arg-def/mapper-js

How to use

1) Know the structure from your source data

Before we start, it is essential that we know your data structure so we can map it accordingly.

For this demo case, let's assume that we have the following object:

const source = {
  person: {
    name: {
      firstName: 'John',
      lastName: 'Doe'
    },
    age: 32,
    drinks: ['beer', 'whiskey'],
    address: [
      {
        street: 'Infinite Loop',
        city: 'Cupertino',
        state: 'CA',
        postalCode: 95014,
        country: 'United States'
      },
      {
        street: '1600 Amphitheatre',
        city: 'Mountain View',
        state: 'CA',
        postalCode: 94043,
        country: 'United States',
      },
    ]
  }
}

2) Create your mapping using dot notation

At this step, we need to create our mapping against our data source.

We will be using dot notation to create our final structure.

For more info about dot notation API, check out the documentation

With mapper, it is possible to get one or several values from our source and even transform it in the way we need.

For that, map.get() accepts single dot notation path or an array of dot notation paths. E.g.: map.get('person.name.firstName'), map.get([person.name.firstName, person.name.lastName]);'

Those values can be transformed by using the .transform() method, which expects a function as argument and provides the selected values as array in the parameter.

For more information about the usage, check the API Documentation.

Now let's create our mapping!

import mapper from '@arg-def/mapper-js'

...

const mapping = mapper.mapping((map) => ({
  'person.name': map
                .get('person.name')
                .transform(([{ firstName, lastName }]) => `${firstName} ${lastName}`)
                .value,
  'person.lastName': map.get('person.lastName').value,
  'person.isAllowedToDrive': map.get(['person.age', 'person.drinks'])
  				.transform(([age, drinks]) => age > 18 && drinks.includes('soft-drink'))
  				.value,
  address: map.get('person.address').value,
  defaultAddress: map.get('person.address[0]').value,
}));

3) Create your mapped object

import mapper from '@arg-def/mapper-js'
...

const result = mapper(source, mapping);
/* outputs 
{
  person: {
    name: 'John Doe',
    isAllowedToDrive: false,
  },
  address: [
    {
      street: 'Infinite Loop',
      city: 'Cupertino',
      state: 'CA',
      postalCode: 95014,
      country: 'United States'
    },
    ...
  ],
  defaultAddress: {
    street: 'Infinite Loop',
    city: 'Cupertino',
    state: 'CA',
    postalCode: 95014,
    country: 'United States'
  }
}
*/

API Documentation

mapper

Type: function() Parameter: source, mapping Signature: (callback: (map: IMap) => IMapping): Function => callback

Description:

mapper() mappes your source data against your mapping.

Example:

mapper(source, mapping);

/* outputs 
{
  employee: {
    name: 'John',
    age: 32,
    address: [
      {
        street: 'Infinite Loop',
        city: 'Cupertino',
        state: 'CA',
        postalCode: 95014,
        country: 'United States'
      },
      ...
    ],
  },
}
*/

mapper.mapping

Type: function() Parameter: map Signature: (callback: (map: IMap) => IMapping): Function => callback

Description:

mapper.mapping() is the method responsible for mapping the values from your source data against your object shape. It accepts dot notation path as key.

Example:

// raw definition
const mapping = mapper.mapping((map) => {
    ...
});

// with map() query
const mapping = mapper.mapping((map) => {
  'employee.name': map.get('person.name.firstName').value,
  'employee.age': map.get('person.name.age').value,
  'employee.address': map.get('person.address').value,
});



map

Type: object

Properties

get

Type: function Parameter: string|string[] Signature: <T>(key: string | string[]) => IMap;

Description:

.get method retrieves values from your source data using dot notation path, it accepts a string or array of string.

Example:

map.get('person.name.firstName');
map.get(['person.name.firstName', 'person.name.lastName']);
transform

Type: function Parameter: any[] Signature: <T, V>(callback: (values: T[]) => V) => IMap

Description:

.transform method provides you the ability to transform the retrieved values from .get() according to your needs, and for that, it expects a return value.

.transform provides you as parameter, an array with the retrieved values in the same order as defined in the get method.

Example:

// single value
map.get('person.name.firstName')
   .transform(([firstName]) => firstName.toLoweCase());

// multiple values
map.get(['person.name.firstName', 'person.name.lastName'])
   .transform(([firstName, lastName]) => `${firstName} ${lastName]`);
value

Type: readonly Returns: any | any[] | undefined | null Description:

.value returns the value of your dot notation query. If transformed, returns the transformed value.

Example:

// single value
map.get('person.name.firstName')
   .transform(([firstName]) => firstName.toLoweCase())
   .value;

// multiple values
map.get(['person.name.firstName', 'person.name.lastName'])
   .transform(([firstName, lastName]) => `${firstName} ${lastName]`)
   .value;

Keywords

FAQs

Package last updated on 05 Oct 2019

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