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

hodash.reshape

v3.0.1

Published

Allows a mapping to be defined for reshaping an object.

Downloads

253

Readme

GitHub license npm version PRs Welcome

Map from one object shape to another.

The objective of this utility is to map objects from one shape to another. The most common use case for this would be if you are getting data from an external service and need to map it to your own schema.

$ npm install --save hodash.reshape

Include the package.
import reshape from 'hodash.reshape';

Now, let's say our goal is to transform data from inputs that look like this:

type Input = {
  Age: number;
  FirstName: string;
  LastName: string;
  Address: {
    Street: string;
    Lat: number;
    Long: number;
  };
};

into outputs that look like this:

type Output = {
  age: number;
  name: string;
  address: {
    street: string;
    coords: {
      lat: number;
      lng: number;
    };
  };
};

Let's see what that mapping would look like:

Define Reshaper

// We define the mapping here.
const shapeUser = reshape({
  age: 'Age',
  name: obj => obj.FirstName + ' ' + obj.LastName,
  address: obj => reshape({
    street: 'Address.Street',
    coords: reshape({
      lat: 'Address.Lat',
      lng: 'Address.Long',
    }, obj),
  }, obj),
});

The properties of the mapping object are in the shape that our output will take on. By having a property on the mapping object, such as age or name, you are defining what you would like a property keyed as in your output. The value of the mapping function can be one of three types: function, string, or other.

  • function: If the value on the mapping is a function, such as with name, the whole object we are mapping from will be passed in. The function is executed in a try catch block, so you can be very aggressive with reaching for properties. obj => Math.floor(obj.age.median) is fine to do even if you don't know if obj.age or obj.age.median are defined, let alone whether or not Math.floor will throw an error. If it throws an error, it will be caught, and the value will be undefined.
  • string: A dot-delimited path to the value you're looking for. 'address.street' will safely attempt to return obj.address.street, and return undefined if there is anything undefined along the way.
  • other: Just places the value encountered in the output at that property. (See mapping.address.coords for reference)
Let's remap!
const result = shapeUser({
  Age: 25,
  FirstName: 'Tom',
  LastName: 'Bingus',
  Address: {
    Street: '1234 Dangus St',
    Lat: 123,
    Long: 10
  }
});
The output.
{
  "age": 25,
  "name": "Tom Bingus",
  "address": {
    "street": "1234 Dangus St",
    "coords": {
      "lat": 123,
      "lng": 10
    }
  }
}