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

bimedia-objectmapper

v0.2.3

Published

object mapper for node

Downloads

341

Readme

bimedia-objectmapper build status

object mapper for node

Object Mapper Module

This module allow to map keys and values from an object to an other. Mapping uses a tranformation rules. There are 3 transformation rules types :

  • identity : copy the attribute and value to the target object.
  • alias : rename the attribute in the target object (value is unchanged).
  • complex : apply tranformation rule provided to change attribute name and value.

Installation

npm install --save bimedia-objectmapper

Usage

#### Simple usecase

var ObjectMapper = require('bimedia-objectmapper');
var mapper = objectMapper({'key':'clé'});
var result = mapper({key:'value'});
// {'clé': 'value'}

#### Streams

var objectMapper = require('bimedia-objectmapper'), 
  fs = require('fs'), 
  JSONStream = require('JSONStream');
  
var mapper = objectMapper(rules);
fs.createReadStream('package.json')
  .pipe(JSONStream.parse())
  .pipe(mapper.stream())
  .pipe(process.stdout);

#### Arrays

var objectMapper = require('bimedia-objectmapper');
var mapper = objectMapper({'key':'clé'});
var sources = [{key:'value1'}, {key:'value2'}, {key:'value3'}];
var result = sources.map(mapper);
//[ { 'clé': 'value1' },
//  { 'clé': 'value2' },
//  { 'clé': 'value3' } ]

Mapping rules definitions

Identity Rule

No specific rule is needed. Attribute is copied to target object.

Simple Rule (alias)

Declare a constant as attribute value. Example :

var rules = {
    "trxpvptpv": "trxid"
};

This rule renames trxpvptpv attribute in source object, to trxid in target object.

Complex Rule

Define an object matching the source attribute. Example :

var rules = {
     'state' : {
        name : 'currentState',
        mapper : function (val) {
            if (val == 'Y') {
                return 'REFUNDED';
            }
            return val == 'N' ? 'REFUSED' : 'PENDING';
        }
    }
};

Or directly with a function :

var rules = {
     'state' : function (k, v) {
            var value;
            if (val == 'Y') {
                value =  'REFUNDED';
            }
            value = val == 'N' ? 'REFUSED' : 'PENDING';
            return {key: 'currentState', value: value};
        }
    }
};

This 2 examples transforms the state attribute in source object to an currentState attribute in target object.

Options

Mapping can be customize with the second argmuent of ObjectMapper : For instance to disable identity mapping by default :

var mapper = ObjectMapper(rules, {defaults: false});

Or to specify a default mapping function :

var mapper = ObjectMapper(rules, {defaults: ObjectMapper.keys.camelCase});

supported options

  • defaults : (function | boolean) define a default mapping function. By defaut ObjectMapper.identity.