Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

prettier-plugin-sort-json

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prettier-plugin-sort-json - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

CHANGELOG.md

11

index.d.ts
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";
};
};
"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

30

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc