@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
32 lines (31 loc) • 1.86 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// packages/global-styles-ui/src/font-library/utils/filter-fonts.ts
/**
* Filters a list of fonts based on the specified filters.
*
* This function filters a given array of fonts based on the criteria provided in the filters object.
* It supports filtering by category and a search term. If the category is provided and not equal to 'all',
* the function filters the fonts array to include only those fonts that belong to the specified category.
* Additionally, if a search term is provided, it filters the fonts array to include only those fonts
* whose name includes the search term, case-insensitively.
*
* @param fonts Array of font objects in font-collection schema fashion to be filtered. Each font object should have a 'categories' property and a 'font_family_settings' property with a 'name' key.
* @param filters Object containing the filter criteria. It should have a 'category' key and/or a 'search' key.
* @param filters.category The category to filter fonts by. If 'all', no category filtering is applied.
* @param filters.search The search term to filter fonts by. If provided, only fonts whose
*
* @return Array of filtered font objects based on the provided criteria.
*/
function filterFonts(fonts, filters) {
const { category, search } = filters;
let filteredFonts = fonts || [];
if (category && category !== "all") filteredFonts = filteredFonts.filter((font) => font.categories && font.categories.indexOf(category) !== -1);
if (search) filteredFonts = filteredFonts.filter((font) => font.font_family_settings && font.font_family_settings.name.toLowerCase().includes(search.trim().toLowerCase()));
return filteredFonts;
}
exports.default = filterFonts;
//# sourceMappingURL=filter-fonts.cjs.map