@tamtamchik/json-deep-sort 1.1.4 → 1.2.0
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/dist/index.js +18 -24
- package/dist/index.mjs +18 -24
- package/package.json +4 -4
- package/src/index.ts +27 -42
package/dist/index.js
CHANGED
|
@@ -24,33 +24,27 @@ __export(src_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(src_exports);
|
|
26
26
|
function getType(data) {
|
|
27
|
-
if (Array.isArray(data))
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return 0 /* OBJECT */;
|
|
31
|
-
} else if (typeof data === "string") {
|
|
32
|
-
return 2 /* STRING */;
|
|
33
|
-
} else {
|
|
34
|
-
return 3 /* OTHER */;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
function isRecursiveType(data) {
|
|
38
|
-
return [0 /* OBJECT */, 1 /* ARRAY */].includes(getType(data));
|
|
27
|
+
if (Array.isArray(data)) return "array" /* ARRAY */;
|
|
28
|
+
if (data !== null && typeof data === "object") return "object" /* OBJECT */;
|
|
29
|
+
return "primitive" /* PRIMITIVE */;
|
|
39
30
|
}
|
|
40
31
|
function sort(data, asc = true) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
32
|
+
const type = getType(data);
|
|
33
|
+
if (type === "array" /* ARRAY */) {
|
|
34
|
+
return data.map(
|
|
35
|
+
(item) => getType(item) !== "primitive" /* PRIMITIVE */ ? sort(item, asc) : item
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
if (type === "object" /* OBJECT */) {
|
|
39
|
+
const sortedKeys = Object.keys(data).sort();
|
|
40
|
+
if (!asc) sortedKeys.reverse();
|
|
41
|
+
return sortedKeys.reduce((result, key) => {
|
|
42
|
+
const value = data[key];
|
|
43
|
+
result[key] = getType(value) !== "primitive" /* PRIMITIVE */ ? sort(value, asc) : value;
|
|
44
|
+
return result;
|
|
45
|
+
}, {});
|
|
53
46
|
}
|
|
47
|
+
return data;
|
|
54
48
|
}
|
|
55
49
|
// Annotate the CommonJS export names for ESM import in node:
|
|
56
50
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -1,32 +1,26 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
function getType(data) {
|
|
3
|
-
if (Array.isArray(data))
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return 0 /* OBJECT */;
|
|
7
|
-
} else if (typeof data === "string") {
|
|
8
|
-
return 2 /* STRING */;
|
|
9
|
-
} else {
|
|
10
|
-
return 3 /* OTHER */;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
function isRecursiveType(data) {
|
|
14
|
-
return [0 /* OBJECT */, 1 /* ARRAY */].includes(getType(data));
|
|
3
|
+
if (Array.isArray(data)) return "array" /* ARRAY */;
|
|
4
|
+
if (data !== null && typeof data === "object") return "object" /* OBJECT */;
|
|
5
|
+
return "primitive" /* PRIMITIVE */;
|
|
15
6
|
}
|
|
16
7
|
function sort(data, asc = true) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
8
|
+
const type = getType(data);
|
|
9
|
+
if (type === "array" /* ARRAY */) {
|
|
10
|
+
return data.map(
|
|
11
|
+
(item) => getType(item) !== "primitive" /* PRIMITIVE */ ? sort(item, asc) : item
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
if (type === "object" /* OBJECT */) {
|
|
15
|
+
const sortedKeys = Object.keys(data).sort();
|
|
16
|
+
if (!asc) sortedKeys.reverse();
|
|
17
|
+
return sortedKeys.reduce((result, key) => {
|
|
18
|
+
const value = data[key];
|
|
19
|
+
result[key] = getType(value) !== "primitive" /* PRIMITIVE */ ? sort(value, asc) : value;
|
|
20
|
+
return result;
|
|
21
|
+
}, {});
|
|
29
22
|
}
|
|
23
|
+
return data;
|
|
30
24
|
}
|
|
31
25
|
export {
|
|
32
26
|
sort
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamtamchik/json-deep-sort",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A comprehensive utility for sorting JSON objects by keys.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -33,10 +33,10 @@
|
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/jest": "^29.5.12",
|
|
36
|
-
"@types/node": "^
|
|
36
|
+
"@types/node": "^22.3.0",
|
|
37
37
|
"jest": "^29.7.0",
|
|
38
|
-
"ts-jest": "^29.2.
|
|
39
|
-
"tsup": "^8.2.
|
|
38
|
+
"ts-jest": "^29.2.4",
|
|
39
|
+
"tsup": "^8.2.4",
|
|
40
40
|
"typescript": "^5.5.4"
|
|
41
41
|
},
|
|
42
42
|
"exports": {
|
package/src/index.ts
CHANGED
|
@@ -3,62 +3,47 @@
|
|
|
3
3
|
* @enum {string}
|
|
4
4
|
*/
|
|
5
5
|
enum DataType {
|
|
6
|
-
OBJECT,
|
|
7
|
-
ARRAY,
|
|
8
|
-
|
|
9
|
-
OTHER,
|
|
6
|
+
OBJECT = 'object',
|
|
7
|
+
ARRAY = 'array',
|
|
8
|
+
PRIMITIVE = 'primitive'
|
|
10
9
|
}
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
12
|
* Function to determine the data type of the provided data.
|
|
14
|
-
* @param {
|
|
13
|
+
* @param {unknown} data - The data to be checked.
|
|
15
14
|
* @returns {DataType} The DataType value corresponding to the data's type.
|
|
16
15
|
*/
|
|
17
|
-
function getType
|
|
18
|
-
if (Array.isArray(data))
|
|
19
|
-
|
|
20
|
-
|
|
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))
|
|
16
|
+
function getType(data: unknown): DataType {
|
|
17
|
+
if (Array.isArray(data)) return DataType.ARRAY;
|
|
18
|
+
if (data !== null && typeof data === 'object') return DataType.OBJECT;
|
|
19
|
+
return DataType.PRIMITIVE;
|
|
36
20
|
}
|
|
37
21
|
|
|
38
22
|
/**
|
|
39
23
|
* Function to sort JSON data.
|
|
40
|
-
* @param {
|
|
24
|
+
* @param {unknown} data - The data to be sorted.
|
|
41
25
|
* @param {boolean} [asc=true] - Whether to sort in ascending order.
|
|
42
|
-
* @returns {
|
|
43
|
-
* @throws {Error} If the data is not an object or array.
|
|
26
|
+
* @returns {unknown} The sorted data.
|
|
44
27
|
*/
|
|
45
|
-
export function sort
|
|
46
|
-
|
|
28
|
+
export function sort(data: unknown, asc: boolean = true): unknown {
|
|
29
|
+
const type = getType(data);
|
|
47
30
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (!asc) keys.reverse()
|
|
31
|
+
if (type === DataType.ARRAY) {
|
|
32
|
+
return (data as unknown[]).map(item =>
|
|
33
|
+
getType(item) !== DataType.PRIMITIVE ? sort(item, asc) : item
|
|
34
|
+
);
|
|
35
|
+
}
|
|
55
36
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}, {})
|
|
37
|
+
if (type === DataType.OBJECT) {
|
|
38
|
+
const sortedKeys = Object.keys(data as object).sort();
|
|
39
|
+
if (!asc) sortedKeys.reverse();
|
|
60
40
|
|
|
61
|
-
|
|
62
|
-
|
|
41
|
+
return sortedKeys.reduce((result: Record<string, unknown>, key) => {
|
|
42
|
+
const value = (data as Record<string, unknown>)[key];
|
|
43
|
+
result[key] = getType(value) !== DataType.PRIMITIVE ? sort(value, asc) : value;
|
|
44
|
+
return result;
|
|
45
|
+
}, {});
|
|
63
46
|
}
|
|
47
|
+
|
|
48
|
+
return data;
|
|
64
49
|
}
|