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

data-model-mapper-plus

v1.0.1

Published

Utility to map (and revert) data objects to another data model.

Downloads

9

Readme

data-model-mapper

npm module to map (and revert mapping) data objects into another (specified) data model

Usage

Let's assume we've got following object:

{
    DATE: '20160512',
    TIME: '140000',
    FIRST_NAME: 'John',
    LAST_NAME: 'Doe',
    ID: 'EMP000210290'
}

We want to transform it to match our model:

{
    id: 'EMP000210290'
    lastLogin: Thu May 12 2016 14:00:00 GMT+0200 (CEST), // JS Date object
    employeeName: 'John Doe'
}

We must create transform config:

var moment = require('moment');

var mapDateTime = function (date, time) {
    return moment(date + time, 'YYYYMMDDHHmmss').toDate(); // moment.js library
};

var revertDateTime = function (dateObject) {
    var m = moment(dateObject);
    return [m.format('YYYYMMDD'), m.format('HHmmss')]; // dateObject has to be split into 2 fields (DATE, TIME).
                                                       // Function must return array of values.
};

var conf = [
    {
        dest: 'id',
        src: 'ID'
    }, {
        dest: 'lastLogin',
        src: ['DATE', 'TIME'],
        map: mapDateTime,
        revert: revertDateTime
    }, {
        dest: 'employeeName',
        src: ['FIRST_NAME', 'LAST_NAME'],
        map: function (fName, lName) { return fName + ' ' + lName; },
        revert: function (name) { return name.split(' '); }
    }
];

From now on, it's really simple. Just include data-model-mapper, create new instance of mapper, pass config array and map your object:

var DMM = require('data-model-mapper');

var obj = {
    DATE: '20160512',
    TIME: '140000',
    FIRST_NAME: 'John',
    LAST_NAME: 'Doe',
    ID: 'EMP000210290'
};

var m = new DMM(conf);

var mapped = m.map(obj);
/* result is:
{
    id: 'EMP000210290'
    lastLogin: Thu May 12 2016 14:00:00 GMT+0200 (CEST), // JS Date object
    employeeName: 'John Doe'
}
*/

var reverted = m.revert(mapped);
/* result is original object */

Configuration

Configuration array is a set of objects defining mapping process.

src (String or Array)

Source fields names (fields from original object). Fields that you want to use in your transformation. Values of this/these field(s) will be passed to map function.

dest (String or Array)

Destination fields names (fields created in mapped object). If array is passed fields from array will be created but map function should return array of values in order to properly populate fields' values.

map Function
optional

Transform function which maps src field(s) into dest field(s). Function gets parameter value(s) form field(s) specified in src. If dest is an array function should return array of values in order to properly populate fields' values.

If map function is not defined dest field(s) should contain values from src field(s);

revert Function
optional

Transform function which maps dest field(s) into src field(s). Function gets parameter value(s) form field(s) specified in dest. If src is an array function should return array of values in order to properly populate fields' values.

You do not have to provide revert function unless you want to get the original data model.

New Functionality

map (and revert respectively) creates every dest destination field specified in the in conf. If src does not exist then the value of dest will be equal to undefined:

var DMM = require('data-model-mapper');

var obj = {
    DATE: '20160512',
    TIME: '140000',
    FIRST_NAME: 'John',
    LAST_NAME: 'Doe'
    //ID missing
};

var m = new DMM(conf);

var mapped = m.map(obj);
/* result is:
{
    id: undefined
    lastLogin: Thu May 12 2016 14:00:00 GMT+0200 (CEST), // JS Date object
    employeeName: 'John Doe'
}
*/

Even though this functionality may be useful in some cases, there are times when we want to map only the fields that exist in the data we provide:

var DMM = require('data-model-mapper');

var obj = {
    DATE: '20160512',
    TIME: '140000',
    FIRST_NAME: 'John',
    LAST_NAME: 'Doe'
    //ID missing
};

var m = new DMM(conf);

var mapped = m.map_existing_keys(obj);
/* result is:
{
    //id is not created as ID is not found in the src
    lastLogin: Thu May 12 2016 14:00:00 GMT+0200 (CEST), // JS Date object
    employeeName: 'John Doe'
}
*/
map_existing_keys Function
optional

Transform function which maps only existing src field(s) in provided data into dest field(s). Function gets parameter value(s) form field(s) specified in src. If dest is an array function should return array of values in order to properly populate fields' values.

revert_existing_keys Function
optional

Transform function which maps only existing dest field(s) in provided data into src field(s). Function gets parameter value(s) form field(s) specified in dest. If src is an array function should return array of values in order to properly populate fields' values.