Home / Function/ clone() — svelte Function Reference

clone() — svelte Function Reference

Architecture documentation for the clone() function in clone.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  cb8572f5_46cc_29c0_76fa_a47b56411d5b["clone()"]
  258b696c_d923_7010_457a_b58908a057b0["clone.js"]
  cb8572f5_46cc_29c0_76fa_a47b56411d5b -->|defined in| 258b696c_d923_7010_457a_b58908a057b0
  532a740d_d410_0fd6_983a_933cb13808e7["snapshot()"]
  532a740d_d410_0fd6_983a_933cb13808e7 -->|calls| cb8572f5_46cc_29c0_76fa_a47b56411d5b
  style cb8572f5_46cc_29c0_76fa_a47b56411d5b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/internal/shared/clone.js lines 57–137

function clone(value, cloned, path, paths, original = null, no_tojson = false) {
	if (typeof value === 'object' && value !== null) {
		var unwrapped = cloned.get(value);
		if (unwrapped !== undefined) return unwrapped;

		if (value instanceof Map) return /** @type {Snapshot<T>} */ (new Map(value));
		if (value instanceof Set) return /** @type {Snapshot<T>} */ (new Set(value));

		if (is_array(value)) {
			var copy = /** @type {Snapshot<any>} */ (Array(value.length));
			cloned.set(value, copy);

			if (original !== null) {
				cloned.set(original, copy);
			}

			for (var i = 0; i < value.length; i += 1) {
				var element = value[i];
				if (i in value) {
					copy[i] = clone(element, cloned, DEV ? `${path}[${i}]` : path, paths, null, no_tojson);
				}
			}

			return copy;
		}

		if (get_prototype_of(value) === object_prototype) {
			/** @type {Snapshot<any>} */
			copy = {};
			cloned.set(value, copy);

			if (original !== null) {
				cloned.set(original, copy);
			}

			for (var key in value) {
				copy[key] = clone(
					// @ts-expect-error
					value[key],
					cloned,
					DEV ? `${path}.${key}` : path,
					paths,
					null,
					no_tojson
				);
			}

			return copy;
		}

		if (value instanceof Date) {
			return /** @type {Snapshot<T>} */ (structuredClone(value));
		}

		if (typeof (/** @type {T & { toJSON?: any } } */ (value).toJSON) === 'function' && !no_tojson) {
			return clone(
				/** @type {T & { toJSON(): any } } */ (value).toJSON(),
				cloned,
				DEV ? `${path}.toJSON()` : path,
				paths,
				// Associate the instance with the toJSON clone
				value
			);
		}
	}

	if (value instanceof EventTarget) {
		// can't be cloned
		return /** @type {Snapshot<T>} */ (value);
	}

	try {
		return /** @type {Snapshot<T>} */ (structuredClone(value));
	} catch (e) {
		if (DEV) {
			paths.push(path);
		}

		return /** @type {Snapshot<T>} */ (value);
	}
}

Subdomains

Called By

Frequently Asked Questions

What does clone() do?
clone() is a function in the svelte codebase, defined in packages/svelte/src/internal/shared/clone.js.
Where is clone() defined?
clone() is defined in packages/svelte/src/internal/shared/clone.js at line 57.
What calls clone()?
clone() is called by 1 function(s): snapshot.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free