utils.ts — astro Source File
Architecture documentation for utils.ts, a typescript file in the astro codebase. 3 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 77c4177d_ca70_d1e3_9542_4dbf91aa1dfd["utils.ts"] a2fc192c_cdf9_df62_81bf_9c18a80b317e["./fonts/constants.js"] 77c4177d_ca70_d1e3_9542_4dbf91aa1dfd --> a2fc192c_cdf9_df62_81bf_9c18a80b317e ea387312_d0a2_bb52_dafc_3872f7307f95["../../src/assets/fonts/types.js"] 77c4177d_ca70_d1e3_9542_4dbf91aa1dfd --> ea387312_d0a2_bb52_dafc_3872f7307f95 07866fa4_1b50_10c2_1b6b_5b1f385406b5["unifont"] 77c4177d_ca70_d1e3_9542_4dbf91aa1dfd --> 07866fa4_1b50_10c2_1b6b_5b1f385406b5 style 77c4177d_ca70_d1e3_9542_4dbf91aa1dfd fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import type * as unifont from 'unifont';
import { FONT_TYPES, GENERIC_FALLBACK_NAMES } from './constants.js';
import type { CssProperties, FontType, GenericFallbackName } from './types.js';
/**
* Turns unifont font face data into generic CSS properties, to be consumed by the CSS renderer.
*/
export function unifontFontFaceDataToProperties(
font: Partial<unifont.FontFaceData>,
): CssProperties {
return {
src: font.src ? renderFontSrc(font.src) : undefined,
'font-display': font.display ?? 'swap',
'unicode-range': font.unicodeRange?.length ? font.unicodeRange.join(',') : undefined,
'font-weight': renderFontWeight(font.weight),
'font-style': font.style,
'font-stretch': font.stretch,
'font-feature-settings': font.featureSettings,
'font-variation-settings': font.variationSettings,
};
}
export function renderFontWeight(weight: unifont.FontFaceData['weight']): string | undefined {
return Array.isArray(weight) ? weight.join(' ') : weight?.toString();
}
/**
* Turns unifont font face data src into a valid CSS property.
* Adapted from https://github.com/nuxt/fonts/blob/main/src/css/render.ts#L68-L81
*/
export function renderFontSrc(
sources: Exclude<unifont.FontFaceData['src'][number], string>[],
): string {
return sources
.map((src) => {
if ('name' in src) {
return `local("${src.name}")`;
}
let rendered = `url("${src.url}")`;
if (src.format) {
rendered += ` format("${src.format}")`;
}
if (src.tech) {
rendered += ` tech(${src.tech})`;
}
return rendered;
})
.join(', ');
}
const QUOTES_RE = /^["']|["']$/g;
/**
* Removes the quotes from a string. Used for family names
*/
export function withoutQuotes(str: string): string {
return str.trim().replace(QUOTES_RE, '');
}
export function isFontType(str: string): str is FontType {
return (FONT_TYPES as Readonly<Array<string>>).includes(str);
}
export function isGenericFontFamily(str: string): str is GenericFallbackName {
return (GENERIC_FALLBACK_NAMES as unknown as Array<string>).includes(str);
}
export function dedupe<const T extends Array<any>>(arr: T): T {
return [...new Set(arr)] as T;
}
export function sortObjectByKey<T extends Record<string, any>>(unordered: T): T {
const ordered = Object.keys(unordered)
.sort()
.reduce((obj, key) => {
const value = unordered[key];
// @ts-expect-error Type 'T' is generic and can only be indexed for reading. That's fine here
obj[key] = Array.isArray(value)
? value.map((v) => (typeof v === 'object' && v !== null ? sortObjectByKey(v) : v))
: typeof value === 'object' && value !== null
? sortObjectByKey(value)
: value;
return obj;
}, {} as T);
return ordered;
}
Domain
Subdomains
Functions
Dependencies
- ../../src/assets/fonts/types.js
- ./fonts/constants.js
- unifont
Source
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): dedupe, isFontType, isGenericFontFamily, renderFontSrc, renderFontWeight, sortObjectByKey, unifontFontFaceDataToProperties, withoutQuotes.
What does utils.ts depend on?
utils.ts imports 3 module(s): ../../src/assets/fonts/types.js, ./fonts/constants.js, unifont.
Where is utils.ts in the architecture?
utils.ts is located at packages/astro/src/assets/fonts/utils.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/assets/fonts).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free