Home / File/ utils.ts — astro Source File

utils.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 2 imports 5 functions

Entity Profile

Dependency Diagram

graph LR
  7b0bb4ed_570d_ff47_83a8_9e0495974e66["utils.ts"]
  c32d12e2_d85e_28c0_eea7_9b29629857e0["../types/public/config.js"]
  7b0bb4ed_570d_ff47_83a8_9e0495974e66 --> c32d12e2_d85e_28c0_eea7_9b29629857e0
  fe669073_2e4e_f283_db9d_18a6d7df3f04["../i18n/index.js"]
  7b0bb4ed_570d_ff47_83a8_9e0495974e66 --> fe669073_2e4e_f283_db9d_18a6d7df3f04
  style 7b0bb4ed_570d_ff47_83a8_9e0495974e66 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { Locales } from '../types/public/config.js';
import { getAllCodes, normalizeTheLocale, normalizeThePath } from './index.js';

type BrowserLocale = {
	locale: string;
	qualityValue: number | undefined;
};

/**
 * Parses the value of the `Accept-Language` header:
 *
 * More info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language
 *
 * Complex example: `fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5`
 *
 */
export function parseLocale(header: string): BrowserLocale[] {
	// Any language, early return
	if (header === '*') {
		return [{ locale: header, qualityValue: undefined }];
	}
	const result: BrowserLocale[] = [];
	// we split by `,` and trim the white spaces
	const localeValues = header.split(',').map((str) => str.trim());

	for (const localeValue of localeValues) {
		// split the locale name from the quality value
		const split = localeValue.split(';').map((str) => str.trim());
		const localeName: string = split[0];
		const qualityValue: string | undefined = split[1];

		if (!split) {
			// invalid value
			continue;
		}

		// we check if the quality value is present, and it is actually `q=`
		if (qualityValue && qualityValue.startsWith('q=')) {
			const qualityValueAsFloat = Number.parseFloat(qualityValue.slice('q='.length));
			// The previous operation can return a `NaN`, so we check if it is a safe operation
			if (Number.isNaN(qualityValueAsFloat) || qualityValueAsFloat > 1) {
				result.push({
					locale: localeName,
					qualityValue: undefined,
				});
			} else {
				result.push({
					locale: localeName,
					qualityValue: qualityValueAsFloat,
				});
			}
		} else {
			result.push({
				locale: localeName,
				qualityValue: undefined,
			});
		}
	}

	return result;
// ... (130 more lines)

Domain

Subdomains

Dependencies

  • ../i18n/index.js
  • ../types/public/config.js

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 5 function(s): computeCurrentLocale, computePreferredLocale, computePreferredLocaleList, parseLocale, sortAndFilterLocales.
What does utils.ts depend on?
utils.ts imports 2 module(s): ../i18n/index.js, ../types/public/config.js.
Where is utils.ts in the architecture?
utils.ts is located at packages/astro/src/i18n/utils.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/i18n).

Analyze Your Own Codebase

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

Try Supermodel Free