npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dynamic-object-mapper

v0.0.2

Published

>A dynamic object mapper for Node.js

Downloads

5

Readme

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