@tamtamchik/json-deep-sort 1.2.0 → 1.2.1
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 +38 -13
- package/dist/index.js +14 -19
- package/dist/index.mjs +14 -19
- package/package.json +1 -1
- package/src/index.ts +28 -36
package/README.md
CHANGED
|
@@ -8,9 +8,15 @@
|
|
|
8
8
|
[![Software License][ico-license]](LICENSE)
|
|
9
9
|
[![Total Downloads][ico-downloads]][link-downloads]
|
|
10
10
|
|
|
11
|
-
JSON Deep Sort is a
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
JSON Deep Sort is a powerful and versatile TypeScript package designed for efficient sorting of JSON objects by keys. It offers:
|
|
12
|
+
|
|
13
|
+
- Comprehensive handling of deeply nested objects and arrays
|
|
14
|
+
- Flexible sorting options, including ascending and descending order
|
|
15
|
+
- Support for both synchronous and asynchronous operations
|
|
16
|
+
- Preservation of non-sortable objects (e.g., Date, RegExp, Function)
|
|
17
|
+
- Lightweight implementation with zero dependencies
|
|
18
|
+
|
|
19
|
+
Whether you're working with simple flat objects or complex nested structures, JSON Deep Sort provides a reliable solution for organizing your data.
|
|
14
20
|
|
|
15
21
|
## Installation
|
|
16
22
|
|
|
@@ -37,19 +43,38 @@ import { sort } from '@tamtamchik/json-deep-sort';
|
|
|
37
43
|
Here is an example of sorting a JSON object:
|
|
38
44
|
|
|
39
45
|
```typescript
|
|
40
|
-
|
|
46
|
+
// Example 1: Sorting a simple object
|
|
47
|
+
const simpleData = { b: 'b', a: 'a', c: 'c' };
|
|
48
|
+
console.log(sort(simpleData));
|
|
49
|
+
// Output: { a: 'a', b: 'b', c: 'c' }
|
|
50
|
+
|
|
51
|
+
// Example 2: Sorting a nested object
|
|
52
|
+
const nestedData = {
|
|
41
53
|
b: 'b',
|
|
42
54
|
a: 'a',
|
|
43
|
-
c: {
|
|
44
|
-
d: 'd',
|
|
45
|
-
c: 'c',
|
|
46
|
-
a: 'a',
|
|
47
|
-
b: 'b'
|
|
48
|
-
}
|
|
55
|
+
c: { d: 'd', c: 'c', a: 'a', b: 'b' }
|
|
49
56
|
};
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
console.log(sort(nestedData));
|
|
58
|
+
// Output: { a: 'a', b: 'b', c: { a: 'a', b: 'b', c: 'c', d: 'd' } }
|
|
59
|
+
|
|
60
|
+
// Example 3: Sorting an array of objects
|
|
61
|
+
const arrayData = [{ b: 'b', a: 'a' }, { d: 'd', c: 'c' }];
|
|
62
|
+
console.log(sort(arrayData));
|
|
63
|
+
// Output: [{ a: 'a', b: 'b' }, { c: 'c', d: 'd' }]
|
|
64
|
+
|
|
65
|
+
// Example 4: Sorting in descending order
|
|
66
|
+
const descendingData = { a: 'a', c: 'c', b: 'b' };
|
|
67
|
+
console.log(sort(descendingData, false));
|
|
68
|
+
// Output: { c: 'c', b: 'b', a: 'a' }
|
|
69
|
+
|
|
70
|
+
// Example 5: Handling mixed data types
|
|
71
|
+
const mixedData = {
|
|
72
|
+
b: [3, 1, 2],
|
|
73
|
+
a: { z: 'z', y: 'y' },
|
|
74
|
+
c: new Date('2023-01-01')
|
|
75
|
+
};
|
|
76
|
+
console.log(sort(mixedData));
|
|
77
|
+
// Output: { a: { y: 'y', z: 'z' }, b: [3, 1, 2], c: Date('2023-01-01') }
|
|
53
78
|
```
|
|
54
79
|
|
|
55
80
|
## Contributing
|
package/dist/index.js
CHANGED
|
@@ -23,28 +23,23 @@ __export(src_exports, {
|
|
|
23
23
|
sort: () => sort
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
function getType(data) {
|
|
27
|
-
if (Array.isArray(data)) return "array" /* ARRAY */;
|
|
28
|
-
if (data !== null && typeof data === "object") return "object" /* OBJECT */;
|
|
29
|
-
return "primitive" /* PRIMITIVE */;
|
|
30
|
-
}
|
|
31
26
|
function sort(data, asc = true) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
);
|
|
27
|
+
if (!data || typeof data !== "object") {
|
|
28
|
+
return data;
|
|
29
|
+
}
|
|
30
|
+
if (Array.isArray(data)) {
|
|
31
|
+
return data.map((item) => sort(item, asc));
|
|
37
32
|
}
|
|
38
|
-
if (
|
|
39
|
-
|
|
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
|
-
}, {});
|
|
33
|
+
if (isNonSortableObject(data)) {
|
|
34
|
+
return data;
|
|
46
35
|
}
|
|
47
|
-
|
|
36
|
+
const sortedEntries = Object.entries(data).sort(([a], [b]) => asc ? a.localeCompare(b) : b.localeCompare(a));
|
|
37
|
+
return Object.fromEntries(
|
|
38
|
+
sortedEntries.map(([key, value]) => [key, sort(value, asc)])
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
function isNonSortableObject(obj) {
|
|
42
|
+
return obj instanceof Date || obj instanceof RegExp || obj instanceof Function || obj instanceof Error || obj instanceof Map || obj instanceof Set || obj instanceof WeakMap || obj instanceof WeakSet || obj instanceof Promise || Symbol.iterator in Object(obj);
|
|
48
43
|
}
|
|
49
44
|
// Annotate the CommonJS export names for ESM import in node:
|
|
50
45
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -1,26 +1,21 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
function getType(data) {
|
|
3
|
-
if (Array.isArray(data)) return "array" /* ARRAY */;
|
|
4
|
-
if (data !== null && typeof data === "object") return "object" /* OBJECT */;
|
|
5
|
-
return "primitive" /* PRIMITIVE */;
|
|
6
|
-
}
|
|
7
2
|
function sort(data, asc = true) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
);
|
|
3
|
+
if (!data || typeof data !== "object") {
|
|
4
|
+
return data;
|
|
5
|
+
}
|
|
6
|
+
if (Array.isArray(data)) {
|
|
7
|
+
return data.map((item) => sort(item, asc));
|
|
13
8
|
}
|
|
14
|
-
if (
|
|
15
|
-
|
|
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
|
-
}, {});
|
|
9
|
+
if (isNonSortableObject(data)) {
|
|
10
|
+
return data;
|
|
22
11
|
}
|
|
23
|
-
|
|
12
|
+
const sortedEntries = Object.entries(data).sort(([a], [b]) => asc ? a.localeCompare(b) : b.localeCompare(a));
|
|
13
|
+
return Object.fromEntries(
|
|
14
|
+
sortedEntries.map(([key, value]) => [key, sort(value, asc)])
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
function isNonSortableObject(obj) {
|
|
18
|
+
return obj instanceof Date || obj instanceof RegExp || obj instanceof Function || obj instanceof Error || obj instanceof Map || obj instanceof Set || obj instanceof WeakMap || obj instanceof WeakSet || obj instanceof Promise || Symbol.iterator in Object(obj);
|
|
24
19
|
}
|
|
25
20
|
export {
|
|
26
21
|
sort
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,24 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Enumeration of data types.
|
|
3
|
-
* @enum {string}
|
|
4
|
-
*/
|
|
5
|
-
enum DataType {
|
|
6
|
-
OBJECT = 'object',
|
|
7
|
-
ARRAY = 'array',
|
|
8
|
-
PRIMITIVE = 'primitive'
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Function to determine the data type of the provided data.
|
|
13
|
-
* @param {unknown} data - The data to be checked.
|
|
14
|
-
* @returns {DataType} The DataType value corresponding to the data's type.
|
|
15
|
-
*/
|
|
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;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
1
|
/**
|
|
23
2
|
* Function to sort JSON data.
|
|
24
3
|
* @param {unknown} data - The data to be sorted.
|
|
@@ -26,24 +5,37 @@ function getType(data: unknown): DataType {
|
|
|
26
5
|
* @returns {unknown} The sorted data.
|
|
27
6
|
*/
|
|
28
7
|
export function sort(data: unknown, asc: boolean = true): unknown {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (type === DataType.ARRAY) {
|
|
32
|
-
return (data as unknown[]).map(item =>
|
|
33
|
-
getType(item) !== DataType.PRIMITIVE ? sort(item, asc) : item
|
|
34
|
-
);
|
|
8
|
+
if (!data || typeof data !== 'object') {
|
|
9
|
+
return data;
|
|
35
10
|
}
|
|
36
11
|
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
|
|
12
|
+
if (Array.isArray(data)) {
|
|
13
|
+
return data.map(item => sort(item, asc));
|
|
14
|
+
}
|
|
40
15
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
result[key] = getType(value) !== DataType.PRIMITIVE ? sort(value, asc) : value;
|
|
44
|
-
return result;
|
|
45
|
-
}, {});
|
|
16
|
+
if (isNonSortableObject(data)) {
|
|
17
|
+
return data;
|
|
46
18
|
}
|
|
47
19
|
|
|
48
|
-
|
|
20
|
+
const sortedEntries = Object.entries(data as Record<string, unknown>)
|
|
21
|
+
.sort(([a], [b]) => asc ? a.localeCompare(b) : b.localeCompare(a));
|
|
22
|
+
|
|
23
|
+
return Object.fromEntries(
|
|
24
|
+
sortedEntries.map(([key, value]) => [key, sort(value, asc)])
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isNonSortableObject(obj: object): boolean {
|
|
29
|
+
return (
|
|
30
|
+
obj instanceof Date ||
|
|
31
|
+
obj instanceof RegExp ||
|
|
32
|
+
obj instanceof Function ||
|
|
33
|
+
obj instanceof Error ||
|
|
34
|
+
obj instanceof Map ||
|
|
35
|
+
obj instanceof Set ||
|
|
36
|
+
obj instanceof WeakMap ||
|
|
37
|
+
obj instanceof WeakSet ||
|
|
38
|
+
obj instanceof Promise ||
|
|
39
|
+
Symbol.iterator in Object(obj)
|
|
40
|
+
);
|
|
49
41
|
}
|