MCPcopy Index your code
hub / github.com/fontsource/fontsource / selectVariableAxisKey

Function selectVariableAxisKey

packages/core/src/utils/variable.ts:139–190  ·  view source on GitHub ↗
(
	variableConfig: VariableAxisConfig,
	selectedAxes: Iterable<string>,
)

Source from the content-addressed store, hash-verified

137 * - anything involving multiple custom axes -> `full`
138 */
139export const selectVariableAxisKey = (
140 variableConfig: VariableAxisConfig,
141 selectedAxes: Iterable<string>,
142): VariableAxisKey => {
143 const publishedAxisKeys = getVariableAxisKeys(variableConfig);
144 const defaultKey = determineAxisKey(variableConfig);
145
146 // Normalize all axes for comparison.
147 const activeAxes = Array.from(selectedAxes)
148 .map((axis) => axis.trim().toLowerCase())
149 .filter(Boolean)
150 .filter((axis) => axis !== 'ital');
151
152 // Deduplicate after normalization.
153 const uniqueAxes = [...new Set(activeAxes)];
154
155 if (uniqueAxes.length === 0) {
156 return defaultKey;
157 }
158
159 // Single axis should go for a direct lookup.
160 if (uniqueAxes.length === 1) {
161 return findAxisKey(publishedAxisKeys, uniqueAxes[0]) ?? defaultKey;
162 }
163
164 // Two axes where one is `wght` should still try for a direct lookup of the other axis as `wght` is implicit in most bundles.
165 if (uniqueAxes.length === 2 && uniqueAxes.includes('wght')) {
166 const directAxis = uniqueAxes.find((axis) => axis !== 'wght');
167
168 if (directAxis) {
169 const directAxisKey = findAxisKey(publishedAxisKeys, directAxis);
170 if (directAxisKey) {
171 return directAxisKey;
172 }
173 }
174 }
175
176 // If every requested axis is a standard axis, try the `standard` bundle.
177 const allStandard = uniqueAxes.every((axis) =>
178 STANDARD_VARIABLE_AXES.has(axis),
179 );
180
181 if (allStandard) {
182 const standardAxisKey = findAxisKey(publishedAxisKeys, 'standard');
183 if (standardAxisKey) {
184 return standardAxisKey;
185 }
186 }
187
188 // Fall back to the `full` bundle which contains every axis.
189 return findAxisKey(publishedAxisKeys, 'full') ?? defaultKey;
190};
191
192/**
193 * Return the axis keys a caller actually wants to emit. Providing an empty list defaults to all

Callers 3

utils.test.tsFile · 0.90
buildVariablePreviewCSSFunction · 0.90
getVariableImportFunction · 0.90

Calls 3

getVariableAxisKeysFunction · 0.85
determineAxisKeyFunction · 0.85
findAxisKeyFunction · 0.85

Tested by

no test coverage detected