From 3066bb9340d038de56c6ba483d5e854a8222cb31 Mon Sep 17 00:00:00 2001 From: frank Date: Thu, 30 May 2024 07:44:26 -0700 Subject: [PATCH] When merging config, union arrays instead of replacing them. --- Changelog.md | 3 +++ README.md | 14 +++++++++----- package-lock.json | 2 +- package.json | 2 +- src/merge-config.ts | 4 +++- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Changelog.md b/Changelog.md index 1600be2..8efd968 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 diff --git a/README.md b/README.md index 8aacd8d..64dffbf 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/package-lock.json b/package-lock.json index 5871d56..7f09b20 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "dyflex-config", - "version": "1.0.2", + "version": "1.0.3", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index db288af..6eb9457 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/merge-config.ts b/src/merge-config.ts index 426639c..1a332a9 100644 --- a/src/merge-config.ts +++ b/src/merge-config.ts @@ -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. @@ -33,6 +33,8 @@ export function mergeConfig(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());