Skip to content

Commit

Permalink
feat(sort-maps): add groups, custom groups and new lines between options
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Jan 18, 2025
1 parent a6e1daf commit 317baaa
Show file tree
Hide file tree
Showing 6 changed files with 1,025 additions and 66 deletions.
4 changes: 2 additions & 2 deletions docs/content/rules/sort-array-includes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ Each group of elements (separated by empty lines) is treated independently, and
</sub>
<sub>default: `{}`</sub>

Allows you to specify filters to match a particular options configuration for a given object.
Allows you to specify filters to match a particular options configuration for a given array.

The first matching options configuration will be used. If no configuration matches, the default options configuration will be used.

- `allNamesMatchPattern` — A regexp pattern that all object keys must match.
- `allNamesMatchPattern` — A regexp pattern that all array keys must match.

Example configuration:
```ts
Expand Down
134 changes: 134 additions & 0 deletions docs/content/rules/sort-maps.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Specifies the sorting method.
- `'natural'` — Sort items in a [natural](https://github.com/yobacca/natural-orderby) order (e.g., “item2” < “item10”).
- `'line-length'` — Sort items by the length of the code line (shorter lines first).
- `'custom'` — Sort items using the alphabet entered in the [`alphabet`](#alphabet) option.
- `'unsorted'` — Do not sort items. To be used with the [`useConfigurationIf`](#useconfigurationif) option.

### order

Expand Down Expand Up @@ -177,6 +178,131 @@ new Map([

Each group of map members (separated by empty lines) is treated independently, and the order within each group is preserved.

### newlinesBetween

<sub>default: `'ignore'`</sub>

Specifies how new lines should be handled between map members.

- `ignore` — Do not report errors related to new lines between map members.
- `always` — Enforce one new line between each group, and forbid new lines inside a group.
- `never` — No new lines are allowed in maps.

This option is only applicable when `partitionByNewLine` is `false`.

### useConfigurationIf

<sub>
type: `{ allNamesMatchPattern?: string }`
</sub>
<sub>default: `{}`</sub>

Allows you to specify filters to match a particular options configuration for a given map.

The first matching options configuration will be used. If no configuration matches, the default options configuration will be used.

- `allNamesMatchPattern` — A regexp pattern that all map keys must match.

Example configuration:
```ts
{
'perfectionist/sort-maps': [
'error',
{
groups: ['r', 'g', 'b'], // Sort colors by RGB
customGroups: [
{
elementNamePattern: '^r$',
groupName: 'r',
},
{
elementNamePattern: '^g$',
groupName: 'g',
},
{
elementNamePattern: '^b$',
groupName: 'b',
},
],
useConfigurationIf: {
allNamesMatchPattern: '^r|g|b$',
},
},
{
type: 'alphabetical' // Fallback configuration
}
],
}
```

### groups

<sub>
type: `Array<string | string[]>`
</sub>
<sub>default: `[]`</sub>

Allows you to specify a list of groups for sorting. Groups help organize elements into categories.

Each element will be assigned a single group specified in the `groups` option (or the `unknown` group if no match is found).
The order of items in the `groups` option determines how groups are ordered.

Within a given group, members will be sorted according to the `type`, `order`, `ignoreCase`, etc. options.

Individual groups can be combined together by placing them in an array. The order of groups in that array does not matter.
All members of the groups in the array will be sorted together as if they were part of a single group.

### customGroups

<sub>
type: `Array<CustomGroupDefinition | CustomGroupAnyOfDefinition>`
</sub>
<sub>default: `{}`</sub>

You can define your own groups and use regexp patterns to match specific elements.

A custom group definition may follow one of the two following interfaces:

```ts
interface CustomGroupDefinition {
groupName: string
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
order?: 'asc' | 'desc'
elementNamePattern?: string
}

```
An array element will match a `CustomGroupDefinition` group if it matches all the filters of the custom group's definition.

or:

```ts
interface CustomGroupAnyOfDefinition {
groupName: string
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
order?: 'asc' | 'desc'
anyOf: Array<{
elementNamePattern?: string
}>
}
```

An array element will match a `CustomGroupAnyOfDefinition` group if it matches all the filters of at least one of the `anyOf` items.

#### Attributes

- `groupName`: The group's name, which needs to be put in the `groups` option.
- `elementNamePattern`: If entered, will check that the name of the element matches the pattern entered.
- `type`: Overrides the sort type for that custom group. `unsorted` will not sort the group.
- `order`: Overrides the sort order for that custom group

#### Match importance

The `customGroups` list is ordered:
The first custom group definition that matches an element will be used.

Custom groups have a higher priority than any predefined group.

## Usage

<CodeTabs
Expand All @@ -201,6 +327,10 @@ Each group of map members (separated by empty lines) is treated independently, a
specialCharacters: 'keep',
partitionByNewLine: false,
partitionByComment: false,
newlinesBetween: false,
useConfigurationIf: {},
groups: [],
customGroups: [],
},
],
},
Expand All @@ -227,6 +357,10 @@ Each group of map members (separated by empty lines) is treated independently, a
specialCharacters: 'keep',
partitionByNewLine: false,
partitionByComment: false,
newlinesBetween: false,
useConfigurationIf: {},
groups: [],
customGroups: [],
},
],
},
Expand Down
Loading

0 comments on commit 317baaa

Please sign in to comment.