sort-array-includes
Enforce sorted array values if the includes
method is immediately called after the array is created.
By keeping arrays sorted, developers can quickly scan and verify the values, making the code more predictable and reducing the likelihood of errors. This practice simplifies debugging and enhances the overall clarity of the codebase.
Try it out
Options
This rule accepts an options object with the following properties:
type
default:'alphabetical'
Specifies the sorting method.
'alphabetical'
— Sort items alphabetically (e.g., “a” < “b” < “c”) using localeCompare.'natural'
— Sort items in a natural 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 thealphabet
option.'unsorted'
— Do not sort items. To be used with theuseConfigurationIf
option.
order
default:'asc'
Determines whether the sorted items should be in ascending or descending order.
'asc'
— Sort items in ascending order (A to Z, 1 to 9).'desc'
— Sort items in descending order (Z to A, 9 to 1).
alphabet
default:''
Only used when the type
option is set to 'custom'
. Specifies the custom alphabet to use when sorting.
Use the Alphabet
utility class from eslint-plugin-perfectionist/alphabet
to quickly generate a custom alphabet.
Example: 0123456789abcdef...
ignoreCase
default:true
Controls whether sorting should be case-sensitive or not.
true
— Ignore case when sorting alphabetically or naturally (e.g., “A” and “a” are the same).false
— Consider case when sorting (e.g., “a” comes before “A”).
specialCharacters
default:keep
Controls whether special characters should be trimmed, removed or kept before sorting.
'keep'
— Keep special characters when sorting (e.g., “_a” comes before “a”).'trim'
— Trim special characters when sorting alphabetically or naturally (e.g., “_a” and “a” are the same).'remove'
— Remove special characters when sorting (e.g., “/a/b” and “ab” are the same).
locales
default:'en-US'
Specifies the sorting locales. See String.prototype.localeCompare() - locales.
string
— A BCP 47 language tag (e.g.'en'
,'en-US'
,'zh-CN'
).string[]
— An array of BCP 47 language tags.
[DEPRECATED] groupKind
default:'literals-first'
Use the groups option with the literal
and spread
selectors instead. Make sure to set this option to mixed
.
Allows you to group array elements by their kind, determining whether spread values should come before or after literal values.
mixed
— Do not group array elements by their kind; spread values are sorted together with literal values.literals-first
— Group all literal values before spread values.spreads-first
— Group all spread values before literal values.
partitionByComment
default:false
Allows you to use comments to separate the members of arrays into logical groups. This can help in organizing and maintaining large arrays by creating partitions based on comments.
true
— All comments will be treated as delimiters, creating partitions.false
— Comments will not be used as delimiters.string
— A regexp pattern to specify which comments should act as delimiters.string[]
— A list of regexp patterns to specify which comments should act as delimiters.
partitionByNewLine
default:false
When true
, the rule will not sort the members of an array if there is an empty line between them. This can be useful for keeping logically separated groups of members in their defined order.
if ([
// Group 1
'Drone',
'Keyboard',
'Mouse',
'Smartphone',
// Group 2
'Laptop',
'Monitor',
'Smartwatch',
'Tablet',
// Group 3
'Headphones',
'Router',
].includes(product.name)) {
return 'Electronics'
}
Each group of elements (separated by empty lines) is treated independently, and the order within each group is preserved.
useConfigurationIf
type: { allNamesMatchPattern?: string }
{}
Allows you to specify filters to match a particular options configuration for a given object.
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.
Example configuration:
{
'perfectionist/sort-array-includes': [
'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
type: Array<string | string[]>
[]
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.
Predefined groups are characterized by a selector.
List of selectors
literal
: Array elements that are not spread values.spread
: Array elements that are spread values.
customGroups
type: Array<CustomGroupDefinition | CustomGroupAnyOfDefinition>
{}
You can define your own groups and use regexp pattern to match specific object type members.
A custom group definition may follow one of the two following interfaces:
interface CustomGroupDefinition {
groupName: string
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
order?: 'asc' | 'desc'
selector?: string
elementNamePattern?: string
}
An array element will match a CustomGroupDefinition
group if it matches all the filters of the custom group’s definition.
or:
interface CustomGroupAnyOfDefinition {
groupName: string
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
order?: 'asc' | 'desc'
anyOf: Array<{
selector?: string
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 thegroups
option.selector
: Filter on theselector
of the element.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
Version
This rule was introduced in v0.5.0.