mistercrunch commented on code in PR #34876: URL: https://github.com/apache/superset/pull/34876#discussion_r2322557173
########## superset-frontend/packages/superset-ui-core/src/utils/lodash.ts: ########## @@ -0,0 +1,52 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { mergeWith } from 'lodash'; + +/** + * Merges objects using lodash.mergeWith, but replaces arrays instead of concatenating them. + * This is useful for configuration objects where you want to completely override array values + * rather than merge them by index. + * + * @example + * const obj1 = { a: [1, 2], b: { c: 3 } }; + * const obj2 = { a: [4, 5], b: { d: 6 } }; + * mergeReplaceArrays(obj1, obj2); + * // Result: { a: [4, 5], b: { c: 3, d: 6 } } + * + * @example + * // ECharts configuration merging + * const baseConfig = { series: [{ type: 'line' }], grid: { left: '10%' } }; + * const overrides = { series: [{ type: 'bar' }], grid: { right: '10%' } }; + * mergeReplaceArrays(baseConfig, overrides); + * // Result: { series: [{ type: 'bar' }], grid: { left: '10%', right: '10%' } } + * + * @param sources - Objects to merge (rightmost wins for arrays, deep merge for objects) + * @returns Merged object with arrays replaced, not concatenated + */ +export function mergeReplaceArrays<T = any>(...sources: any[]): T { + const replaceArrays = (objValue: any, srcValue: any) => { + if (Array.isArray(srcValue)) { + return srcValue; // Replace arrays entirely + } + return undefined; // Let lodash handle object merging for non-arrays + }; + + return mergeWith({}, ...sources, replaceArrays); +} Review Comment: btw for `echartsOptions` merging, maybe merging/stacking arrays is fine (?) Felt wrong to me. Maybe it's right in some cases, wrong in others. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org