@tamtamchik/json-deep-sort 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -10
- package/package.json +7 -7
- package/src/index.ts +64 -0
package/README.md
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
[![Buy Me A Coffee][ico-coffee]][link-coffee]
|
|
4
4
|
[![Latest Version on NPM][ico-version]][link-npm]
|
|
5
|
-
[![
|
|
5
|
+
[![Scrutinizer build][ico-scrutinizer-build]][link-scrutinizer]
|
|
6
|
+
[![Scrutinizer quality][ico-scrutinizer-quality]][link-scrutinizer]
|
|
7
|
+
[![Scrutinizer coverage][ico-scrutinizer-coverage]][link-scrutinizer]
|
|
6
8
|
[![Software License][ico-license]](LICENSE)
|
|
7
9
|
[![Total Downloads][ico-downloads]][link-downloads]
|
|
8
10
|
|
|
@@ -10,13 +12,6 @@ JSON Deep Sort is a comprehensive package that provides flexible sorting of JSON
|
|
|
10
12
|
TypeScript-written package can handle deeply nested objects and arrays, and offers both synchronous and asynchronous
|
|
11
13
|
operations.
|
|
12
14
|
|
|
13
|
-
## Features
|
|
14
|
-
|
|
15
|
-
- Efficiently sorts keys in JSON objects and arrays.
|
|
16
|
-
- Handles deeply nested JSON objects and arrays.
|
|
17
|
-
- Provides both synchronous and asynchronous sorting operations.
|
|
18
|
-
- Written in TypeScript for robust typing and error checking.
|
|
19
|
-
|
|
20
15
|
## Installation
|
|
21
16
|
|
|
22
17
|
Using npm:
|
|
@@ -69,9 +64,11 @@ JSON Deep Sort is [MIT licensed](./LICENSE).
|
|
|
69
64
|
[ico-version]: https://img.shields.io/npm/v/@tamtamchik/json-deep-sort.svg?style=flat-square
|
|
70
65
|
[ico-license]: https://img.shields.io/npm/l/@tamtamchik/json-deep-sort.svg?style=flat-square
|
|
71
66
|
[ico-downloads]: https://img.shields.io/npm/dt/@tamtamchik/json-deep-sort.svg?style=flat-square
|
|
72
|
-
[ico-
|
|
67
|
+
[ico-scrutinizer-build]: https://img.shields.io/scrutinizer/build/g/tamtamchik/json-deep-sort/main.svg?style=flat-square
|
|
68
|
+
[ico-scrutinizer-quality]: https://img.shields.io/scrutinizer/quality/g/tamtamchik/json-deep-sort/main.svg?style=flat-square
|
|
69
|
+
[ico-scrutinizer-coverage]: https://img.shields.io/scrutinizer/coverage/g/tamtamchik/json-deep-sort/main.svg?style=flat-square
|
|
73
70
|
|
|
74
71
|
[link-coffee]: https://www.buymeacoffee.com/tamtamchik
|
|
75
72
|
[link-npm]: https://www.npmjs.com/package/@tamtamchik/json-deep-sort
|
|
76
73
|
[link-downloads]: https://www.npmjs.com/package/@tamtamchik/json-deep-sort
|
|
77
|
-
[link-
|
|
74
|
+
[link-scrutinizer]: https://scrutinizer-ci.com/g/tamtamchik/json-deep-sort/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamtamchik/json-deep-sort",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "A comprehensive utility for sorting JSON objects by keys.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -32,12 +32,12 @@
|
|
|
32
32
|
"json-tools"
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@types/jest": "29.5.
|
|
36
|
-
"@types/node": "20.
|
|
37
|
-
"jest": "29.
|
|
38
|
-
"ts-jest": "29.1.
|
|
39
|
-
"tsup": "
|
|
40
|
-
"typescript": "5.
|
|
35
|
+
"@types/jest": "^29.5.12",
|
|
36
|
+
"@types/node": "^20.12.8",
|
|
37
|
+
"jest": "^29.7.0",
|
|
38
|
+
"ts-jest": "^29.1.2",
|
|
39
|
+
"tsup": "^8.0.2",
|
|
40
|
+
"typescript": "^5.4.5"
|
|
41
41
|
},
|
|
42
42
|
"exports": {
|
|
43
43
|
".": {
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enumeration of data types.
|
|
3
|
+
* @enum {string}
|
|
4
|
+
*/
|
|
5
|
+
enum DataType {
|
|
6
|
+
OBJECT,
|
|
7
|
+
ARRAY,
|
|
8
|
+
STRING,
|
|
9
|
+
OTHER,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Function to determine the data type of the provided data.
|
|
14
|
+
* @param {any} data - The data to be checked.
|
|
15
|
+
* @returns {DataType} The DataType value corresponding to the data's type.
|
|
16
|
+
*/
|
|
17
|
+
function getType (data: any): DataType {
|
|
18
|
+
if (Array.isArray(data)) {
|
|
19
|
+
return DataType.ARRAY
|
|
20
|
+
} else if (typeof data === 'object' && data !== null) {
|
|
21
|
+
return DataType.OBJECT
|
|
22
|
+
} else if (typeof data === 'string') {
|
|
23
|
+
return DataType.STRING
|
|
24
|
+
} else {
|
|
25
|
+
return DataType.OTHER
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Function to determine whether the provided data is a recursive type.
|
|
31
|
+
* @param {any} data - The data to be checked.
|
|
32
|
+
* @returns {boolean} Whether the data is a recursive type.
|
|
33
|
+
*/
|
|
34
|
+
function isRecursiveType (data: any): boolean {
|
|
35
|
+
return [DataType.OBJECT, DataType.ARRAY].includes(getType(data))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Function to sort JSON data.
|
|
40
|
+
* @param {any} data - The data to be sorted.
|
|
41
|
+
* @param {boolean} [asc=true] - Whether to sort in ascending order.
|
|
42
|
+
* @returns {any} The sorted data.
|
|
43
|
+
* @throws {Error} If the data is not an object or array.
|
|
44
|
+
*/
|
|
45
|
+
export function sort (data: any, asc: boolean = true): any {
|
|
46
|
+
switch (getType(data)) {
|
|
47
|
+
|
|
48
|
+
case DataType.ARRAY:
|
|
49
|
+
return data.map((d: any) => isRecursiveType(d) ? sort(d, asc) : d)
|
|
50
|
+
|
|
51
|
+
case DataType.OBJECT:
|
|
52
|
+
const keys = Object.keys(data).sort()
|
|
53
|
+
|
|
54
|
+
if (!asc) keys.reverse()
|
|
55
|
+
|
|
56
|
+
return keys.reduce((newData: any, key: string) => {
|
|
57
|
+
newData[key] = isRecursiveType(data[key]) ? sort(data[key], asc) : data[key]
|
|
58
|
+
return newData
|
|
59
|
+
}, {})
|
|
60
|
+
|
|
61
|
+
default:
|
|
62
|
+
throw new Error('Invalid data type: expected an object or array of objects.')
|
|
63
|
+
}
|
|
64
|
+
}
|