-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcategories.ts
More file actions
90 lines (84 loc) · 2.36 KB
/
categories.ts
File metadata and controls
90 lines (84 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import type { CategoryConfig, Group, PluginConfig } from '@code-pushup/models';
import {
type PluginUrlContext,
createCategoryRefs,
expandCategoryRefs,
removeIndex,
shouldExpandForUrls,
validateUrlContext,
} from '@code-pushup/utils';
import { AXE_PLUGIN_SLUG } from './constants.js';
import { type AxeCategoryGroupSlug, isAxeGroupSlug } from './groups.js';
/**
* Creates categories for the Axe plugin.
*
* @public
* @param plugin - {@link PluginConfig} object with groups and context
* @param categories - {@link CategoryConfig} optional user-defined categories
* @returns {CategoryConfig[]} - expanded and aggregated categories
*
* @example
* const axe = await axePlugin(urls);
* const axeCoreConfig = {
* plugins: [axe],
* categories: axeCategories(axe),
* };
*/
export function axeCategories(
plugin: Pick<PluginConfig, 'groups' | 'context'>,
categories?: CategoryConfig[],
): CategoryConfig[] {
if (!plugin.groups || plugin.groups.length === 0) {
return categories ?? [];
}
validateUrlContext(plugin.context);
if (!categories) {
return createCategories(plugin.groups, plugin.context);
}
return expandCategories(categories, plugin.context);
}
function createCategories(
groups: Group[],
context: PluginUrlContext,
): CategoryConfig[] {
return [createAggregatedCategory(groups, context)];
}
function expandCategories(
categories: CategoryConfig[],
context: PluginUrlContext,
): CategoryConfig[] {
if (!shouldExpandForUrls(context.urlCount)) {
return categories;
}
return categories.map(category =>
expandAggregatedCategory(category, context),
);
}
export function createAggregatedCategory(
groups: Group[],
context: PluginUrlContext,
): CategoryConfig {
const refs = extractGroupSlugs(groups).flatMap(slug =>
createCategoryRefs(slug, AXE_PLUGIN_SLUG, context),
);
return {
slug: 'axe-a11y',
title: 'Axe Accessibility',
refs,
};
}
export function expandAggregatedCategory(
category: CategoryConfig,
context: PluginUrlContext,
): CategoryConfig {
return {
...category,
refs: category.refs.flatMap(ref =>
ref.plugin === AXE_PLUGIN_SLUG ? expandCategoryRefs(ref, context) : [ref],
),
};
}
export function extractGroupSlugs(groups: Group[]): AxeCategoryGroupSlug[] {
const slugs = groups.map(({ slug }) => removeIndex(slug));
return [...new Set(slugs)].filter(isAxeGroupSlug);
}