API Reference

deep-clone

API entries in this module: 6

Current deep-clone implementation

IDeepClone resolves to DefaultDeepClone. It receives IDeepCloneList via multi-inject and tries each clone implementation in injected order until one succeeds.

In the default module configuration, list order is: StructuredDeepClone then LodashDeepClone. If one strategy throws, the next strategy is attempted.

LodashDeepClone unwraps proxy-wrapped values to their original targets using IProxyRegistry. During clone traversal it also calls IDeepCloneExcept (default: DefaultDeepCloneExcept) to substitute special values, such as Promise/Observable with their last resolved/emitted value.

Example: use IDeepClone service

import {
  InjectionContainer,
  type IDeepClone,
  RsXCoreInjectionTokens,
  RsXCoreModule,
} from '@rs-x/core';

await InjectionContainer.load(RsXCoreModule);

const deepClone = InjectionContainer.get(
  RsXCoreInjectionTokens.IDeepClone,
) as IDeepClone;

const source = {
  user: { id: 1, name: 'Ada' },
  tags: ['core', 'docs'],
};

const cloned = deepClone.clone(source) as typeof source;

console.log(cloned.user.name); // Ada
console.log(cloned === source); // false

How to extend or modify

Register your own IDeepClone implementation and override IDeepCloneList order. Earlier entries run first, so put domain-specific strategies before generic fallbacks.

Override deep-clone strategy order

import {
  InjectionContainer,
  overrideMultiInjectServices,
  RsXCoreInjectionTokens,
  type IMultiInjectService,
  RsXCoreModule,
} from '@rs-x/core';

await InjectionContainer.load(RsXCoreModule);

const customDeepCloneList: IMultiInjectService[] = [
  // your custom strategy first
  { target: MyDomainDeepClone, token: Symbol('MyDomainDeepClone') },
  // keep default fallbacks
  { target: StructuredDeepClone, token: RsXCoreInjectionTokens.IStructuredDeepClone },
  { target: LodashDeepClone, token: RsXCoreInjectionTokens.ILodashDeepClone },
];

overrideMultiInjectServices(
  InjectionContainer,
  RsXCoreInjectionTokens.IDeepCloneList,
  customDeepCloneList,

Module API entries