Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/utils/producer.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, pls add a test for this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes i will add the test in some time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey I have added the test...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for too late.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { clean,type ICleanerOptions } from 'fast-clean';
import { clean, type ICleanerOptions } from 'fast-clean';
import { produce } from 'immer';
import { pipe } from 'rambdax';

Expand Down Expand Up @@ -50,6 +50,45 @@ export const produceRmDoubleUnderscoreKeys = produce((draft) => {
rmDoubleUnderscoreKeys(draft);
});

// Preserve plugin entries with empty configs before cleaning
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const producePreservePlugins = produce((draft: any) => {
if (draft.plugins && typeof draft.plugins === 'object') {
// Mark plugin configs to preserve them from being cleaned
Object.keys(draft.plugins).forEach((pluginName) => {
const config = draft.plugins[pluginName];
if (
config &&
typeof config === 'object' &&
!Array.isArray(config) &&
Object.keys(config).length === 0
) {
// Add a marker that will be removed later
draft.plugins[pluginName] = { __preserve: true };
}
});
}
});

// Remove the preserve markers after cleaning
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const produceRemovePreserveMarkers = produce((draft: any) => {
if (draft.plugins && typeof draft.plugins === 'object') {
Object.keys(draft.plugins).forEach((pluginName) => {
const config = draft.plugins[pluginName];
if (
config &&
typeof config === 'object' &&
config.__preserve === true &&
Object.keys(config).length === 1
) {
// Restore to empty object
draft.plugins[pluginName] = {};
}
});
}
});

/**
* FIXME: type error
*/
Expand All @@ -64,7 +103,9 @@ export const pipeProduce = (...funcs: ((a: any) => unknown)[]) => {
...fs,
produceRmDoubleUnderscoreKeys,
produceTime,
produceDeepCleanEmptyKeys()
producePreservePlugins, // Mark empty plugin configs before cleaning
produceDeepCleanEmptyKeys(),
produceRemovePreserveMarkers // Restore empty plugin configs after cleaning
)(draft) as never;
}) as T;
};
Loading