npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eslint-plugin-sort-keys-plus

v1.4.0

Published

Fork of eslint rule that sorts keys in objects (https://eslint.org/docs/rules/sort-keys) with extra features

Downloads

27,580

Readme

eslint-plugin-sort-keys-plus

Fork of eslint rule that sorts keys in objects (https://eslint.org/docs/rules/sort-keys) with autofix enabled

Installation

You'll first need to install ESLint:

$ npm i eslint --save-dev

Next, install eslint-plugin-sort-keys-plus:

$ npm install eslint-plugin-sort-keys-plus --save-dev

Note: If you installed ESLint globally (using the -g flag) then you must also install eslint-plugin-sort-keys-plus globally.

Usage

Add sort-keys-plus to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
  "plugins": [
    "sort-keys-plus"
  ]
}

Then add sort-keys-plus rule under the rules section.

{
    "rules": {
        "sort-keys-plus/sort-keys": "warn"
    }
}

Often it makes sense to enable sort-keys-plus only for certain files/directories. For cases like that, use override key of eslint config:

{
  "rules": {
    // ...
  },
  "overrides": [
    {
      "files": ["src/alphabetical.js", "bin/*.js", "lib/*.js"],
      "rules": {
        "sort-keys-plus/sort-keys-plus": "warn"
      }
    }
  ]
}

In some cases, there should be a specific order for some properties of an object, such as with mongodb aggregations. For that, use the override key of the configuration:

{
  "rules": {
    "sort-keys-plus/sort-keys": ["warn", "asc", {
      "overrides": [
        {
          "properties": ["$lookup"],
          "order": ["from", "localField", "foreignField", "as"]
        }
      ]
    }]
  }
}

Rule configuration

For available config options, see official sort-keys reference. All options supported by sort-keys are supported by sort-keys-plus.

{
  "sort-keys-shorthand/sort-keys-shorthand": [
    "error",
    "asc",
    {
      "caseSensitive": true,
      "minKeys": 2,
      "natural": false,
      "ignoreSingleLine": false,
      "allCaps": "ignore",
      "shorthand": "ignore",
      "overrides": [],
    }
  ]
}

Additional properties that can be set in the 2nd option object supported are as follows:

  • ignoreSingleline - if true, this rule is ignored on single line objects. Default is false.
  • allCaps handling for ALL_CAPS properties
    • ignore no rules for all caps
    • first all caps properties must be first
    • last all caps properties must be last
  • shorthand handling for shorthand properties
    • ignore no rules for shorthands
    • first shorthand properties must be first
    • last shrothand properties must be last

shorthand is checked after allCaps, so ALL_CAPS will be before shorthand when both are 'first'.

  • overrides allows custom orders for specific sets of keys, or sub-objects with a specific parent key. See below for configuration.

ignoreSingleLine

Examples of incorrect code for the {ignoreSingleLine: true} option:

/*eslint sort-keys-plus/sort-keys: ["error", "asc", {ignoreSingleLine: true}]*/
/*eslint-env es6*/
let obj = {
  e: 1,
  c: 3,
  C: 4,
  b: 2
};

Examples of correct code for the {ignoreSingleLine: true} option:

/*eslint sort-keys-plus/sort-keys: ["error", "asc", {ignoreSingleLine: true}]*/
/*eslint-env es6*/
let obj = { e: 1, b: 2, c: 3, C: 4 };

allCaps

Examples of incorrect code for the {allCaps: 'first'} option:

/*eslint sort-keys-plus/sort-keys: ["error", "asc", {allCaps: 'first'}]*/
/*eslint-env es6*/
let obj = {
  a: 1,
  B_CONSTANT: 2, // not sorted correctly (should be 1st key)
  c: 3,
  d: 4
};

Examples of correct code for the {allCaps: 'first'} option:

/*eslint sort-keys-plus/sort-keys: ["error", "asc", {allCaps: 'first'}]*//
/*eslint-env es6*/
let obj = {
    B_CONSTANT: 2,
    a: 1,
    c: 3,
};

shorthand

Examples of incorrect code for the {shorthand: 'first'} option:

/*eslint sort-keys-plus/sort-keys: ["error", "asc", {shorthand: 'first'}]*/
/*eslint-env es6*/
const b = 2;
let obj = {
  a: 1,
  b, // not sorted correctly (should be 1st key)
  c: 3,
  d: 4
};

Examples of correct code for the {shorthand: 'first'} option:

/*eslint sort-keys-plus/sort-keys: ["error", "asc", {shorthand: 'first'}]*//
/*eslint-env es6*/
const b = 2;
let obj = {
    b,
    a: 1,
    c: 3,
};

overrides

Configuration:

      "overrides": [
        {
          "order": ["title", "description"],
          "message": "`title` should be before `description`",
        },
        {
          "
        }
      ],
  • order define the property keys that should be ordered
  • properties define parent property key for this rule. If this is not provided, the override will apply to all objects with a subset of the keys in order
  • message optional custom message when the rule is violated

Examples of incorrect code for the {overrides: [{order: ['b', 'a', 'd']}]} option:

/*eslint sort-keys-plus/sort-keys: ["error", "asc", {overrides: [{order: ['b', 'a', 'd']}]}]*/
/*eslint-env es6*/
let obj = {
  a: 1,
  b: 2, // not sorted correctly (should be 1st key)
  d: 4
};

Examples of correct code for the {overrides: [{order: ['b', 'a', 'd']}]} option:

/*eslint sort-keys-plus/sort-keys: ["error", "asc", {overrides: [{order: ['b', 'a', 'd']}]}]*/
/*eslint-env es6*/
let obj = {
  b: 2,
  a: 1,
  d: 4
};
// has additional properties
let obj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};

Examples of incorrect code for the {overrides: [{properties: ['a'], order: ['b', 'a', 'd']}]} option:

/*eslint sort-keys-plus/sort-keys: ["error", "asc", {overrides: [{properties: ['a'], order: ['b', 'a', 'd']}]}]*/
/*eslint-env es6*/
let obj = { a:
  {
    a: 1,
    b: 2, // not sorted correctly (should be 1st key)
    c: 3,
    d: 4  // not sorted correctly (should be 3rd key)
  };

Examples of correct code for the {overrides: [{overrides: [{properties: ['a'], order: ['b', 'a', 'd']}]} option:

/*eslint sort-keys-plus/sort-keys: ["error", "asc", {overrides: [{overrides: [{properties: ['a'], order: ['b', 'a', 'd']}]}*/
/*eslint-env es6*/
let obj = {
  b: 2,
  a: 1,
  d: 4,
  c: 3
};