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

mapper.js

v0.4.1

Published

Transform object from one structure to another by using schema. It's useful in API when you want to modify data before sending it to clients

Downloads

32

Readme

mapper.js

Transform object from one structure to another by using schema. It's useful in API when you want to modify data before sending it to clients

NPM version Build status

Note: This module works in browsers and Node.js >= 6.0.

Demo

Try demo on RunKit.

Installation

npm install mapper.js

Node.js

const mapper = require('mapper.js');

Browser

<script src="node_modules/mapper.js/dist/mapper.js">

or minified version

<script src="node_modules/mapper.js/dist/mapper.min.js">

You can use the module with AMD/CommonJS or just use window.mapper.

Usage

There are two ways to use module:

1. mapper(data, schema)

Parameters

  • data (Object | Object[]) - Object or array of objects to map
  • schema (Function | Object) - Schema which defines how to map object

Return value

(Object | Object[]) - Result of mapping (object or array of objects)

const mapper = require('mapper.js');

const result = mapper(object, schema);

2. mapper(schema)

Parameters

  • schema (Function | Object) - Schema which defines how to map object

Return value

(Function): Mapping function which can be called with data further

const mapper = require('mapper.js');
const map    = mapper(schema);

const result = map(object);

Dynamic mapping (schema as a function)

const mapper = require('mapper.js');

const products = [
    {
        type: 'book',
        name: 'The Adventures of Tom Sawyer',
        description: 'Awesome book',
        count: 1
    },
    {
        type: 'sugar',
        description: 'Very tasty',
        weight: 3000
    }
];

const productsSchemas = {
    book: {
        name: true,
        count: true
    },

    sugar: {
        weight: true
    }
};

// function accepts an object and has to return a schema
const schema = product => Object.assign({}, productsSchemas[product.type], { type: true });

const result = mapper(products, schema);

Example

Imagine we have collection of users and each user object has the following structure:

const user = {
    id: 12345,

    firstName: 'Peter',
    lastName: 'Parker',

    company: {
        name: 'Daily Bugle',
        position: 'Photographer'
    },

    email: '[email protected]',

    socialNetworks: {
        vk: 'https://vk.com/peter.parker',
        facebook: 'https://www.facebook.com/peter.parker'
    },

    friends: [
        {
            id: 12346,
            firstName: 'Mary Jane',
            lastName: 'Watson'
        },
        {
            id: 12347,
            firstName: 'Harold',
            lastName: 'Osborn'
        }
    ]
};

We want to send users data to clients in a different format:

{
    id: 12345,
    fullName: 'Peter Parker',
    companyName: 'Daily Bugle',

    contacts: {
        email: '[email protected]',
        vk: 'https://vk.com/peter.parker',
        facebook: 'https://www.facebook.com/peter.parker'
    },

    friends: [
        { id: 12346, fullName: 'Mary Jane Watson' },
        { id: 12347, fullName: 'Harold Osborn' }
    ]
}

We can do it by using the schema:

const mapper = require('mapper.js');

const userSchema = {
    // take field without any changes
    id: true, // or '=',

    // you can specify function and return custom value
    fullName: (fullName, user) => user.firstName + ' ' + user.lastName,

    // take "name" field from "company" object
    companyName: 'company.name', // or '=company.name'

    // you can work with embedded objects
    contacts: {
        // by default property path is relative to current object,
        // so if you want to get value from top-level object ("user" in example) use "$"
        email: '$.email', // or '=$.email'

        // function takes 4 arguments:
        //   - current value of property = user.contacts.facebook
        //   - current object            = user.contacts
        //   - original object           = user
        //   - path as array             = ['contacts', 'facebook']
        facebook: (facebook, contacts, user, path) => user.socialNetworks.facebook,

        vk: '$.socialNetworks.vk'
    },

    // for arrays use array with schema for item
    friends: [{
        id: true,
        fullName: (fullName, friend) => friend.firstName + ' ' + friend.lastName
    }]
};

const result = mapper(user, userSchema); // or mapper(userSchema)(user)

Build

npm install
npm run build

Tests

npm install
npm test

License

MIT