Home / File/ utils.js — svelte Source File

utils.js — svelte Source File

Architecture documentation for utils.js, a javascript file in the svelte codebase. 0 imports, 36 dependents.

File javascript SharedInternal BitFlags 36 dependents 8 functions

Entity Profile

Dependency Diagram

graph LR
  cb946435_ce66_d1e8_6bee_287bdb07e7c5["utils.js"]
  717fc8d5_bdb4_4b73_d6c5_c4a367a60cf2["index-client.js"]
  717fc8d5_bdb4_4b73_d6c5_c4a367a60cf2 --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  8cc0d8dc_e5ce_948a_44b5_69c173cc6f73["index-server.js"]
  8cc0d8dc_e5ce_948a_44b5_69c173cc6f73 --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  ad878f27_7e4b_5069_1b06_1750c58a617d["ownership.js"]
  ad878f27_7e4b_5069_1b06_1750c58a617d --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  277e61a1_318f_c1a6_cf40_f6e1a0f3aa2f["await.js"]
  277e61a1_318f_c1a6_cf40_f6e1a0f3aa2f --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  ca0d28d0_c4b0_db5c_32c9_bdad64d5deaa["each.js"]
  ca0d28d0_c4b0_db5c_32c9_bdad64d5deaa --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  0acd2537_e1bf_d7ae_30d5_407378cfa4d3["attributes.js"]
  0acd2537_e1bf_d7ae_30d5_407378cfa4d3 --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  2bd13401_d37c_cb90_2161_eedf0d33a0c9["props.js"]
  2bd13401_d37c_cb90_2161_eedf0d33a0c9 --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  dbe1ea86_6e94_eeeb_a2d1_1db78ca967bc["select.js"]
  dbe1ea86_6e94_eeeb_a2d1_1db78ca967bc --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  1a45e32c_24fc_a764_ca2d_b81df9bec325["custom-element.js"]
  1a45e32c_24fc_a764_ca2d_b81df9bec325 --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  2c990bd1_acff_5910_3af2_ab75f655b31b["events.js"]
  2c990bd1_acff_5910_3af2_ab75f655b31b --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  e29c417d_6c26_6125_97f5_600e95e02dac["transitions.js"]
  e29c417d_6c26_6125_97f5_600e95e02dac --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  10959c39_ee67_ccc9_0299_768227609d29["event-modifiers.js"]
  10959c39_ee67_ccc9_0299_768227609d29 --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  309b6da7_c79b_4906_049d_a61ee844e1d7["lifecycle.js"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  b0dea6fe_a39b_1574_4bd3_e0f24d88074a["misc.js"]
  b0dea6fe_a39b_1574_4bd3_e0f24d88074a --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  style cb946435_ce66_d1e8_6bee_287bdb07e7c5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

// Store the references to globals in case someone tries to monkey patch these, causing the below
// to de-opt (this occurs often when using popular extensions).
export var is_array = Array.isArray;
export var index_of = Array.prototype.indexOf;
export var includes = Array.prototype.includes;
export var array_from = Array.from;
export var object_keys = Object.keys;
export var define_property = Object.defineProperty;
export var get_descriptor = Object.getOwnPropertyDescriptor;
export var get_descriptors = Object.getOwnPropertyDescriptors;
export var object_prototype = Object.prototype;
export var array_prototype = Array.prototype;
export var get_prototype_of = Object.getPrototypeOf;
export var is_extensible = Object.isExtensible;

/**
 * @param {any} thing
 * @returns {thing is Function}
 */
export function is_function(thing) {
	return typeof thing === 'function';
}

export const noop = () => {};

// Adapted from https://github.com/then/is-promise/blob/master/index.js
// Distributed under MIT License https://github.com/then/is-promise/blob/master/LICENSE

/**
 * @template [T=any]
 * @param {any} value
 * @returns {value is PromiseLike<T>}
 */
export function is_promise(value) {
	return typeof value?.then === 'function';
}

/** @param {Function} fn */
export function run(fn) {
	return fn();
}

/** @param {Array<() => void>} arr */
export function run_all(arr) {
	for (var i = 0; i < arr.length; i++) {
		arr[i]();
	}
}

/**
 * TODO replace with Promise.withResolvers once supported widely enough
 * @template [T=void]
 */
export function deferred() {
	/** @type {(value: T) => void} */
	var resolve;

	/** @type {(reason: any) => void} */
	var reject;

	/** @type {Promise<T>} */
	var promise = new Promise((res, rej) => {
		resolve = res;
		reject = rej;
	});

	// @ts-expect-error
	return { promise, resolve, reject };
}

/**
 * @template V
 * @param {V} value
 * @param {V | (() => V)} fallback
 * @param {boolean} [lazy]
 * @returns {V}
 */
export function fallback(value, fallback, lazy = false) {
	return value === undefined
		? lazy
			? /** @type {() => V} */ (fallback)()
			: /** @type {V} */ (fallback)
		: value;
}

/**
 * When encountering a situation like `let [a, b, c] = $derived(blah())`,
 * we need to stash an intermediate value that `a`, `b`, and `c` derive
 * from, in case it's an iterable
 * @template T
 * @param {ArrayLike<T> | Iterable<T>} value
 * @param {number} [n]
 * @returns {Array<T>}
 */
export function to_array(value, n) {
	// return arrays unchanged
	if (Array.isArray(value)) {
		return value;
	}

	// if value is not iterable, or `n` is unspecified (indicates a rest
	// element, which means we're not concerned about unbounded iterables)
	// convert to an array with `Array.from`
	if (n === undefined || !(Symbol.iterator in value)) {
		return Array.from(value);
	}

	// otherwise, populate an array with `n` values

	/** @type {T[]} */
	const array = [];

	for (const element of value) {
		array.push(element);
		if (array.length === n) break;
	}

	return array;
}

Subdomains

Imported By

Frequently Asked Questions

What does utils.js do?
utils.js is a source file in the svelte codebase, written in javascript. It belongs to the SharedInternal domain, BitFlags subdomain.
What functions are defined in utils.js?
utils.js defines 8 function(s): deferred, fallback, is_function, is_promise, noop, run, run_all, to_array.
What files import utils.js?
utils.js is imported by 36 file(s): attributes.js, await.js, batch.js, clone.js, custom-element.js, deriveds.js, dev.js, each.js, and 28 more.
Where is utils.js in the architecture?
utils.js is located at packages/svelte/src/internal/shared/utils.js (domain: SharedInternal, subdomain: BitFlags, directory: packages/svelte/src/internal/shared).

Analyze Your Own Codebase

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

Try Supermodel Free