* Parse a typography properties object into a ResolvedTypography.
(props: Record<string, string | number>, path: string, findings: Finding[])
| 298 | * Parse a typography properties object into a ResolvedTypography. |
| 299 | */ |
| 300 | function parseTypography(props: Record<string, string | number>, path: string, findings: Finding[]): ResolvedTypography { |
| 301 | const result: ResolvedTypography = { type: 'typography' }; |
| 302 | |
| 303 | if (typeof props['fontFamily'] === 'string') { |
| 304 | const ff = props['fontFamily']; |
| 305 | if (isValidColor(ff)) { |
| 306 | findings.push({ |
| 307 | severity: 'error', |
| 308 | path: `${path}.fontFamily`, |
| 309 | message: `'${ff}' appears to be a color, not a valid font family.`, |
| 310 | }); |
| 311 | } |
| 312 | result.fontFamily = ff; |
| 313 | } |
| 314 | if (props['fontWeight'] !== undefined) { |
| 315 | const fw = props['fontWeight']; |
| 316 | let fwValue: number | undefined; |
| 317 | |
| 318 | if (typeof fw === 'number') { |
| 319 | fwValue = fw; |
| 320 | } else if (typeof fw === 'string') { |
| 321 | const parsed = Number(fw); |
| 322 | if (!isNaN(parsed)) { |
| 323 | fwValue = parsed; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (fwValue === undefined) { |
| 328 | findings.push({ |
| 329 | severity: 'error', |
| 330 | path: `${path}.fontWeight`, |
| 331 | message: `'${fw}' is not a valid font weight. Expected a number.`, |
| 332 | }); |
| 333 | } else { |
| 334 | result.fontWeight = fwValue; |
| 335 | } |
| 336 | } |
| 337 | if (typeof props['fontFeature'] === 'string') result.fontFeature = props['fontFeature']; |
| 338 | if (typeof props['fontVariation'] === 'string') result.fontVariation = props['fontVariation']; |
| 339 | |
| 340 | const dimensionProps = ['fontSize', 'lineHeight', 'letterSpacing'] as const; |
| 341 | for (const prop of dimensionProps) { |
| 342 | const raw = props[prop]; |
| 343 | if (typeof raw === 'string') { |
| 344 | if (isParseableDimension(raw)) { |
| 345 | const parsed = parseDimension(raw); |
| 346 | if (parsed.unit !== 'px' && parsed.unit !== 'rem' && parsed.unit !== 'em') { |
| 347 | findings.push({ |
| 348 | severity: 'error', |
| 349 | path: `${path}.${prop}`, |
| 350 | message: `'${raw}' has an invalid unit '${parsed.unit}'. Only px, rem, and em are allowed.`, |
| 351 | }); |
| 352 | } |
| 353 | result[prop] = parsed; |
| 354 | } else if (prop === 'lineHeight' && /^\d*\.?\d+$/.test(raw)) { |
| 355 | result[prop] = { |
| 356 | type: 'dimension', |
| 357 | value: parseFloat(raw), |
no test coverage detected
searching dependent graphs…