From d5f11fc80a391969b175b58a02ba80e9c57946d1 Mon Sep 17 00:00:00 2001 From: Reda Al Sulais Date: Thu, 10 Aug 2023 22:07:31 +0300 Subject: [PATCH 1/3] convert `assignWithDepth` to TS --- ...{assignWithDepth.js => assignWithDepth.ts} | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) rename packages/mermaid/src/{assignWithDepth.js => assignWithDepth.ts} (65%) diff --git a/packages/mermaid/src/assignWithDepth.js b/packages/mermaid/src/assignWithDepth.ts similarity index 65% rename from packages/mermaid/src/assignWithDepth.js rename to packages/mermaid/src/assignWithDepth.ts index 6f2e706ab..22d0da192 100644 --- a/packages/mermaid/src/assignWithDepth.js +++ b/packages/mermaid/src/assignWithDepth.ts @@ -1,32 +1,36 @@ -'use strict'; +/* eslint-disable @typescript-eslint/no-explicit-any */ + /** - * @function assignWithDepth Extends the functionality of {@link ObjectConstructor.assign} with the + * assignWithDepth Extends the functionality of {@link ObjectConstructor.assign} with the * ability to merge arbitrary-depth objects For each key in src with path `k` (recursively) * performs an Object.assign(dst[`k`], src[`k`]) with a slight change from the typical handling of - * undefined for dst[`k`]: instead of raising an error, dst[`k`] is auto-initialized to {} and + * undefined for dst[`k`]: instead of raising an error, dst[`k`] is auto-initialized to `{}` and * effectively merged with src[`k`]

Additionally, dissimilar types will not clobber unless the * config.clobber parameter === true. Example: * - * ```js - * let config_0 = { foo: { bar: 'bar' }, bar: 'foo' }; - * let config_1 = { foo: 'foo', bar: 'bar' }; - * let result = assignWithDepth(config_0, config_1); - * console.log(result); - * //-> result: { foo: { bar: 'bar' }, bar: 'bar' } - * ``` + * ``` + * const config_0 = { foo: { bar: 'bar' }, bar: 'foo' }; + * const config_1 = { foo: 'foo', bar: 'bar' }; + * const result = assignWithDepth(config_0, config_1); + * console.log(result); + * //-> result: { foo: { bar: 'bar' }, bar: 'bar' } + * ``` * * Traditional Object.assign would have clobbered foo in config_0 with foo in config_1. If src is a * destructured array of objects and dst is not an array, assignWithDepth will apply each element * of src to dst in order. - * @param {any} dst - The destination of the merge - * @param {any} src - The source object(s) to merge into destination - * @param {{ depth: number; clobber: boolean }} [config] - Depth: depth - * to traverse within src and dst for merging - clobber: should dissimilar types clobber (default: - * { depth: 2, clobber: false }). Default is `{ depth: 2, clobber: false }` - * @returns {any} + * @param dst - The destination of the merge + * @param src - The source object(s) to merge into destination + * @param config - + * * depth: depth to traverse within src and dst for merging + * * clobber: should dissimilar types clobber */ -const assignWithDepth = function (dst, src, config) { - const { depth, clobber } = Object.assign({ depth: 2, clobber: false }, config); +const assignWithDepth = ( + dst: any, + src: any, + config: { depth: number; clobber: boolean } = { depth: 2, clobber: false } +): any => { + const { depth, clobber } = config; if (Array.isArray(src) && !Array.isArray(dst)) { src.forEach((s) => assignWithDepth(dst, s, config)); return dst; From d8dd68cad29d762e6523dd53475584c12f010a94 Mon Sep 17 00:00:00 2001 From: Reda Al Sulais Date: Fri, 11 Aug 2023 01:39:04 +0300 Subject: [PATCH 2/3] redeclare `config` parameter add default value for each variable --- packages/mermaid/src/assignWithDepth.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/mermaid/src/assignWithDepth.ts b/packages/mermaid/src/assignWithDepth.ts index 22d0da192..263b005bc 100644 --- a/packages/mermaid/src/assignWithDepth.ts +++ b/packages/mermaid/src/assignWithDepth.ts @@ -28,9 +28,9 @@ const assignWithDepth = ( dst: any, src: any, - config: { depth: number; clobber: boolean } = { depth: 2, clobber: false } + { depth = 2, clobber = false }: { depth: number; clobber: boolean } = { depth: 2, clobber: false } ): any => { - const { depth, clobber } = config; + const config = Object.assign({ depth, clobber }); if (Array.isArray(src) && !Array.isArray(dst)) { src.forEach((s) => assignWithDepth(dst, s, config)); return dst; From c37c494a1ea53b45fd5810ab3076921a2eb126b9 Mon Sep 17 00:00:00 2001 From: Reda Al Sulais Date: Fri, 11 Aug 2023 19:54:37 +0300 Subject: [PATCH 3/3] Update assignWithDepth.ts --- packages/mermaid/src/assignWithDepth.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/mermaid/src/assignWithDepth.ts b/packages/mermaid/src/assignWithDepth.ts index 263b005bc..831825779 100644 --- a/packages/mermaid/src/assignWithDepth.ts +++ b/packages/mermaid/src/assignWithDepth.ts @@ -28,9 +28,9 @@ const assignWithDepth = ( dst: any, src: any, - { depth = 2, clobber = false }: { depth: number; clobber: boolean } = { depth: 2, clobber: false } + { depth = 2, clobber = false }: { depth?: number; clobber?: boolean } = {} ): any => { - const config = Object.assign({ depth, clobber }); + const config: { depth: number; clobber: boolean } = { depth, clobber }; if (Array.isArray(src) && !Array.isArray(dst)) { src.forEach((s) => assignWithDepth(dst, s, config)); return dst;