|
1 | | -import type { NewlinesBetweenOption } from '../types/common-options' |
| 1 | +import type { GroupsOptions } from '../types/common-options' |
2 | 2 |
|
| 3 | +import { validateNewlinesBetweenInsideGroups } from './validate-newlines-between-inside-groups' |
| 4 | +import { validateNoDuplicatedGroups } from './validate-no-duplicated-groups' |
3 | 5 | import { isNewlinesBetweenOption } from './is-newlines-between-option' |
4 | 6 |
|
5 | | -type Group = { newlinesBetween: NewlinesBetweenOption } | string[] | string |
| 7 | +interface ValidateGroupsConfigurationParameters { |
| 8 | + options: { |
| 9 | + groups: GroupsOptions<string> |
| 10 | + } |
| 11 | + allowedPredefinedGroups: string[] |
| 12 | + allowedCustomGroups: string[] |
| 13 | +} |
6 | 14 |
|
7 | 15 | /** |
8 | 16 | * Throws an error if one of the following conditions is met: |
9 | 17 | * - One or more groups specified in `groups` are not predefined nor specified |
10 | 18 | * in `customGroups` |
11 | 19 | * - A group is specified in `groups` more than once |
12 | | - * @param {Group[]} groups - The groups to validate. |
13 | | - * @param {string[]} allowedPredefinedGroups - An array of predefined group |
| 20 | + * @param {object} parameters - Parameters object. |
| 21 | + * @param {object} parameters.options - Options containing the groups to validate. |
| 22 | + * @param {string[]} parameters.allowedPredefinedGroups - An array of predefined |
| 23 | + * group names that are considered valid. |
| 24 | + * @param {string[]} parameters.allowedCustomGroups - An array of custom group |
14 | 25 | * names that are considered valid. |
15 | | - * @param {string[]} allowedCustomGroups - An array of custom group names that |
16 | | - * are considered valid. |
17 | | - * @throws Will throw an error if invalid or duplicated groups are found. |
| 26 | + * @throws Error Will throw an error if invalid or duplicated groups are found. |
18 | 27 | */ |
19 | | -export let validateGroupsConfiguration = ( |
20 | | - groups: Group[], |
21 | | - allowedPredefinedGroups: string[], |
22 | | - allowedCustomGroups: string[], |
23 | | -): void => { |
| 28 | +export let validateGroupsConfiguration = ({ |
| 29 | + allowedPredefinedGroups, |
| 30 | + allowedCustomGroups, |
| 31 | + options, |
| 32 | +}: ValidateGroupsConfigurationParameters): void => { |
24 | 33 | let allowedGroupsSet = new Set([ |
25 | 34 | ...allowedPredefinedGroups, |
26 | 35 | ...allowedCustomGroups, |
27 | 36 | ]) |
28 | 37 | let invalidGroups: string[] = [] |
29 | | - let isPreviousElementNewlinesBetween = false |
30 | | - for (let groupElement of groups) { |
| 38 | + |
| 39 | + for (let groupElement of options.groups) { |
31 | 40 | if (isNewlinesBetweenOption(groupElement)) { |
32 | | - // There should not be two consecutive `newlinesBetween` objects |
33 | | - if (isPreviousElementNewlinesBetween) { |
34 | | - throw new Error("Consecutive 'newlinesBetween' objects are not allowed") |
35 | | - } |
36 | | - isPreviousElementNewlinesBetween = true |
37 | | - } else { |
38 | | - isPreviousElementNewlinesBetween = false |
39 | | - let groupElements = Array.isArray(groupElement) |
40 | | - ? groupElement |
41 | | - : [groupElement] |
42 | | - for (let group of groupElements) { |
43 | | - if (!allowedGroupsSet.has(group)) { |
44 | | - invalidGroups.push(group) |
45 | | - } |
| 41 | + continue |
| 42 | + } |
| 43 | + let groupElements = Array.isArray(groupElement) |
| 44 | + ? groupElement |
| 45 | + : [groupElement] |
| 46 | + for (let group of groupElements) { |
| 47 | + if (!allowedGroupsSet.has(group)) { |
| 48 | + invalidGroups.push(group) |
46 | 49 | } |
47 | 50 | } |
48 | 51 | } |
49 | 52 | if (invalidGroups.length > 0) { |
50 | 53 | throw new Error(`Invalid group(s): ${invalidGroups.join(', ')}`) |
51 | 54 | } |
52 | | - validateNoDuplicatedGroups(groups) |
53 | | -} |
54 | | - |
55 | | -/** |
56 | | - * Throws an error if a group is specified more than once |
57 | | - * @param {Group[]} groups - The groups to check for duplicates. |
58 | | - * @throws Will throw an error if duplicated groups are found. |
59 | | - */ |
60 | | -export let validateNoDuplicatedGroups = (groups: Group[]): void => { |
61 | | - let flattenGroups = groups.flat() |
62 | | - let seenGroups = new Set<string>() |
63 | | - let duplicatedGroups = new Set<string>() |
64 | | - |
65 | | - for (let group of flattenGroups) { |
66 | | - if (isNewlinesBetweenOption(group)) { |
67 | | - continue |
68 | | - } |
69 | | - if (seenGroups.has(group)) { |
70 | | - duplicatedGroups.add(group) |
71 | | - } else { |
72 | | - seenGroups.add(group) |
73 | | - } |
74 | | - } |
75 | 55 |
|
76 | | - if (duplicatedGroups.size > 0) { |
77 | | - throw new Error(`Duplicated group(s): ${[...duplicatedGroups].join(', ')}`) |
78 | | - } |
| 56 | + validateNoDuplicatedGroups(options) |
| 57 | + validateNewlinesBetweenInsideGroups(options) |
79 | 58 | } |
0 commit comments