create structuredCleanClone helper function

This commit is contained in:
Reda Al Sulais 2023-08-10 23:10:06 +03:00
parent c4113541e1
commit be106befff
2 changed files with 29 additions and 8 deletions

View File

@ -0,0 +1,24 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export function removeUndefined(data: any): any {
if (typeof data === 'object') {
const entries: [string, any][] = Object.entries(data).filter(
([, value]: [string, any]) => value !== undefined
);
const clean: any[][] = entries.map(([key, v]: [string, any]) => {
const value = typeof v == 'object' ? removeUndefined(v) : v;
return [key, value];
});
return Object.fromEntries(clean);
} else if (Array.isArray(data)) {
return data.filter((value: any) => value !== undefined);
}
return data;
}
export function structuredCleanClone<T = any>(defaultData: T, data?: Partial<T>): T {
const cleanValue: T = removeUndefined(data);
return structuredClone<T>({ ...defaultData, ...cleanValue });
}

View File

@ -15,6 +15,7 @@ import type { ParseDirectiveDefinition } from '../../diagram-api/types.js';
import type { PieFields, PieDB, Sections } from './pieTypes.js';
import type { RequiredDeep } from 'type-fest';
import type { PieDiagramConfig } from '../../config.type.js';
import { structuredCleanClone } from '../../cleanClone.js';
export const DEFAULT_PIE_CONFIG: Required<PieDiagramConfig> = {
useMaxWidth: true,
@ -30,20 +31,16 @@ export const DEFAULT_PIE_DB: RequiredDeep<PieFields> = {
let sections: Sections = DEFAULT_PIE_DB.sections;
let showData: boolean = DEFAULT_PIE_DB.showData;
const config: Required<PieDiagramConfig> = structuredClone(DEFAULT_PIE_CONFIG);
let config: Required<PieDiagramConfig> = structuredCleanClone(DEFAULT_PIE_CONFIG);
const setConfig = (conf: PieDiagramConfig): void => {
config.useWidth = conf.useWidth ?? DEFAULT_PIE_CONFIG.useWidth;
config.useMaxWidth = conf.useMaxWidth ?? DEFAULT_PIE_CONFIG.useMaxWidth;
config.textPosition = conf.textPosition ?? DEFAULT_PIE_CONFIG.textPosition;
config = structuredCleanClone(DEFAULT_PIE_CONFIG, conf);
};
const getConfig = (): Required<PieDiagramConfig> => config;
const resetConfig = (): void => {
config.useWidth = DEFAULT_PIE_CONFIG.useWidth;
config.useMaxWidth = DEFAULT_PIE_CONFIG.useMaxWidth;
config.textPosition = DEFAULT_PIE_CONFIG.textPosition;
config = structuredCleanClone(DEFAULT_PIE_CONFIG);
};
const parseDirective: ParseDirectiveDefinition = (statement, context, type) => {
@ -51,7 +48,7 @@ const parseDirective: ParseDirectiveDefinition = (statement, context, type) => {
};
const clear = (): void => {
sections = structuredClone(DEFAULT_PIE_DB.sections);
sections = structuredCleanClone(DEFAULT_PIE_DB.sections);
showData = DEFAULT_PIE_DB.showData;
commonClear();
resetConfig();