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

mapperjs

v3.0.1

Published

The solution for node.js for migrate data from the old format to the new format data with pre-processing

Downloads

22

Readme

mapper

Travis CI Join the chat at https://gitter.im/chetverikov/mapperjs

NodeJS Data Mapper for transfer data from old format to new.

Install


 npm install mapperjs;

Usage


const Mapper = require('mapperjs');

mapper = new Mapper( map, options );
mapper.transfer( source, destination )
 .then(dst_res_obj => {
   // call after
 }
 .catch(err => {
   // call after if reject with err
 });

Map

The map must be a binary array consisting of the string/array/functions. The first array element should be a string or an array of strings path to data of the original object.


// Source data
source = {
  title: '',
  descriptions: {
    short: '',
    long: ''
  },
  photos: [
    '1.jpg',
    '2.jpg',
    '3.jpg'
  ],
  old_photos: [
    'a.png',
    'b.png'
  ]
}

destination = {};

map = {
  title: 'title',
  /*
   * in destination object will be field description with data from descriptions.long
   */
  'descriptions.long': 'description',
  /*
   * in preview will be first photo from photos
   */
  'photos.0': 'preview'
}

Sync and Async Map functions

On the map are the functions for processing the data sync and async.


map = {

  /**
   * Async map func
   * value - content entity id
   */
  entityId: function( value ){
    // this.dst - destination
    // this.src - source

    retun db.queryById( value )
     .then( entity ){

      /**
       * first arg - error
       * second arg - object:
       *   key - path to destination object
       *   value - value

       * The second argument may contain multiple key/value to setup more fields and values.
       */
      return { entity: entity };
     })
     .catch(err => defer.reject(err));
  },

  /**
   * Sync map func
   */
  comments: comments => {
    return { comments_count: getCountOnlyActiveComments(comments) };
  }
}

Debug

Mapperjs uses the debug module internally to log information about route matches and application mode. To see this information, simply set the DEBUG environment variable to substance:* when launching your app and the debug information will appear on the console.


DEBUG=mapper* node app.js

Options

skipError

For skip error from async callback. Default: false

if set to false, then the transfer process will stop after the first error


const Mapper = require('mapper');

mapper = new Mapper( map, { skipError: true } );

// not passed errors in an asynchronous callback, and do not stop the transfer process
mapper.transfer( source, destination ).then(dst_res_obj => {
  // call after
});

skipFields

For skip not required fields, you can use the option skipFields:


const Mapper = require('mapper');

mapper = new Mapper( map, { skipFields: 'field1 field2 iAnotherField' } );

// without fields field1, field2, iAnotherField
mapper.transfer( source, destination ).then(dst_res_obj => {
  // call after
});

Example


  const oldObj = {
        username: 'Maksim Chetverikov',
        avatar: '2fge0923df08r.jpg',
        country: 'Russia'
        city: 'Irkutsk'
      }
    , newObj = {
        firstname: '',
        lastname: '',
        avatar: '',
        address: ''
     };

  const map = {
    username: username => {
      const parts = username.split(' ');

      return { firstname: parts[0], lastname: parts[1] };
    },
    avatar: 'avatar',
    'country city', values => {
        return {address: value.country + ', ' + value.city}
    }
  };

  const mapper = new Mapper( map );

  mapper.transfer( oldObj, newObj ).then(obj => console.log( obj ));

results


{
  firstname: 'Maksim',
  lastname: 'Chetverikov',
  avatar: '2fge0923df08r.jpg',
  address: 'Russia, Irkutsk'
}

Tests

npm test