{"_id":"prettier-plugin-sort-json","_rev":"2795967","name":"prettier-plugin-sort-json","description":"Prettier plugin to sort JSON files alphanumerically by key","dist-tags":{"latest":"0.0.2"},"maintainers":[{"name":"gudahtt","email":"markjstacey@gmail.com"}],"time":{"modified":"2022-07-12T09:44:25.000Z","created":"2020-09-23T01:14:45.006Z","0.0.2":"2021-02-13T18:43:39.057Z","0.0.1":"2020-09-23T01:14:45.006Z"},"users":{},"author":{"name":"Mark Stacey","email":"markjstacey@gmail.com"},"repository":{"type":"git","url":"git+https://github.com/Gudahtt/prettier-plugin-sort-json.git"},"versions":{"0.0.2":{"name":"prettier-plugin-sort-json","version":"0.0.2","description":"Prettier plugin to sort JSON files alphanumerically by key","main":"index.js","repository":{"type":"git","url":"git+https://github.com/Gudahtt/prettier-plugin-sort-json.git"},"author":{"name":"Mark Stacey","email":"markjstacey@gmail.com"},"scripts":{"build":"tsc --project .","prepublishOnly":"yarn build","test":"jest","lint":"eslint . --ext .ts,.js"},"license":"MIT","private":false,"devDependencies":{"@metamask/eslint-config":"^5.0.0","@types/jest":"^26.0.13","@typescript-eslint/eslint-plugin":"^4.15.0","@typescript-eslint/parser":"^4.15.0","eslint":"^7.20.0","eslint-plugin-import":"^2.22.1","eslint-plugin-jest":"^24.1.3","eslint-plugin-node":"^11.1.0","prettier":"^2.2.1","jest":"^26.4.2","ts-jest":"^26.3.0","typescript":"^4.1.5"},"dependencies":{"@types/prettier":"^2.2.0"},"peerDependencies":{"prettier":"^2.1.0"},"gitHead":"db85cefd1ced407d1cf197d855cfb23e6e0a82ec","bugs":{"url":"https://github.com/Gudahtt/prettier-plugin-sort-json/issues"},"homepage":"https://github.com/Gudahtt/prettier-plugin-sort-json#readme","_id":"prettier-plugin-sort-json@0.0.2","_nodeVersion":"10.20.1","_npmVersion":"6.14.4","dist":{"shasum":"e2aa58829d1b895ed8d9e762860b3e3b84607245","size":3871,"noattachment":false,"key":"/prettier-plugin-sort-json/-/prettier-plugin-sort-json-0.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/prettier-plugin-sort-json/download/prettier-plugin-sort-json-0.0.2.tgz"},"_npmUser":{"name":"gudahtt","email":"markjstacey@gmail.com"},"directories":{},"maintainers":[{"name":"gudahtt","email":"markjstacey@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/prettier-plugin-sort-json_0.0.2_1613241818945_0.036164910678417206"},"_hasShrinkwrap":false,"publish_time":1613241819057,"_cnpm_publish_time":1613241819057,"_cnpmcore_publish_time":"2021-12-16T18:35:42.199Z"},"0.0.1":{"name":"prettier-plugin-sort-json","version":"0.0.1","description":"Prettier plugin to sort JSON files alphanumerically by key","main":"index.js","repository":{"type":"git","url":"git+https://github.com/Gudahtt/prettier-plugin-sorted-json.git"},"author":{"name":"Mark Stacey","email":"markjstacey@gmail.com"},"scripts":{"build":"tsc --project .","prepublishOnly":"yarn build","test":"jest","lint":"eslint . --ext .ts,.js"},"license":"MIT","private":false,"devDependencies":{"@metamask/eslint-config":"^3.2.0","@types/jest":"^26.0.13","@typescript-eslint/eslint-plugin":"^2.20.0","@typescript-eslint/parser":"^2.17.0","eslint":"^6.8.0","eslint-plugin-import":"^2.20.1","eslint-plugin-jest":"^23.6.0","jest":"^26.4.2","ts-jest":"^26.3.0","typescript":"^4.0.2"},"dependencies":{"@babel/types":"^7.11.5","@types/prettier":"^2.1.0","prettier":"^2.1.1"},"gitHead":"ae91cee6a7e553842d419cce5d487c4fc6800c20","bugs":{"url":"https://github.com/Gudahtt/prettier-plugin-sorted-json/issues"},"homepage":"https://github.com/Gudahtt/prettier-plugin-sorted-json#readme","_id":"prettier-plugin-sort-json@0.0.1","_nodeVersion":"10.20.1","_npmVersion":"6.14.4","dist":{"shasum":"fe4525200745da6327d0d174b652ce41969fbe0a","size":2668,"noattachment":false,"key":"/prettier-plugin-sort-json/-/prettier-plugin-sort-json-0.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/prettier-plugin-sort-json/download/prettier-plugin-sort-json-0.0.1.tgz"},"maintainers":[{"name":"gudahtt","email":"markjstacey@gmail.com"}],"_npmUser":{"name":"gudahtt","email":"markjstacey@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/prettier-plugin-sort-json_0.0.1_1600823684847_0.7636913243105421"},"_hasShrinkwrap":false,"publish_time":1600823685006,"_cnpm_publish_time":1600823685006,"_cnpmcore_publish_time":"2021-12-16T18:35:42.422Z"}},"readme":"# prettier-plugin-sort-json\n\nA plugin for [Prettier](https://prettier.io) that sorts JSON files by property name.\n\n## Description\n\nThis plugin adds a JSON preprocessor that will sort JSON files alphanumerically by key.\n\nBy default, top-level object entries are sorted by key lexically using [`Array.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort), according to each character's Unicode code point value. It can be [configured](#configuration) to sort recursively, and with a custom sort order.\n\n### Example\n\nBefore:\n\n```json\n{\n  \"z\": null,\n  \"a\": null,\n  \"0\": null,\n  \"exampleNestedObject\": {\n    \"z\": null,\n    \"a\": null\n  }\n}\n```\n\nAfter:\n\n```json\n{\n  \"0\": null,\n  \"a\": null,\n  \"exampleNestedObject\": {\n    \"z\": null,\n    \"a\": null\n  },\n  \"z\": null\n}\n```\n\n### Exceptions\n\n- Non-objects\n\n  This is meant to sort objects. JSON files with a top-level value that is not an object are skipped.\n\n- JSON files with dedicated Prettier parsers\n\n  This will not sort `package.json`, `package-lock.json`, or `composer.json`. This plugin only affects the `json` parser used by Prettier. Prettier uses an alternate parser (`json-stringify`) for those three specific files ([See here for details](https://github.com/prettier/prettier/blob/9a8b579d368db99394ab9da114cc37ba772fc887/src/language-js/index.js#L80)).\n\n- JSON embedded in other files\n\n  This will not sort JSON objects within other types of files, such as JavaScript or TypeScript files. This is just for sorting JSON files.\n\n## Requirements\n\nThis module requires an [LTS](https://github.com/nodejs/Release) Node version (v16.0.0+), and `prettier` v3+.\n\nWe are maintaining support for Prettier v2 on version 2 of this plugin. See [the main-v2 branch](https://github.com/Gudahtt/prettier-plugin-sort-json/tree/main-v2) for instructions on using v2 of this plugin.\n\n## Install\n\nUsing `npm`:\n\n```console\nnpm install --save-dev prettier-plugin-sort-json\n```\n\nUsing `pnpm`:\n\n```console\npnpm add --save-dev prettier-plugin-sort-json\n```\n\nUsing `yarn`:\n\n```console\nyarn add --dev prettier-plugin-sort-json\n```\n\nThen [follow these instructions](https://prettier.io/docs/en/plugins#using-plugins) to load the plugin.\n\nThere are some additional configuration options available ([described below](#configuration)), but they are all optional.\n\n### Example Prettier configuration\n\n```json\n{\n  \"plugins\": [\"prettier-plugin-sort-json\"]\n}\n```\n\n## Configuration\n\nThese configuration options are all optional. Each option can be set as a CLI flag, or as an entry in your Prettier configuraton (e.g. in your `.prettierrc` file).\n\nHere are example Prettier configuration files with all default options set:\n\n- `.prettierrc.json`:\n\n  ```json\n  {\n    \"plugins\": [\"prettier-plugin-sort-json\"],\n    \"jsonRecursiveSort\": false,\n    \"jsonSortOrder\": \"{\\\"/.*/\\\": \\\"lexical\\\"}\"\n  }\n  ```\n\n- `.prettierrc.js`:\n\n  ```javascript\n  module.exports = {\n    plugins: ['prettier-plugin-sort-json'],\n    jsonRecursiveSort: false,\n    jsonSortOrder: JSON.stringify({ [/.*/]: 'lexical' }),\n  };\n  ```\n\n### JSON Recursive Sort\n\nSort JSON objects recursively, including all nested objects. This also sorts objects within JSON arrays.\n\n| Default | CLI                     | Configuration               |\n| ------- | ----------------------- | --------------------------- |\n| `false` | `--json-recursive-sort` | `jsonRecursiveSort: <bool>` |\n\n### JSON Sort Order\n\nUse a custom sort order. This is specified as a JSON string containing a set of sorting rules.\n\n| Default | CLI                            | Configuration             |\n| ------- | ------------------------------ | ------------------------- |\n| `\"\"`    | `--json-sort-order '<string>'` | `jsonSortOrder: <string>` |\n\nThis JSON string is an _ordered_ set of sorting rules. Each entry is one sorting rule, where they key of the sorting rule defines a _group_ of keys in the object being sorted, and the value of the sorting rule defines the sorting algorithm to use _within_ that group of keys. That is, a rule is structured like this: `[definition of sorting group]: [sorting algorithm within that group]`\n\nEach sorting group is defined either by an exact string, or by a regular expression. For example, the group `A` would include just the key `A`, and the group `/.*/` would include all keys.\n\n> [!NOTE]\n> Regular expression sorting groups **must** start with `/` and end with `/`, optionally followed by any supported regular expression flag (supported flags are `i`, `m`, `s`, and `u`, see [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#advanced_searching_with_flags) for details on how they work).\n\nThe order of the rules determines the order of the groups; for example, keys in the first group are first, those in the second group come next, etc. Keys that would qualify for multiple sorting groups are always placed in the first. Keys that are not in any sorting group are treated as being in an implied last group, with the default `lexical` sorting algorithm.\n\n#### Example\n\nHere is an example of a custom sort order string:\n\n```yaml\n'{ \"placeThisFirst\": null, \"/^\\\\\\\\d+/\": \"numeric\", \"/.*/\": \"caseInsensitiveLexical\" }'\n```\n\nThis string has three rules. Here is what they each mean:\n\n1. `\"placeThisFirst\": null`\n\n   The group is `placeThisFirst`, which is not a regular expression (no leading or trailing forward slash), so it's interpreted as an exact string. This is the first rule, so the key `placeThisFirst` will be first in the sort order.\n\n   No sorting algorithm is specified in this rule (the value is `null`) so the default `lexical` sort _would_ be used to sort keys in this group. Except in this case the sorting algorithm is irrelevant because only one key can be in this group.\n\n2. `\"/^\\\\\\\\d+/\": \"numeric\"`\n\n   The group in this case is a regular expression that matches keys that start with a number. This is the second rule, so keys starting with a number will come second after `placeThisFirst`.\n\n   The regular expression is double-escaped because it's a string within a stringified JSON object; the real regular expression this represents is `/^\\d+/` (see [Escaping](#escaping) for more details).\n\n   The sorting algorithm is `numeric`, so these keys will be sorted numerically (in ascending order).\n\n3. `\"/.*/\": \"caseInsensitiveLexical\"`\n\n   The group `/.*/` matches all other keys. These keys will be sorted third, after `placeThisFirst` and after keys starting with a number.\n\n   The sorting algorithm here is `caseInsensitiveLexical`, so keys in this group will be sorted lexically ignoring case.\n\nHere are some example JSON objects that would match these rules, with inline comments to explain:\n\n<details>\n<summary>Example Sorted JSON file #1:</summary>\n\n```jsonc\n{\n  // `placeThisFirst` is the first sorting rule, so this always comes first.\n  \"placeThisFirst\": null,\n\n  // These match the second sorting rule.\n  // These keys start with a number, so they're sorted numerically (ascending).\n  \"1\": null,\n  // The `numeric` sort order only looks at the numeric prefix.\n  // The rest of the key is ignored.\n  \"2_____irrelevant_text\": null,\n  \"10\": null,\n\n  // Everything is matched by the third sorting rule.\n  \"a\": null,\n  // This is in the middle because we're using `caseInsensitiveLexical`,\n  // rather than the default `lexical` sort order.\n  \"B\": null,\n  \"c\": null,\n}\n```\n\n</details>\n\n<br>\n\n<details>\n<summary> Example Sorted JSON File #2: </summary>\n\nThis example only has keys that match the second sorting rule.\n\n```jsonc\n{\n  // The keys are sorted in ascending order.\n  // Values are ignored by all sorting rules. Only keys are sorted.\n  \"1\": 10,\n  \"2\": 9,\n  \"3\": 8,\n}\n```\n\n</details>\n\n#### Sorting Algorithms\n\nEach `jsonSortOrder` _value_ represents the sorting algorithm to use _within_ that category. If the value is `null`, the default sorting algorithm `lexical` is used. Here are the supported sorting algorithms:\n\n| Sorting Algorithm               | Description                                                                                                 |\n| ------------------------------- | ----------------------------------------------------------------------------------------------------------- |\n| `lexical`                       | Sort lexically (i.e. lexicographically). This is the default.                                               |\n| `numeric`                       | For keys that are prefixed with a number, sort by that number in ascending order. Otherwise sort lexically. |\n| `reverseLexical`                | Reverse-order lexical sort.                                                                                 |\n| `reverseNumeric`                | Reverse-order numeric sort.                                                                                 |\n| `caseInsensitiveLexical`        | Case-insensitive lexical sort.                                                                              |\n| `caseInsensitiveNumeric`        | Case-insensitive numeric sort.                                                                              |\n| `caseInsensitiveReverseLexical` | Case-insensitive reverse-order lexical sort.                                                                |\n| `caseInsensitiveReverseNumeric` | Case-insensitive reverse-order numeric sort.                                                                |\n| `none`                          | Do not sort.                                                                                                |\n\n#### Escaping\n\nA stringified JSON object is a strange configuration format. This format was chosen to work around a limitation of Prettier plugins; object configuration values for plugins are not supported, but strings are, so we put an object in a string. This has downsides though, especially if the string includes special characters.\n\nSpecial characters (such as backslashes) in string literals need to be escaped by a backslash. This is commonplace in string representations of regular expressions, which often include the backslash special character. For example, try running `/^\\d+/.toString()` in a console, and you'll see it prints out `\"/^\\\\d+/\"`.\n\nWhen this string is included as the key of a stringified JSON object, all double-quotes and backslashes need to be escaped _again_. That's why a single backslash needed to be represented by _four_ backslashes in the earlier example. If this string is used in a JSON file (e.g. in `package.json` or `.prettierrc.json`) it gets even worse because the string itself needs to use double-quotes, requiring even more escaping of all double-quotes used inside the string:\n\n```yaml\n\"{ \\\"placeThisFirst\\\": null, \\\"/\\\\\\\\d+/\\\": \\\"numeric\\\", \\\"/.+/\\\": \\\"caseInsensitiveLexical\\\" }\"\n```\n\nIf you're using a JavaScript Prettier configuration file, all of this escaping can be avoided by using `JSON.stringify` to create the `jsonSortOrder` string from an object with RegExp literal keys. For example, you could create the earlier example JSON sort order string like this:\n\n```javascript\n{\n    jsonSortOrder: JSON.stringify({\n      placeThisFirst: null,\n      [/^\\d+/]: 'numeric',\n      [/.*/]: 'caseInsensitiveLexical',\n    }),\n}\n```\n\nThis makes the configuration much easier to read and write. This approach is strongly recommended, if the configuration format you're using allows it.\n\n## Ignoring files\n\nThis plugin can be used on specific files using [Prettier configuration overrides](https://prettier.io/docs/en/configuration#configuration-overrides). By configuring this plugin in an override, you can control which files it is applied to. Overrides can also allow using different configuration for different files (e.g. different sort order)\n\nFor example, lets say you had the following requirements:\n\n- No sorting of JSON by default\n- Shallow (non-recursive) sort JSON in the `json/` directory\n- Do not sort the file `json/unsorted.json`\n- Recursively sort `recursively-sorted.json`\n\nYou could do that with this `.prettierrc.json` file:\n\n```json\n{\n  \"overrides\": [\n    {\n      \"excludeFiles\": [\"./json/unsorted.json\"],\n      \"files\": [\"./json/**\"],\n      \"options\": {\n        \"plugins\": [\"prettier-plugin-sort-json\"]\n      }\n    },\n    {\n      \"files\": [\"./json/recursive-sorted.json\"],\n      \"options\": {\n        \"jsonRecursiveSort\": true\n      }\n    }\n  ]\n}\n```\n\n## Contributing\n\nSee [CONTRIBUTING.md](./CONTRIBUTING.md)\n","_attachments":{},"homepage":"https://github.com/Gudahtt/prettier-plugin-sort-json#readme","bugs":{"url":"https://github.com/Gudahtt/prettier-plugin-sort-json/issues"},"license":"MIT"}