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

js-object-mapper

v1.0.0

Published

Node module with utility functions to help you easily map one JS object to another.

Downloads

1

Readme

js-object-mapper

Node module with utility functions to help you easily map one JS object to another.

Motivation

When doing UI development using TypeScript or JavaScript it becomes necessary to map between various ViewModel objects and the various DataAcessObjects (DAOs) received from the various services used to fetch data for the UI. Writing code to do this mapping becomes tedious and repetivive. So this module aims to help by providing functions to easily map any JS object to another.


Examples

DeepSet

This method allows you to set deeply nested property or object values within an object. If the target object does not contain the targetted property to be set, then the targetted property hierarchy is created. This is very useful when trying to initialise a deeply nested object structure without having to explicily create each object in the targetted property hierarchy. This works for deeply nested arrays as well. If the targetted property hierarchy exists then, it used and the property is set without it overwriting perviously set values.

  1. Initialise an empty object with a deeply nested object property, in this case a nested multidimentional array.
   const r = {};
   mapper.deepSet(r, 'b[0][1].h.y[0][1][0].p', 2);
   // Prints 2 to the console
   console.log(b[0][1].h.y[0][1][0].p);
  1. Update a deeply nested property.
   let r = {'a': 1, 'b': 2, c: { x: 1, y:[1, 2]} };
   mapper.deepSet(r, 'c.y[1]', 22);
   // Prints 22 to the console
   console.log(r.c.y[1]);
   // Prints 1 to the console
   console.log(r.a);

DeepGet

This method allows you to fetch deeply nested properties from within an object without having to imperatively traverse the entire property hierarchy to get the value of the property. Additionally you do not need to do null checks along the way, if the property does not exist or one of the properties along the way is null, the method will return undefined. It also works for deeply nested multi-dimensional array values.

  1. Get nested array values
r = {a: 1, b:[[1,2,3], [4, 5]], c:{x:[9, {y:[10]}]}};
// Prints 10 to the console
console.log(mapper.deepGet(r, 'c.x[1].y[0]'));

Map

This method allows you to map a property on one object to a property on another object. It leverages the 'DeepSet' and 'DeepGet' methods above. If the property hierarchy on the target object does not exist, the entire property hierarchy on the target object is created and this saves you the tedious task of having to create the entire property hierarchy on the target object before setting the value of a deeply nested property. This also works for deeply nested arrays and multi-dimensionl arrays. If the source property does not exist then the target propery is set to 'undefined'.

  1. Set target object properties
        s = {a:1, b:2, c:{p:1, q:[1, 2]}};
        t = {o:{}};

        mapper.map(s, t, 'c.p', 'o.p');
        mapper.map(s, t, 'c.q', 'o.q');

        // Prints 1 to the console
        console.log(t.o.p);

        // Prints [1, 2] to the console
        console.log(t.o.q);