UNPKG

@wordpress/global-styles-ui

Version:
1 lines • 2.42 kB
{"version":3,"file":"filter-fonts.mjs","names":[],"sources":["../../../src/font-library/utils/filter-fonts.ts"],"sourcesContent":["import type { CollectionFontFamily } from '@wordpress/core-data';\n\n/**\n * Filters a list of fonts based on the specified filters.\n *\n * This function filters a given array of fonts based on the criteria provided in the filters object.\n * It supports filtering by category and a search term. If the category is provided and not equal to 'all',\n * the function filters the fonts array to include only those fonts that belong to the specified category.\n * Additionally, if a search term is provided, it filters the fonts array to include only those fonts\n * whose name includes the search term, case-insensitively.\n *\n * @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.\n * @param filters Object containing the filter criteria. It should have a 'category' key and/or a 'search' key.\n * @param filters.category The category to filter fonts by. If 'all', no category filtering is applied.\n * @param filters.search The search term to filter fonts by. If provided, only fonts whose\n *\n * @return Array of filtered font objects based on the provided criteria.\n */\nexport default function filterFonts(\n\tfonts: CollectionFontFamily[],\n\tfilters: { category?: string; search?: string }\n): CollectionFontFamily[] {\n\tconst { category, search } = filters;\n\tlet filteredFonts = fonts || [];\n\n\tif ( category && category !== 'all' ) {\n\t\tfilteredFonts = filteredFonts.filter(\n\t\t\t( font ) =>\n\t\t\t\tfont.categories && font.categories.indexOf( category ) !== -1\n\t\t);\n\t}\n\n\tif ( search ) {\n\t\tfilteredFonts = filteredFonts.filter(\n\t\t\t( font ) =>\n\t\t\t\tfont.font_family_settings &&\n\t\t\t\tfont.font_family_settings.name\n\t\t\t\t\t.toLowerCase()\n\t\t\t\t\t.includes( search.trim().toLowerCase() )\n\t\t);\n\t}\n\n\treturn filteredFonts;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAkBA,SAAwB,YACvB,OACA,SACyB;CACzB,MAAM,EAAE,UAAU,WAAW;CAC7B,IAAI,gBAAgB,SAAS,CAAC;CAE9B,IAAK,YAAY,aAAa,OAC7B,gBAAgB,cAAc,QAC3B,SACD,KAAK,cAAc,KAAK,WAAW,QAAS,QAAS,MAAM,EAC7D;CAGD,IAAK,QACJ,gBAAgB,cAAc,QAC3B,SACD,KAAK,wBACL,KAAK,qBAAqB,KACxB,YAAY,CAAC,CACb,SAAU,OAAO,KAAK,CAAC,CAAC,YAAY,CAAE,CAC1C;CAGD,OAAO;AACR"}