Home / File/ utils.ts — astro Source File

utils.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 4 imports 8 functions

Entity Profile

Dependency Diagram

graph LR
  a37e7fee_e130_2a00_6f99_81cf3cf65c83["utils.ts"]
  10d4e39f_edb6_3e34_aa93_ae1211e7da05["../types/public/internal.js"]
  a37e7fee_e130_2a00_6f99_81cf3cf65c83 --> 10d4e39f_edb6_3e34_aa93_ae1211e7da05
  38ee36f6_1b8f_5a62_1295_989b44329ca0["js-yaml"]
  a37e7fee_e130_2a00_6f99_81cf3cf65c83 --> 38ee36f6_1b8f_5a62_1295_989b44329ca0
  700c64be_9e12_6323_a727_8d7af053511b["smol-toml"]
  a37e7fee_e130_2a00_6f99_81cf3cf65c83 --> 700c64be_9e12_6323_a727_8d7af053511b
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  a37e7fee_e130_2a00_6f99_81cf3cf65c83 --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style a37e7fee_e130_2a00_6f99_81cf3cf65c83 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { YAMLException } from 'js-yaml';
import type { TomlError } from 'smol-toml';
import type { ErrorPayload as ViteErrorPayload } from 'vite';
import type { SSRError } from '../../types/public/internal.js';

/**
 * Get the line and character based on the offset
 * @param offset The index of the position
 * @param text The text for which the position should be retrieved
 */
export function positionAt(
	offset: number,
	text: string,
): {
	line: number;
	column: number;
} {
	const lineOffsets = getLineOffsets(text);
	offset = Math.max(0, Math.min(text.length, offset));

	let low = 0;
	let high = lineOffsets.length;
	if (high === 0) {
		return {
			line: 0,
			column: offset,
		};
	}

	while (low <= high) {
		const mid = Math.floor((low + high) / 2);
		const lineOffset = lineOffsets[mid];

		if (lineOffset === offset) {
			return {
				line: mid,
				column: 0,
			};
		} else if (offset > lineOffset) {
			low = mid + 1;
		} else {
			high = mid - 1;
		}
	}

	// low is the least x for which the line offset is larger than the current offset
	// or array.length if no line offset is larger than the current offset
	const line = low - 1;
	return { line, column: offset - lineOffsets[line] };
}

function getLineOffsets(text: string) {
	const lineOffsets = [];
	let isLineStart = true;

	for (let i = 0; i < text.length; i++) {
		if (isLineStart) {
			lineOffsets.push(i);
			isLineStart = false;
		}
// ... (62 more lines)

Domain

Subdomains

Dependencies

  • ../types/public/internal.js
  • js-yaml
  • smol-toml
  • vite

Frequently Asked Questions

What does utils.ts do?
utils.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 utils.ts?
utils.ts defines 8 function(s): createSafeError, formatTOMLError, formatYAMLException, getLineOffsets, isTOMLError, isYAMLException, normalizeLF, positionAt.
What does utils.ts depend on?
utils.ts imports 4 module(s): ../types/public/internal.js, js-yaml, smol-toml, vite.
Where is utils.ts in the architecture?
utils.ts is located at packages/astro/src/core/errors/utils.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/core/errors).

Analyze Your Own Codebase

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

Try Supermodel Free