Skip to content

Commit a0dbd58

Browse files
committed
fixup!
1 parent 4ff95d8 commit a0dbd58

File tree

3 files changed

+52
-58
lines changed

3 files changed

+52
-58
lines changed

src/generators/api-links/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The `api-links` generator creates a mapping of publicly accessible functions to
66

77
The `api-links` generator accepts the following configuration options:
88

9-
| Name | Type | Default | Description |
10-
| -------- | --------- | ------- | --------------------------------------------------- |
11-
| `output` | `string` | - | The directory where `apilinks.json` will be written |
12-
| `minify` | `boolean` | `true` | Whether to minify the output JSON |
9+
| Name | Type | Default | Description |
10+
| -------- | --------- | ----------------------- | --------------------------------------------------- |
11+
| `output` | `string` | - | The directory where `apilinks.json` will be written |
12+
| `minify` | `boolean` | Inherited from `global` | Whether to minify the output JSON |

src/utils/configuration/index.mjs

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { allGenerators } from '../../generators/index.mjs';
88
import { parseChangelog, parseIndex } from '../../parsers/markdown.mjs';
99
import { enforceArray } from '../array.mjs';
1010
import { leftHandAssign } from '../generators.mjs';
11-
import { lazy } from '../misc.mjs';
11+
import { deepMerge, lazy } from '../misc.mjs';
1212
import { importFromURL } from '../url.mjs';
1313

1414
/**
@@ -39,53 +39,6 @@ export const getDefaultConfig = lazy(() =>
3939
)
4040
);
4141

42-
/**
43-
* Deeply merges multiple objects with special handling for arrays and nested objects.
44-
*
45-
* @param {Object[]} objs - Objects to merge, with earlier arguments taking precedence
46-
* @param {number} maxDepth - Maximum depth for recursive merging
47-
* @param {number} currentDepth - Current recursion depth
48-
* @returns {Object} A new object containing the merged result
49-
*/
50-
const merge = (objs, maxDepth, currentDepth = 0) => {
51-
return objs.reduce((result, current) => {
52-
return Object.keys({ ...result, ...current }).reduce((acc, k) => {
53-
const resultVal = result[k];
54-
const currentVal = current[k];
55-
56-
return {
57-
...acc,
58-
[k]:
59-
// If both values exist
60-
resultVal && currentVal
61-
? // Both are arrays: concatenate them
62-
Array.isArray(resultVal) && Array.isArray(currentVal)
63-
? [...resultVal, ...currentVal]
64-
: // result is array, current is string: keep result (array wins)
65-
Array.isArray(resultVal) && typeof currentVal === 'string'
66-
? resultVal.concat([currentVal])
67-
: // result is string, current is array: keep current (array wins)
68-
typeof resultVal === 'string' && Array.isArray(currentVal)
69-
? currentVal.concat([resultVal])
70-
: // Both are objects: recursively merge if depth allows
71-
typeof resultVal === 'object' &&
72-
typeof currentVal === 'object'
73-
? currentDepth < maxDepth
74-
? merge(
75-
[resultVal, currentVal],
76-
maxDepth,
77-
currentDepth + 1
78-
)
79-
: currentVal // At max depth, use current value (last wins)
80-
: // Otherwise: result takes precedence
81-
resultVal
82-
: // Only one exists: use nullish coalescing to pick the defined value
83-
(resultVal ?? currentVal),
84-
};
85-
}, {});
86-
}, {});
87-
};
88-
8942
/**
9043
* Loads a configuration file from a URL or file path.
9144
*
@@ -158,11 +111,10 @@ export const createRunConfiguration = async options => {
158111
config.target &&= enforceArray(config.target);
159112

160113
// Merge with defaults
161-
// We set maxDepth as 2, as that's the depth of
162-
// our object.
163-
const merged = merge(
164-
[config, createConfigFromCLIOptions(options), getDefaultConfig()],
165-
1
114+
const merged = deepMerge(
115+
config,
116+
createConfigFromCLIOptions(options),
117+
getDefaultConfig()
166118
);
167119

168120
// These need to be coerced

src/utils/misc.mjs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
11
/**
2-
* Lazy-loader
2+
* Creates a lazy-initialized function that caches the result of the first invocation.
3+
* @template {(...args: any[]) => any} T
4+
* @param {T} fn - The function to be lazily executed
5+
* @returns {T} A wrapper function that lazily executes fn and caches its result
36
*/
47
export const lazy = fn =>
58
(
69
c =>
710
(...args) =>
811
(c ??= fn(...args))
912
)();
13+
14+
/**
15+
* Checks if a value is a plain JavaScript object.
16+
* @param {*} value - The value to check
17+
* @returns {boolean} True if the value is a plain object, false otherwise
18+
*/
19+
export const isPlainObject = value =>
20+
value !== null && typeof value === 'object' && !Array.isArray(value);
21+
22+
/**
23+
* Recursively merges multiple objects deeply.
24+
* @template T
25+
* @param {...T} objects - Any number of objects to merge
26+
* @returns {T} A new object containing the deep merge of all provided objects
27+
*/
28+
export const deepMerge = (...objects) => {
29+
const base = objects.pop();
30+
31+
return objects.reduce((result, source) => {
32+
// Skip null/undefined values
33+
if (!source) {
34+
return result;
35+
}
36+
37+
for (const key in source) {
38+
if (Object.prototype.hasOwnProperty.call(source, key)) {
39+
const sourceValue = source[key];
40+
const targetValue = result[key];
41+
42+
result[key] =
43+
isPlainObject(targetValue) && isPlainObject(sourceValue)
44+
? deepMerge(targetValue, sourceValue, base[key])
45+
: sourceValue;
46+
}
47+
}
48+
49+
return result;
50+
}, base);
51+
};

0 commit comments

Comments
 (0)