0.0.2 • Published 5 years ago

dynamic-object-mapper v0.0.2

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

dinamic-mapper-json

A dynamic object mapper for Node.js

Description

This library takes a configuration JSON object, some input data and generates an output object with the properties that are specified in the config object. It is also capable of making transformations with the properties of the object that are needed. In this way you can adapt the input data to an expected output, for example, modify a price variable by performing a calculation for the output object.

Why JSON?

The main idea of this module is to be able to use it in projects that use a database. In this way it is possible to save this configuration object in the database and pass it to the module dynamically so that it makes the necessary changes in the data.

Usage

const  mapper = require('dynamic-mapper-json');
const config = {
	"name": {
		"dest":  "nombre"
	},
	"price": {
		"dest":  "precio",
		"transform": {
			"method":  "percentage",
			"action":  "addPercentage",
			"value":  10
		}
	},
	"wholesale": {
		"dest":  "precioCompra",
		"transform": [
			{
				"method":  "manipulate",
				"action":  "stringToFloat"
			},
			{
				"method":  "percentage",
				"action":  "subPercentage",
				"value":  10
			}]

	},
}
const  data  = {
	name:  "Producto de test",
	price:  125,
	wholesale:  "28"
}
let output;

mapper.doMap(config, data, (err, result) => {
	output = result;
	// Returns this:
	// {
	//		nombre: "Producto de test",
	//		precio: 137.5,
	//		precioCompra: 25.45
	// }
})

API

Config

Each key of the config object must correspond to the keys of the data object in order to be able to parse it. The value of each key can contain the following variables:

  • dest(required): the key that will be created in the destination object.
  • transform: If you need to change the value of this variable for the output object, you can perform mathematical operations, calculate percentages and make changes of type (string to number, number to string, change to uppercase, lowercase, etc) Have this options: method(required): The method for apply (described below) options: manipulate, percentage, math action(required): The action to apply (described below) value: Value to pass (if needed), for calculate operations (percentage, math)

Transform methods and actions

Manipulate

Performs type change operations.

Actions
  • stringToFloat: Transform a String to a Float type.
  • stringToInt: Transform a string to a Integer type.
  • intToString: Transforms a Integer to a string.
  • toLowerCase: Transforms a string to lower case.
  • toUpperCase: Transforms a string to upper case.

Percentage

Calculates percentages.

Actions
  • addPercentage: Adds a percentage to a number. Expects value
  • subPercentage: Substracts a percentage to a number. Expects value

Math

Performs simple math operations

Actions
  • add: Adds a number. Expects value
  • substract: Substract a number. Expects value
  • multiply: Multiply a number. Expects value
  • divide: Divides a number. Expects value