0.4.0 • Published 6 years ago

object-assign-deep v0.4.0

Weekly downloads
30,189
License
MIT
Repository
github
Last release
6 years ago

Object-Assign-Deep

Like Object.assign() but deeper. This module is the holy grail of simple object manipulation in JavaScript and it does not resort to using the JSON functions. If you need more power or fine-grained control please take a look at the Object-Extender module.

Breaking Changes in v0.3!

  • objectAssignDeep() now mutates the first argument in the same way Object.assign() does.
  • By default, arrays are now replaced instead of merged to preserve backwards compatibility with older versions of this module.

Caution! Danger of Death!

This module is to be used with PLAIN objects that contain primitive values ONLY. Every time you misuse this module a kitten dies.. yes you're a kitten killer.

Do not use this module if:

  • Your objects are (or contain) native objects such as Date (nested Array is fine).
  • Your objects contain circular references (you'll cause a stack overflow).
  • Your objects are instances of some class you've written.
  • You are concerned with prototype chains, property descriptors, unenumerable properties, and any other advanced uses.

If you need to do something fancy like the above you'll need to write a custom solution for your use case.

Quick Start

You can merge plain objects or clone them:

const objectAssignDeep = require(`object-assign-deep`);

const mergedObjects = objectAssignDeep(target, object1, object2, ...objectN);

const clonedObject = objectAssignDeep({}, originalObject);

Simples!

Full Example

See the ./examples directory for a few examples, including one example case that demonstrates why you can't get clever with object cloning.

const objectAssignDeep = require(`object-assign-deep`);

const objectA = {
	prop1: `Hello`,
	prop2: `World`,
	nested: {
		bool: true,
		super: 123,
		still: `here!`,
	},
	array1: [1, 2, 3],
	array2: [4, 5, 6],
};

const objectB = {
	prop2: `Universe`,
	name: `Josh`,
	nested: {
		bool: false,
	},
	array1: null,
};

const objectC = {
	location: `United Kingdom`,
	name: `Bob`,
	nested: {
		super: 999,
	},
	array2: [100, 101, 102],
};

const result = objectAssignDeep(objectA, objectB, objectC);

console.log(`Result:`, result);

/*
*   {
*     prop1: 'Hello',
*     prop2: 'Universe',
*     nested: { bool: false, super: 999, still: 'here!' },
*     array1: null,
*     array2: [100, 101, 102],
*     name: 'Bob',
*     location: 'United Kingdom'
*   }
*/

API Overview

objectAssignDeep(target, object1, object2, ...objectN);

Merges all the objects together mutating the target in the process and returning the result.

objectAssignDeep.noMutate(object1, object2, ...objectN);

Merges all the objects together without mutating any of them and returning the entirely new object.

objectAssignDeep.withOptions(target, objects, options);

Takes a target, an array of objects to merge in, and an options object which can be used to change the behaviour of the function. The available options are:

OptionDefault ValueDescription
arrayBehaviour"replace"By default arrays in later objects will overwrite earlier values, but you can set this to "merge" if you want to concatenate the arrays instead.

If you need more customisation options please take a look at the Object-Extender module which builds upon Object-Assign-Deep.

md4blogocx-sdk-jsgenerator-hanacloudui5@infinitebrahmanuniverse/nolb-object-react-rainbow-styleleasehold-app@everything-registry/sub-chunk-2364fiat-ratesfmv-chartshyper-web-serveriglass-nodesiubenda-moduleeksi-sozlukgren-release-notes@ekoopensource/sdkgraphcool-compose@ewizardjs/vue-editorgoogle-civic-proxy@flipbyte/redux-datatablegenerator-ui5-exceluploadgenerator-ui5-spreadsheetimportergenerator-ui5_to_mtagenerator-template-ui5-projectgithub-release-notesgestalt-core@femessage/github-release-notesgulp-shadow-filesgulp-smart-builderjsxquery-buildjsx-botjrevolverjixijwtfedkea-resources@kaltura-ng/dev-workspace@joinfirefish/megalodonkopiro-tommynuxt-iubendapacking-template-velocitypacking-template-artTemplatepacking-template-ejspacking-template-handlebarspacking-template-htmlpacking-template-pugpacking-template-smartypacking-template-utilola-toolkit@silvapav/github-release-notes@spokedev/eslint-config-jigsaw@rfxcom2mqtt/rfxcom2mqtt@risen-pcip/plugin-pcip-watermarknu-ojtc@octopusx/ndknavenpcip-monitoroberd-oui-webpack-configobject-extenderoblakleasehold-coreldem@kontorol-ng/dev-workspace@luego/gulp-shadow-files@luego/luego-get-datallm-exeluego-get-datamegalodon@oada/cli@oada/client@oada/list-libmultivocal@op/api-sdkmongoose-schema-upgradermongo-mockmongoose-schema-builder@pawel-gawel/github-release-notes@pawk/github-release-notes@plyo/github-release-notesqordoba-file-helper@tuanpham-dev/gulp-liquidjs@tangelo/tangelo-configuration-toolkitchat-renderercli.config.js@appdirect/sfb-toolkitchat-templatecataitaro-builderclioadaconnect-nodesconfig-updater@4qwerty7/syzoj-renderercapitalisk-appsyzoj-rendererstrip-media-queriessuitcss-preprocessorsupple-preprocessorswagger2markdowncoreux-components@chris-goodchild-code/mocha-multi-reportersd3-stencild3-stenciljs
0.4.0

6 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago