prettier-plugin-sort-json
Advanced tools
Comparing version 0.0.1 to 0.0.2
import { Parser } from 'prettier'; | ||
export declare const parsers: Record<string, Parser>; | ||
export declare const parsers: Record<string, Parser<any>>; | ||
export declare const options: { | ||
jsonRecursiveSort: { | ||
category: string; | ||
default: boolean; | ||
description: string; | ||
since: string; | ||
type: "boolean"; | ||
}; | ||
}; |
43
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parsers = void 0; | ||
exports.options = exports.parsers = void 0; | ||
const parser_babel_1 = require("prettier/parser-babel"); | ||
const isObject = (json) => json !== null && typeof json === 'object'; | ||
function sortObject(object, recursive) { | ||
if (Array.isArray(object) && recursive) { | ||
return object.map((entry) => { | ||
return sortObject(entry, recursive); | ||
}); | ||
} | ||
else if (object !== null && typeof object === 'object' && !Array.isArray(object)) { | ||
const sortedJson = {}; | ||
for (const key of Object.keys(object).sort()) { | ||
if (recursive && isObject(object[key])) { | ||
sortedJson[key] = sortObject(object[key], recursive); | ||
} | ||
else { | ||
sortedJson[key] = object[key]; | ||
} | ||
} | ||
return sortedJson; | ||
} | ||
return object; | ||
} | ||
exports.parsers = { | ||
@@ -20,13 +41,23 @@ 'json': Object.assign(Object.assign({}, parser_babel_1.parsers.json), { preprocess(text, options) { | ||
} | ||
const recursive = options.jsonRecursiveSort; | ||
// Only objects are intended to be sorted by this plugin | ||
if (json === null || typeof json !== 'object' || Array.isArray(json)) { | ||
if (json === null || typeof json !== 'object' || (Array.isArray(json) && !recursive)) { | ||
return text; | ||
} | ||
const sortedJson = {}; | ||
for (const key of Object.keys(json).sort()) { | ||
sortedJson[key] = json[key]; | ||
} | ||
const sortedJson = sortObject(json, recursive); | ||
return JSON.stringify(sortedJson, null, 2); | ||
} }), | ||
}; | ||
// I get a TypeScript error if I just set the type to 'boolean' | ||
// This fixes the error. I don't know why. | ||
const type = 'boolean'; | ||
exports.options = { | ||
jsonRecursiveSort: { | ||
category: 'json-sort', | ||
default: false, | ||
description: 'Sort JSON files recursively, including any nested properties', | ||
since: '0.0.2', | ||
type, | ||
}, | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "prettier-plugin-sort-json", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Prettier plugin to sort JSON files alphanumerically by key", | ||
"main": "index.js", | ||
"repository": "https://github.com/Gudahtt/prettier-plugin-sorted-json", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Gudahtt/prettier-plugin-sort-json.git" | ||
}, | ||
"author": "Mark Stacey <markjstacey@gmail.com>", | ||
@@ -22,18 +25,21 @@ "files": [ | ||
"devDependencies": { | ||
"@metamask/eslint-config": "^3.2.0", | ||
"@metamask/eslint-config": "^5.0.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", | ||
"@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.0.2" | ||
"typescript": "^4.1.5" | ||
}, | ||
"dependencies": { | ||
"@babel/types": "^7.11.5", | ||
"@types/prettier": "^2.1.0", | ||
"prettier": "^2.1.1" | ||
"@types/prettier": "^2.2.0" | ||
}, | ||
"peerDependencies": { | ||
"prettier": "^2.1.0" | ||
} | ||
} |
@@ -7,3 +7,3 @@ # prettier-plugin-sort-json | ||
This module requires an [LTS](https://github.com/nodejs/Release) Node version (v10.0.0+), and `prettier` v2.0.0+. | ||
This module requires an [LTS](https://github.com/nodejs/Release) Node version (v10.0.0+), and `prettier` v2.1.0+. | ||
@@ -28,4 +28,2 @@ ## Install | ||
Currently we only perform a shallow sort. Nested objects are not sorted. | ||
Object entries are sorted by key 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. | ||
@@ -61,4 +59,14 @@ | ||
}, | ||
"z": null, | ||
"z": null | ||
} | ||
``` | ||
## Configuration | ||
### JSON Recursive Sort | ||
Sort JSON objects recursively, including all nested objects. | ||
| Default | CLI Override | API Override | | ||
| ------- | ----------------------- | --------------------------- | | ||
| `false` | `--json-recursive-sort` | `jsonRecursiveSort: <bool>` | |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
9412
2
7
72
70
0
12
- Removed@babel/types@^7.11.5
- Removedprettier@^2.1.1
- Removed@babel/helper-string-parser@7.25.9(transitive)
- Removed@babel/helper-validator-identifier@7.25.9(transitive)
- Removed@babel/types@7.26.0(transitive)
Updated@types/prettier@^2.2.0