Skip to content

Commit

Permalink
When merging config, union arrays instead of replacing them.
Browse files Browse the repository at this point in the history
  • Loading branch information
frank committed May 30, 2024
1 parent b13ccb5 commit 3066bb9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.0.3 / 2024-05-30
* When merging config, union arrays (instead of right replacing left).

## 1.0.2 / 2024-05-27 (No functional changes)
* Improve index.ts

Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,15 @@ See `discoverInitializers` and `invokeInitializers` for more info.

## Implementation Notes
lodash.merge is used for configuration merging (along with lodash.set if you are merging at a key prefixed merge point).
Understanding how lodash.merge actually works is important, so please read [its documentation](https://lodash.com/docs/#merge). Once you understand it,
you will see a problem...
* How do I replace an object or an array of values?

This library actually uses [lodash.mergeWith](https://lodash.com/docs/#mergeWith) in order to implement a "not" ability (aka `!`, aka _bang_, aka _replacement_).
Understanding how lodash.merge actually works is important, so please read [its documentation](https://lodash.com/docs/#merge).
Once you understand it,
you will see a problem that this library uses [lodash.mergeWith](https://lodash.com/docs/#mergeWith) to address...
* How do I merge two arrays (instead of having the left array replaced by the right).
* How do I replace an object (as opposed to merging it)?

To address the first, we use lodash.union to 'merge' arrays which is (IMO) the intuitive behavior.
If you really did want to replace the left arry with the right, you can us the 'not' feature described next.
To address the second, we implement a "not" ability (aka `!`, aka _bang_, aka _replacement_).
This feature allows you to **overwrite** a node in the hierarchy instead of merging.
```json5
// Merging in this json5 override will "merge" the values into configuration,
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dyflex-config",
"version": "1.0.2",
"version": "1.0.3",
"description": "Simple, dynamic, flexible, configuration library.",
"author": "Frank Stock",
"license": "MIT",
Expand Down
4 changes: 3 additions & 1 deletion src/merge-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {get as lodashGet, mergeWith as lodashMergeWith, set as lodashSet} from 'lodash';
import {get as lodashGet, mergeWith as lodashMergeWith, set as lodashSet, union as lodashUniton} from 'lodash';

/**
* A unique marker used as the default value for lodashGet.
Expand Down Expand Up @@ -33,6 +33,8 @@ export function mergeConfig<T extends object>(dst: T, src: object, mergePoint?:
});
object[key.substring(1)] = srcValue;
}
else if (Array.isArray(objValue))
return lodashUniton(objValue, srcValue);
return undefined;
});
deletes.forEach(d => d());
Expand Down

0 comments on commit 3066bb9

Please sign in to comment.