@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
25 lines (23 loc) • 743 B
text/typescript
import type { FontFamily, FontFace } from '@wordpress/core-data';
import { kebabCase } from '@wordpress/kebab-case';
export default function makeFamiliesFromFaces(
fontFaces: FontFace[]
): FontFamily[] {
const fontFamiliesObject = fontFaces.reduce(
( acc: Record< string, FontFamily >, item: FontFace ) => {
if ( ! acc[ item.fontFamily ] ) {
acc[ item.fontFamily ] = {
name: item.fontFamily,
fontFamily: item.fontFamily,
slug: kebabCase( item.fontFamily.toLowerCase() ),
fontFace: [],
};
}
// @ts-expect-error `acc[ item.fontFamily ]` is possibly `undefined`.
acc[ item.fontFamily ].fontFace.push( item );
return acc;
},
{}
);
return Object.values( fontFamiliesObject ) as FontFamily[];
}