[sort-json] Add sort-json (#45932)

This commit is contained in:
Florian Keller 2020-07-07 19:26:07 +02:00 committed by GitHub
parent d36efb6a51
commit 120445b449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 0 deletions

52
types/sort-json/index.d.ts vendored Normal file
View File

@ -0,0 +1,52 @@
// Type definitions for sort-json 2.0
// Project: https://github.com/kesla/sort-json
// Definitions by: Florian Keller <https://github.com/ffflorian>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace visit {
interface VisitOptions {
/**
* Depth's level sorting keys on a multidimensional object
* (default: `Infinity`)
*/
depth?: number;
/**
* When sorting keys, converts all keys to lowercase so that
* capitalization doesn't interfere with sort order (default: `false`)
*/
ignoreCase?: boolean;
/** Default: `1` */
level?: number;
/** Reverse the ordering z -> a (default: `false`) */
reverse?: boolean;
}
interface OverwriteOptions extends VisitOptions {
/**
* Formats the file content with an indentation of spaces. Use a number
* greater then 0 for the value (default: detects the used indentation
* of the file)
*/
indentSize?: number;
/** Default: `false` */
noFinalNewLine?: boolean;
}
/**
* Sorts the JSON files with the `visit()` function and then overwrites the
* file with sorted JSON
* @param absolutePaths
* * String: Absolute path to JSON file to sort and overwrite
* * Array: Absolute paths to JSON files to sort and overwrite
*/
function overwrite(absolutePaths: string | string[], options?: OverwriteOptions): any;
}
/**
* Sorts the keys on objects
* @param old An object to sort the keys of, if not object just returns whatever
* was given
*/
declare function visit<T>(old: T, options?: visit.VisitOptions): T;
export = visit;

View File

@ -0,0 +1,7 @@
import sortJson = require('sort-json');
const options: sortJson.VisitOptions = { ignoreCase: true, reverse: true, depth: 1 };
sortJson({ AA: 123, a: 1, b: 21 }, options);
sortJson.overwrite('some/absolute/path.json', options);
sortJson.overwrite(['some/absolute/path1.json', 'some/absolute/path2.json'], options);

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"sort-json-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }