1.0.3 • Published 12 months ago

@carlosmta_/pure-js-mapper v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Pure JS Mapper

Tests Version

Pure JS Mapper, as its name suggests, is a mapping library for converting entities to DTO classes and vice versa that works at runtime.

Installation

This is a re-upload of a library that is outdated in another organization from an old account (@carlosmta).

Please use the new organization name to receive updates (@carlosmta_).

npm

npm i @carlosmta_/pure-js-mapper

yarn

yarn add @carlosmta_/pure-js-mapper

pnpm

pnpm install @carlosmta_/pure-js-mapper

Importing/Requiring

The library output is CommonJS for maximum compatibility.

const Mapper = require('@carlosmta_/pure-js-mapper').default; // CommonJS
import Mapper from '@carlosmta_/pure-js-mapper'; // ES

Usage

// SupermarketDto.js
export default class SuperMarketDto {
  constructor({ address, location, employees }) {
    this.address = address;
    this.location = location;
    this.employees = employees;
  }
}

const Supermarket = {
  address: 'X Street',
  location: 'Spain',
};

const dto = Mapper().map(Supermarket, SupermarketDto).get();

// Outputs

{
  address: 'X Street',
  location: 'Spain',
  employees: undefined
}

API

There are several chained functions that can be used to modify the output.

Global properties

You can set the ignoreUnknownProperties option globally the following way:

Mapper().Globals({ ignoreUnknownProperties: true });

Ignoring undefined or null properties

You may want to ignore some properties that your object does not have set or that are set to null. To do this you can chain map() with ignoreUnknownProperties() to remove them from your dto.

// SupermarketDto.js
export default class SuperMarketDto {
  constructor({ address, location, employees }) {
    this.address = address;
    this.location = location;
    this.employees = employees;
  }
}

const Supermarket = {
  address: 'X Street',
  location: 'Spain',
};

const dto = Mapper().map(Supermarket, SupermarketDto).ignoreUnknownProperties().get();

// Outputs

{
  address: 'X Street',
  location: 'Spain',
}

Using mappings

The setMapping() function allows nested values of your object not to be added to the DTO instance with all its properties but to be mapped according to a specified DTO class.

To add mappings you can use the setMapping() function as many times as you want indicating the property that contains the object to be mapped and the class to which it should be mapped.

// SupermarketDto.js
export default class SuperMarketDto {
  constructor({ address, location, employees }) {
    this.address = address;
    this.location = location;
    this.employees = employees;
  }
}

// EmployeeDto.js
export default class EmployeeDto {
  constructor({ name, surnames, email, phone, managers }) {
    this.name = name;
    this.email = email;
    this.phone = phone;
  }
}


const Supermarket = {
  address: 'X Street',
  location: 'Spain',
  employees: [
    {
      name: 'Carlos',
      email: 'carlos@carlos.com',
      phone: '+34 213 12 321',
      password: '1234'
    }
  ]
};

const dto = Mapper().map(Supermarket, SupermarketDto).setMapping('employees', EmployeeDto).get();

// Outputs

{
  address: 'X Street',
  location: 'Spain',
  employees: [
    {
      name: 'Carlos',
      email: 'carlos@carlos.com',
      phone: '+34 213 12 321',
      password: undefined
    }
  ]
}

Using aliases

Aliases allow mapping properties with different names.

Set the path to the property in the source object that should be mapped to a property in your target class.

// SupermarketDto.js
export default class SuperMarketDto {
  constructor({ address, location, employees }) {
    this.address = address;
    this.location = location;
    this.employees = employees;
  }
}

const Supermarket = {
  address: 'X Street',
  location: 'Spain',
  count: {
    employeeNumber: 10
  }
};

const dto = Mapper().map(Supermarket, SupermarketDto).setAlias("count.employeeNumber", "employees").get();

// Outputs

{
  address: 'X Street',
  location: 'Spain',
  employees: 10
}
1.0.3

12 months ago

1.0.2

1 year ago