From be106befff5ce467e49f26d136bdb5b5b42aff28 Mon Sep 17 00:00:00 2001 From: Reda Al Sulais Date: Thu, 10 Aug 2023 23:10:06 +0300 Subject: [PATCH] create `structuredCleanClone` helper function --- packages/mermaid/src/cleanClone.ts | 24 ++++++++++++++++++++++ packages/mermaid/src/diagrams/pie/pieDb.ts | 13 +++++------- 2 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 packages/mermaid/src/cleanClone.ts diff --git a/packages/mermaid/src/cleanClone.ts b/packages/mermaid/src/cleanClone.ts new file mode 100644 index 000000000..8f9cb7c26 --- /dev/null +++ b/packages/mermaid/src/cleanClone.ts @@ -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(defaultData: T, data?: Partial): T { + const cleanValue: T = removeUndefined(data); + return structuredClone({ ...defaultData, ...cleanValue }); +} diff --git a/packages/mermaid/src/diagrams/pie/pieDb.ts b/packages/mermaid/src/diagrams/pie/pieDb.ts index 78df0431b..bdd7957c2 100644 --- a/packages/mermaid/src/diagrams/pie/pieDb.ts +++ b/packages/mermaid/src/diagrams/pie/pieDb.ts @@ -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 = { useMaxWidth: true, @@ -30,20 +31,16 @@ export const DEFAULT_PIE_DB: RequiredDeep = { let sections: Sections = DEFAULT_PIE_DB.sections; let showData: boolean = DEFAULT_PIE_DB.showData; -const config: Required = structuredClone(DEFAULT_PIE_CONFIG); +let config: Required = 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 => 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();