mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-28 07:03:17 +08:00
Merge remote-tracking branch 'upstream/develop' into chore/convert-svgDrawCommon-to-ts
This commit is contained in:
commit
541ee1eade
@ -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)
|
* 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
|
* 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`]<p> Additionally, dissimilar types will not clobber unless the
|
* effectively merged with src[`k`]<p> Additionally, dissimilar types will not clobber unless the
|
||||||
* config.clobber parameter === true. Example:
|
* config.clobber parameter === true. Example:
|
||||||
*
|
*
|
||||||
* ```js
|
* ```
|
||||||
* let config_0 = { foo: { bar: 'bar' }, bar: 'foo' };
|
* const config_0 = { foo: { bar: 'bar' }, bar: 'foo' };
|
||||||
* let config_1 = { foo: 'foo', bar: 'bar' };
|
* const config_1 = { foo: 'foo', bar: 'bar' };
|
||||||
* let result = assignWithDepth(config_0, config_1);
|
* const result = assignWithDepth(config_0, config_1);
|
||||||
* console.log(result);
|
* console.log(result);
|
||||||
* //-> result: { foo: { bar: 'bar' }, bar: 'bar' }
|
* //-> 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
|
* 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
|
* destructured array of objects and dst is not an array, assignWithDepth will apply each element
|
||||||
* of src to dst in order.
|
* of src to dst in order.
|
||||||
* @param {any} dst - The destination of the merge
|
* @param dst - The destination of the merge
|
||||||
* @param {any} src - The source object(s) to merge into destination
|
* @param src - The source object(s) to merge into destination
|
||||||
* @param {{ depth: number; clobber: boolean }} [config] - Depth: depth
|
* @param config -
|
||||||
* to traverse within src and dst for merging - clobber: should dissimilar types clobber (default:
|
* * depth: depth to traverse within src and dst for merging
|
||||||
* { depth: 2, clobber: false }). Default is `{ depth: 2, clobber: false }`
|
* * clobber: should dissimilar types clobber
|
||||||
* @returns {any}
|
|
||||||
*/
|
*/
|
||||||
const assignWithDepth = function (dst, src, config) {
|
const assignWithDepth = (
|
||||||
const { depth, clobber } = Object.assign({ depth: 2, clobber: false }, config);
|
dst: any,
|
||||||
|
src: any,
|
||||||
|
{ depth = 2, clobber = false }: { depth?: number; clobber?: boolean } = {}
|
||||||
|
): any => {
|
||||||
|
const config: { depth: number; clobber: boolean } = { depth, clobber };
|
||||||
if (Array.isArray(src) && !Array.isArray(dst)) {
|
if (Array.isArray(src) && !Array.isArray(dst)) {
|
||||||
src.forEach((s) => assignWithDepth(dst, s, config));
|
src.forEach((s) => assignWithDepth(dst, s, config));
|
||||||
return dst;
|
return dst;
|
Loading…
x
Reference in New Issue
Block a user