Home / File/ util.ts — astro Source File

util.ts — astro Source File

Architecture documentation for util.ts, a typescript file in the astro codebase. 5 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 5 imports 17 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  05241a8b_1820_8286_5770_4da18477ecde["util.ts"]
  10d4e39f_edb6_3e34_aa93_ae1211e7da05["../types/public/internal.js"]
  05241a8b_1820_8286_5770_4da18477ecde --> 10d4e39f_edb6_3e34_aa93_ae1211e7da05
  a7eea85d_ea97_57b1_9a24_f99face6a97d["./escape.js"]
  05241a8b_1820_8286_5770_4da18477ecde --> a7eea85d_ea97_57b1_9a24_f99face6a97d
  a28c4b4b_41a8_d771_0fc1_f9fbc7ccded6["../util.js"]
  05241a8b_1820_8286_5770_4da18477ecde --> a28c4b4b_41a8_d771_0fc1_f9fbc7ccded6
  b0a43ea3_d8cf_4023_b3ca_f683a93e9dbb["./common.js"]
  05241a8b_1820_8286_5770_4da18477ecde --> b0a43ea3_d8cf_4023_b3ca_f683a93e9dbb
  a5f74094_5c21_e6b7_ab26_8d2c0e575403["clsx"]
  05241a8b_1820_8286_5770_4da18477ecde --> a5f74094_5c21_e6b7_ab26_8d2c0e575403
  style 05241a8b_1820_8286_5770_4da18477ecde fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { clsx } from 'clsx';
import type { SSRElement } from '../../../types/public/internal.js';
import { HTMLString, markHTMLString } from '../escape.js';
import { isPromise } from '../util.js';
import type { RenderDestination, RenderDestinationChunk, RenderFunction } from './common.js';

export const voidElementNames =
	/^(area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)$/i;
const htmlBooleanAttributes =
	/^(?:allowfullscreen|async|autofocus|autoplay|checked|controls|default|defer|disabled|disablepictureinpicture|disableremoteplayback|formnovalidate|hidden|inert|loop|muted|nomodule|novalidate|open|playsinline|readonly|required|reversed|scoped|seamless|selected|itemscope)$/i;

const AMPERSAND_REGEX = /&/g;
const DOUBLE_QUOTE_REGEX = /"/g;

const STATIC_DIRECTIVES = new Set(['set:html', 'set:text']);

// converts (most) arbitrary strings to valid JS identifiers
const toIdent = (k: string) =>
	k.trim().replace(/(?!^)\b\w|\s+|\W+/g, (match, index) => {
		if (/\W/.test(match)) return '';
		return index === 0 ? match : match.toUpperCase();
	});

export const toAttributeString = (value: any, shouldEscape = true) =>
	shouldEscape
		? String(value).replace(AMPERSAND_REGEX, '&').replace(DOUBLE_QUOTE_REGEX, '"')
		: value;

const kebab = (k: string) =>
	k.toLowerCase() === k ? k : k.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);

export const toStyleString = (obj: Record<string, any>) =>
	Object.entries(obj)
		.filter(([_, v]) => (typeof v === 'string' && v.trim()) || typeof v === 'number')
		.map(([k, v]) => {
			if (k[0] !== '-' && k[1] !== '-') return `${kebab(k)}:${v}`;
			return `${k}:${v}`;
		})
		.join(';');

// Adds variables to an inline script.
export function defineScriptVars(vars: Record<any, any>) {
	let output = '';
	for (const [key, value] of Object.entries(vars)) {
		// Use const instead of let as let global unsupported with Safari
		// https://stackoverflow.com/questions/29194024/cant-use-let-keyword-in-safari-javascript
		output += `const ${toIdent(key)} = ${JSON.stringify(value)?.replace(
			/<\/script>/g,
			'\\x3C/script>',
		)};\n`;
	}
	return markHTMLString(output);
}

export function formatList(values: string[]): string {
	if (values.length === 1) {
		return values[0];
	}
	return `${values.slice(0, -1).join(', ')} or ${values[values.length - 1]}`;
}
// ... (258 more lines)

Domain

Subdomains

Dependencies

  • ../types/public/internal.js
  • ../util.js
  • ./common.js
  • ./escape.js
  • clsx

Frequently Asked Questions

What does util.ts do?
util.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in util.ts?
util.ts defines 17 function(s): addAttribute, createBufferedRenderer, defineScriptVars, formatList, handleBooleanAttribute, internalSpreadAttributes, isCustomElement, isHttpUrl, kebab, noop, and 7 more.
What does util.ts depend on?
util.ts imports 5 module(s): ../types/public/internal.js, ../util.js, ./common.js, ./escape.js, clsx.
Where is util.ts in the architecture?
util.ts is located at packages/astro/src/runtime/server/render/util.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/runtime/server/render).

Analyze Your Own Codebase

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

Try Supermodel Free