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

morphit

v1.0.1

Published

Utility to morph easily an object to another object

Downloads

8

Readme

Build Status

Morphit

Simple utility to morph an object to another one.

const morphit = require('morphit');

Basic Usage

Basic morphit function transforms an object to another object, mapping values with dot notation.

const morphit = require('morphit');

// object to morph
const obj = {
    id: 1,
    first_name: 'morph',
    last_name: 'it',
    street: 'Champs Élysées',
    post_code: '75000',
    country: 'FR',
    id_offer: 2,
    orders: [
        {
            id: 1,
            id_products: 1,
            id_invoice: 1,
            id_payment: 1
        }
    ],
    date: {
        created: '2017-01-01',
        updated: '2017-01-02',
    }
};

// morphing
const morph = morphit(obj, {
    user: {
        id: ':id',
        firstName: ':first_name',
        lastName: ':last_name',
        idOffer: ':id_offer',
        creationDate: ':date.created',
        updateDate: ':date.updated',
    },
    address: {
        street: ':street',
        postCode: ':post_code',
        country: ':country',
    },
    orders: ':orders',
    _morphed: true,
});

console.log(morph);
/*
{
    user: {
        id: 1,
        firstName: 'morph',
        lastName: 'it',
        idOffer: 2,
        creationDate: '2017-01-01',
        updateDate: '2017-01-02',
    },
    address: {
        street: 'Champs Élysées',
        postCode: '75000',
        country: 'FR'
    },
    orders: [
        {
            id: 1,
            id_products: 1,
            id_invoice: 1,
            id_payment: 1
        }
    ],
    _morphed: true
}
*/

Array

You can morph arrays of objects

const morphit = require('morphit');

const arr = [
    {
        first_name: 'John',
        last_name: 'Doe',
    }
];

const morph = morphit(arr, {
    firstName: ':first_name',
    lastName: ':last_name',
});

console.log(morph);
/*
[ { firstName: 'John', lastName: 'Doe' } ]
*/

Nested array morphing

You can morph an array nested in an morphing using morphit.each.

const morphit = require('morphit');

const obj = {
    users_info: [
        {
            first_name: 'John',
            last_name: 'Doe',
        },
        {
            first_name: 'Foo',
            last_name: 'Bar',
        }
    ]
};

const morph = morphit(obj, {
    users: morphit.each(':users_info', {
        firstName: '$.first_name',
        lastName: '$.last_name',
    });
});

console.log(morph);
/*
{ users:
   [ { firstName: 'John', lastName: 'Doe' },
     { firstName: 'Foo', lastName: 'Bar' } ] }
*/

Morph transform data

Morphit provides a way to transform the value before mapping to the morphed object; You can run async as long as you return a Promise. When adding transformations, morphit will return a Promise, you must use .then. If you add a _concurrency to the morph object, promises from transformations will be run in a pool of size 4, if no concurrency is provided, all transformations will be run synchronously.

const obj = {
    orders: [
        {
            id: 1,
            id_product: 1,
            id_invoice: 1,
            id_payment: 1
        }
    ],
    id_user: 1
};

const getSomeProduct = (value) => {
    return Promise.resolve({});
};

const getSomeInvoice = (value) => {
    return Promise.resolve({});
};

const getSomePayment = (value) => {
    return Promise.resolve({});
};

const getSomeUser = (value) => {
    return Promise.resolve({ value: value });
};

const morph = morphit(obj, {
    orders: morphit.each(':orders', {
        product: morphit.transform('$.id_product', getSomeProduct), // it returns a Promise,
        invoice: morphit.transform('$.id_invoice', getSomeInvoice), // it returns a Promise,
        payment: morphit.transform('$.id_payment', getSomePayment), // it returns a Promise,
        user: morphit.transform(':id_user', getSomeUser),
    }),
    user: morphit.transform(':id_user', getSomeUser),
    _concurrency: 4,
})
.then((morph) => {
    console.log(morph);
    /*
        { orders: [ { product: {}, invoice: {}, payment: {} } ], user: { value: 1 } }
    */
});