Package Exports
- object-merge-advanced
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (object-merge-advanced) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
object-merge-advanced
Like .merge but an array in a key's value trumps a plain object and a plain object trumps a string. Works for nested objects as well.
Install
$ npm install --save object-merge-advanced
Purpose
It's like _.merge
but it correctly merges different-type things and behaves well when it encounters nested things (arrays or objects).
I was not happy with Lodash _.merge because it gets stuck when it encounters mismatching values within plain objects. All I wanted is to merge two plain objects, retaining as much information as possible after the merging.
When this library merges two objects, it will check the types of their keys:
- If keys are unique, they go straight into the result object.
- If keys are present in both input arguments, they'll get "judged" depending on their type:
- Any object trumps any string
- Non-empty array trumps any object or string
- Empty array trumps empty object, but doesn't trump non-empty object
- Empty array won't trump non-empty string
- If both key have plain object values, they'll get recursively fed back into the library again
We strive to retain as much info as possible after merging.
Basically, there are 81 possible combinations: 9 types of first input (object #1) and 9 types of second input (object #2): non-empty (full) object, empty object, non-empty array, empty array, non-empty string, empty string, boolean, undefined and null.
In the diagram above, the squares show which value wins, first object's (marked 01
) or second one's (marked 02
). In other words, do we assign second object's value onto first, or the opposite.
In certain cases (marked red), there are custom actions needed: 1) passing value objects back into the main function recursively (when both values are objects), 2) array concatenation, or 3) Boolean "and" composition (when both values are Boolean).
I challenge you to check test.js
unit tests to see this library in action.
In practice
In practice I needed this library to normalise JSON files — generate a "schema" object (a superset of all used keys) and fill any missing keys within all JSON files. Also, JSON files get their keys sorted. That library is used to keep us sane when using JSON to store content for email templates — it's enough to add one unique key in one JSON, and all other templates' content files get it added as well.
Use
var mergeAdvanced = require('object-merge-advanced')
API
mergeAdvanced(object1, object2)
API - Input
Input argument | Type | Obligatory? | Description |
---|---|---|---|
object1 |
Plain object | yes | Plain object. Can have nested values. |
object2 |
Plain object | yes | Another plain object. Can have nested values. |
Testing
$ npm test
Unit tests use AVA and JS Standard notation.
Contributing
All contributions are welcome. Please stick to Standard JavaScript notation and supplement the test.js
with new unit tests covering your feature(s).
If you see anything incorrect whatsoever, do raise an issue. If you file a pull request, I'll do my best to help you to get it merged in a timely manner. If you have any comments on the code, including ideas how to improve things, don't hesitate to contact me by email.
Licence
MIT License (MIT)
Copyright (c) 2016 Code and Send Ltd, Roy Reveltas
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.