Skip to content

Commit

Permalink
feat: enhance the merge function to support both arrays and plain o…
Browse files Browse the repository at this point in the history
…bjects
  • Loading branch information
cheton committed Nov 18, 2024
1 parent 65e7a61 commit 2c81177
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions packages/utils/src/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ const _joinWords = (words) => {
};

const _deepClone = (source) => {
if (!isPlainObject(source)) {
return source;
if (Array.isArray(source)) {
return source.map((item) => _deepClone(item));

Check warning on line 20 in packages/utils/src/shared.js

View check run for this annotation

Codecov / codecov/patch

packages/utils/src/shared.js#L20

Added line #L20 was not covered by tests
}

const output = {};

Object.keys(source).forEach((key) => {
output[key] = _deepClone(source[key]);
});
if (isPlainObject(source)) {
const output = {};
Object.keys(source).forEach((key) => {
output[key] = _deepClone(source[key]);
});
return output;
}

return output;
// For primitive values and other types, return as is
return source;
};

export const ariaAttr = (condition) => {
Expand Down Expand Up @@ -55,25 +58,33 @@ export const dataAttr = (condition) => {
};

export const merge = (target, source, options = { clone: true }) => {
const output = options.clone ? { ...target } : target;
// Merge arrays
if (Array.isArray(target) && Array.isArray(source)) {
const output = options.clone ? [...target] : target;
source.forEach((item, index) => {
if (isPlainObject(item) && isPlainObject(output[index])) {
output[index] = merge(output[index], item, options);

Check warning on line 66 in packages/utils/src/shared.js

View check run for this annotation

Codecov / codecov/patch

packages/utils/src/shared.js#L63-L66

Added lines #L63 - L66 were not covered by tests
} else {
output[index] = options.clone ? _deepClone(item) : item;

Check warning on line 68 in packages/utils/src/shared.js

View check run for this annotation

Codecov / codecov/patch

packages/utils/src/shared.js#L68

Added line #L68 was not covered by tests
}
});
return output;

Check warning on line 71 in packages/utils/src/shared.js

View check run for this annotation

Codecov / codecov/patch

packages/utils/src/shared.js#L71

Added line #L71 was not covered by tests
}

// Merge plain objects
if (isPlainObject(target) && isPlainObject(source)) {
const output = options.clone ? { ...target } : target;
Object.keys(source).forEach((key) => {
if (
isPlainObject(source[key]) &&
Object.prototype.hasOwnProperty.call(target, key) &&
isPlainObject(target[key])
) {
output[key] = merge(target[key], source[key], options);
} else if (options.clone) {
output[key] = isPlainObject(source[key]) ? _deepClone(source[key]) : source[key];
if (isPlainObject(source[key]) && isPlainObject(output[key]) && Object.prototype.hasOwnProperty.call(output, key)) {
output[key] = merge(output[key], source[key], options);
} else {
output[key] = source[key];
output[key] = options.clone ? _deepClone(source[key]) : source[key];
}
});
return output;
}

return output;
return options.clone ? _deepClone(source) : source;

Check warning on line 87 in packages/utils/src/shared.js

View check run for this annotation

Codecov / codecov/patch

packages/utils/src/shared.js#L87

Added line #L87 was not covered by tests
};

export const noop = () => {};
Expand Down

0 comments on commit 2c81177

Please sign in to comment.