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

deep-object-assign-with-reduce

v3.0.2

Published

Minimalist deep merging of Objects with the same function signature as Object.assign()

Downloads

366

Readme

deep-object-assign-with-reduce

Build Status Downloads

Introduction

Deep Object.assign() written with modern, functional JavaScript. Inspired by deep-assign and the need for a deeper Object.assign.

No dependencies and very tiny - only ~450 bytes gzipped.

Installation

Requires Node.js 10+, which comes with npm.

In your project directory, type:

npm install deep-object-assign-with-reduce

or

yarn add deep-object-assign-with-reduce

Changelog

  • 3.0.0 - dropped Node 8 and 9 support. Please use 2.x if you wish to use an older version of Node.
  • 2.0.0 - dropped IE 11 support in order to dramatically reduce filesize.
  • 1.2.0 - added deepAssignOptions to give more control over array and object merging
  • 1.1.0 - fixed RegExp as values, Symbols as keys. Moved to Rollup, Jest. Updated Babel.

Examples

Merge complex objects

import { deepAssign } from 'deep-object-assign-with-reduce';

deepAssign({}, { dimensions: { width: 100, height: 100 } }, { dimensions: { width: 200 } });
// -> { dimensions: { width: 200, height: 100 } }

Merge arrays

import { deepAssign } from 'deep-object-assign-with-reduce';

deepAssign({}, { numbers: [1, 2, 3] }, { numbers: [4, 5, 6] });
// -> { numbers: [1, 2, 3, 4, 5, 6] }

Custom overwriting behavior

deepAssign merges objects and arrays by default. If you want to disable this behavior and instead overwrite objects and/or arrays, you can use deepAssignOptions instead, which accepts an options object as the first parameter:

import { deepAssignOptions } from 'deep-object-assign-with-reduce';

deepAssignOptions({overwriteArrays: true}, {}, { numbers: [1, 2] }, { numbers: [3, 4] });
// -> { numbers: [3, 4] }

deepAssignOptions({overwriteObjects: true}, {}, { a: { b: 1 } }, { a: { c: 2 } });
// -> { a: { c: 2 } }

Why not just use Object.assign() or Object spread?

Where Object.assign() works fine

Here's the motivation: the native Object.assign() works great on its own as long as you use simple objects that are one level deep.

There's a common use case where we have some "defaults" configuration object, in which properties can be selectively overridden while retaining the remaining default properties.

Say we have some component which has default dimensions, which we want to be able to selectively override:

// Default properties to "fallback" to.
const componentDefaults = { width: 100, height: 100 };

// Selectively overrides one property.
const componentOptions = { width: 200 };

// Constructs the final options object by merging the objects.
const options = Object.assign({}, componentDefaults, componentOptions);
// -> { width: 200, height: 100 }

This works because Object.assign will merge the simple objects together - and if the same property (such as width) appears in a later object, it will override the previous value. In this case the width in componentOptions overrides the previously set default in componentDefaults.

Where Object.assign() DOESN'T work fine

If your configuration object is more complicated and contains nested objects, Object.assign() turns out not to work so well:

const componentDefaults = { dimensions: { width: 100, height: 100 } };
const componentOptions = { dimensions: { width: 200 } };

const options = Object.assign({}, componentDefaults, componentOptions);
// -> { dimensions: { width: 200 } }

What happened to the height property?! It turns out that the previously set dimensions property was completely overwritten, not merged with the new dimensions object.

Maybe we can use Object spread? But that turns out to have the same limitation with complex objects:

const objSpreadTest = { ...componentDefaults, ...componentOptions };
// -> { dimensions: { width: 200 } }

Testing

To run the test suite, simply type:

npm test

Note that I've used some of the same tests as deep-assign in addition to my own. More contributions are welcome!

License

MIT