@tamtamchik/json-deep-sort 0.1.0 → 1.0.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/.circleci/config.yml +23 -0
- package/README.md +20 -5
- package/package.json +3 -2
- package/.idea/GitLink.xml +0 -6
- package/.idea/json-deep-sort.iml +0 -13
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/cjs/index.d.ts +0 -8
- package/dist/esm/index.d.ts +0 -8
- package/jest.config.js +0 -6
- package/src/index.ts +0 -62
- package/test/sort.test.ts +0 -98
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
version: 2.1
|
|
2
|
+
jobs:
|
|
3
|
+
build:
|
|
4
|
+
docker:
|
|
5
|
+
- image: cimg/node:20.2.0
|
|
6
|
+
steps:
|
|
7
|
+
- checkout
|
|
8
|
+
- restore_cache:
|
|
9
|
+
keys:
|
|
10
|
+
- v1-dependencies-{{ checksum "package-lock.json" }}
|
|
11
|
+
- v1-dependencies-
|
|
12
|
+
- run: npm ci
|
|
13
|
+
- run: npm test
|
|
14
|
+
- run: npm run coverage
|
|
15
|
+
- save_cache:
|
|
16
|
+
paths:
|
|
17
|
+
- node_modules
|
|
18
|
+
key: v1-dependencies-{{ checksum "package-lock.json" }}
|
|
19
|
+
workflows:
|
|
20
|
+
version: 2
|
|
21
|
+
build_and_test:
|
|
22
|
+
jobs:
|
|
23
|
+
- build
|
package/README.md
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
# JSON Deep Sort
|
|
2
2
|
|
|
3
|
-
![
|
|
4
|
-
![
|
|
3
|
+
[![Buy Me A Coffee][ico-coffee]][link-coffee]
|
|
4
|
+
[![Latest Version on NPM][ico-version]][link-npm]
|
|
5
|
+
[![CircleCI][ico-circleci]][link-circleci]
|
|
6
|
+
[![Software License][ico-license]](LICENSE)
|
|
7
|
+
[![Total Downloads][ico-downloads]][link-downloads]
|
|
5
8
|
|
|
6
9
|
JSON Deep Sort is a comprehensive package that provides flexible sorting of JSON objects by keys. This
|
|
7
10
|
TypeScript-written package can handle deeply nested objects and arrays, and offers both synchronous and asynchronous
|
|
@@ -19,13 +22,13 @@ operations.
|
|
|
19
22
|
Using npm:
|
|
20
23
|
|
|
21
24
|
```shell
|
|
22
|
-
npm install json-deep-sort
|
|
25
|
+
npm install @tamtamchik/json-deep-sort
|
|
23
26
|
```
|
|
24
27
|
|
|
25
28
|
Using yarn:
|
|
26
29
|
|
|
27
30
|
```shell
|
|
28
|
-
yarn add json-deep-sort
|
|
31
|
+
yarn add @tamtamchik/json-deep-sort
|
|
29
32
|
```
|
|
30
33
|
|
|
31
34
|
## Usage
|
|
@@ -49,8 +52,9 @@ let data = {
|
|
|
49
52
|
b: 'b'
|
|
50
53
|
}
|
|
51
54
|
};
|
|
55
|
+
|
|
52
56
|
let sortedData = sort(data, true);
|
|
53
|
-
|
|
57
|
+
// { a: 'a', b: 'b', c: { a: 'a', b: 'b', c: 'c', d: 'd' } }
|
|
54
58
|
```
|
|
55
59
|
|
|
56
60
|
## Contributing
|
|
@@ -60,3 +64,14 @@ Pull requests are welcome. For major changes, please open an issue first to disc
|
|
|
60
64
|
## License
|
|
61
65
|
|
|
62
66
|
JSON Deep Sort is [MIT licensed](./LICENSE).
|
|
67
|
+
|
|
68
|
+
[ico-coffee]: https://img.shields.io/badge/Buy%20Me%20A-Coffee-%236F4E37.svg?style=flat-square
|
|
69
|
+
[ico-version]: https://img.shields.io/npm/v/@tamtamchik/json-deep-sort.svg?style=flat-square
|
|
70
|
+
[ico-license]: https://img.shields.io/npm/l/@tamtamchik/json-deep-sort.svg?style=flat-square
|
|
71
|
+
[ico-downloads]: https://img.shields.io/npm/dt/@tamtamchik/json-deep-sort.svg?style=flat-square
|
|
72
|
+
[ico-circleci]: https://img.shields.io/circleci/build/github/tamtamchik/json-deep-sort.svg?style=flat-square
|
|
73
|
+
|
|
74
|
+
[link-coffee]: https://www.buymeacoffee.com/tamtamchik
|
|
75
|
+
[link-npm]: https://www.npmjs.com/package/@tamtamchik/json-deep-sort
|
|
76
|
+
[link-downloads]: https://www.npmjs.com/package/@tamtamchik/json-deep-sort
|
|
77
|
+
[link-circleci]: https://app.circleci.com/pipelines/github/tamtamchik/json-deep-sort?branch=main
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamtamchik/json-deep-sort",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "A comprehensive utility for sorting JSON objects by keys.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -53,6 +53,7 @@
|
|
|
53
53
|
"build": "npm run build:cjs && npm run build:esm",
|
|
54
54
|
"dev": "tsc --watch",
|
|
55
55
|
"prepare": "npm run build",
|
|
56
|
-
"test": "jest"
|
|
56
|
+
"test": "jest",
|
|
57
|
+
"coverage": "jest --coverage"
|
|
57
58
|
}
|
|
58
59
|
}
|
package/.idea/GitLink.xml
DELETED
package/.idea/json-deep-sort.iml
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<module type="WEB_MODULE" version="4">
|
|
3
|
-
<component name="NewModuleRootManager">
|
|
4
|
-
<content url="file://$MODULE_DIR$">
|
|
5
|
-
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
|
6
|
-
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
|
7
|
-
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
|
8
|
-
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
|
9
|
-
</content>
|
|
10
|
-
<orderEntry type="inheritedJdk" />
|
|
11
|
-
<orderEntry type="sourceFolder" forTests="false" />
|
|
12
|
-
</component>
|
|
13
|
-
</module>
|
package/.idea/modules.xml
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<project version="4">
|
|
3
|
-
<component name="ProjectModuleManager">
|
|
4
|
-
<modules>
|
|
5
|
-
<module fileurl="file://$PROJECT_DIR$/.idea/json-deep-sort.iml" filepath="$PROJECT_DIR$/.idea/json-deep-sort.iml" />
|
|
6
|
-
</modules>
|
|
7
|
-
</component>
|
|
8
|
-
</project>
|
package/.idea/vcs.xml
DELETED
package/dist/cjs/index.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Function to sort JSON data.
|
|
3
|
-
* @param {any} data - The data to be sorted.
|
|
4
|
-
* @param {boolean} [asc=true] - Whether to sort in ascending order.
|
|
5
|
-
* @returns {any} The sorted data.
|
|
6
|
-
* @throws {Error} If the data is not an object or array.
|
|
7
|
-
*/
|
|
8
|
-
export declare function sort(data: any, asc?: boolean): any;
|
package/dist/esm/index.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Function to sort JSON data.
|
|
3
|
-
* @param {any} data - The data to be sorted.
|
|
4
|
-
* @param {boolean} [asc=true] - Whether to sort in ascending order.
|
|
5
|
-
* @returns {any} The sorted data.
|
|
6
|
-
* @throws {Error} If the data is not an object or array.
|
|
7
|
-
*/
|
|
8
|
-
export declare function sort(data: any, asc?: boolean): any;
|
package/jest.config.js
DELETED
package/src/index.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Enumeration of data types.
|
|
3
|
-
* @enum {string}
|
|
4
|
-
*/
|
|
5
|
-
enum DataType {
|
|
6
|
-
OBJECT = 'OBJECT',
|
|
7
|
-
ARRAY = 'ARRAY',
|
|
8
|
-
STRING = 'STRING',
|
|
9
|
-
OTHER = '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
|
-
const objectConstructor = {}.constructor
|
|
19
|
-
const arrayConstructor = [].constructor
|
|
20
|
-
const stringConstructor = ''.constructor
|
|
21
|
-
|
|
22
|
-
if (data && data.constructor === objectConstructor) {
|
|
23
|
-
return DataType.OBJECT
|
|
24
|
-
} else if (data && data.constructor === arrayConstructor) {
|
|
25
|
-
return DataType.ARRAY
|
|
26
|
-
} else if (data && data.constructor === stringConstructor) {
|
|
27
|
-
return DataType.STRING
|
|
28
|
-
} else {
|
|
29
|
-
return DataType.OTHER
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Function to sort JSON data.
|
|
35
|
-
* @param {any} data - The data to be sorted.
|
|
36
|
-
* @param {boolean} [asc=true] - Whether to sort in ascending order.
|
|
37
|
-
* @returns {any} The sorted data.
|
|
38
|
-
* @throws {Error} If the data is not an object or array.
|
|
39
|
-
*/
|
|
40
|
-
export function sort (data: any, asc = true): any {
|
|
41
|
-
switch (getType(data)) {
|
|
42
|
-
|
|
43
|
-
case DataType.ARRAY:
|
|
44
|
-
return data.map((d: any) => [DataType.OBJECT, DataType.ARRAY].includes(getType(d)) ? sort(d, asc) : d)
|
|
45
|
-
|
|
46
|
-
case DataType.OBJECT:
|
|
47
|
-
const keys = Object.keys(data).sort()
|
|
48
|
-
|
|
49
|
-
if (!asc) keys.reverse()
|
|
50
|
-
|
|
51
|
-
return keys.reduce((newData: any, key: string) => {
|
|
52
|
-
if ([DataType.OBJECT, DataType.ARRAY].includes(getType(data[key])))
|
|
53
|
-
newData[key] = sort(data[key], asc)
|
|
54
|
-
else
|
|
55
|
-
newData[key] = data[key]
|
|
56
|
-
return newData
|
|
57
|
-
}, {})
|
|
58
|
-
|
|
59
|
-
default:
|
|
60
|
-
throw new Error('Invalid data type: expected an object or array.')
|
|
61
|
-
}
|
|
62
|
-
}
|
package/test/sort.test.ts
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { sort } from '../src'
|
|
2
|
-
|
|
3
|
-
describe('sort', () => {
|
|
4
|
-
it('should sort an object', () => {
|
|
5
|
-
const input = { b: 'b', a: 'a' }
|
|
6
|
-
const expected = { a: 'a', b: 'b' }
|
|
7
|
-
expect(sort(input)).toEqual(expected)
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
it('should not sort an simple array', () => {
|
|
11
|
-
const input = ['a', 'b']
|
|
12
|
-
const expected = ['a', 'b']
|
|
13
|
-
expect(sort(input)).toEqual(expected)
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
it('should sort an array of objects', () => {
|
|
17
|
-
const input = [
|
|
18
|
-
{ b: 'b', a: 'a' },
|
|
19
|
-
{ d: 'd', c: 'c' },
|
|
20
|
-
]
|
|
21
|
-
const expected = [
|
|
22
|
-
{ a: 'a', b: 'b' },
|
|
23
|
-
{ c: 'c', d: 'd' },
|
|
24
|
-
]
|
|
25
|
-
expect(sort(input)).toEqual(expected)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('should sort an object with array values', () => {
|
|
29
|
-
const input = {
|
|
30
|
-
b: ['b', 'a'],
|
|
31
|
-
a: ['d', 'c', 'b', 'a'],
|
|
32
|
-
}
|
|
33
|
-
const expected = {
|
|
34
|
-
a: ['d', 'c', 'b', 'a'],
|
|
35
|
-
b: ['b', 'a'],
|
|
36
|
-
}
|
|
37
|
-
expect(sort(input)).toEqual(expected)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('should sort an object with nested objects', () => {
|
|
41
|
-
const input = {
|
|
42
|
-
a: 'a',
|
|
43
|
-
b: {
|
|
44
|
-
b: 'b',
|
|
45
|
-
a: 'a',
|
|
46
|
-
},
|
|
47
|
-
}
|
|
48
|
-
const expected = {
|
|
49
|
-
a: 'a',
|
|
50
|
-
b: {
|
|
51
|
-
a: 'a',
|
|
52
|
-
b: 'b',
|
|
53
|
-
},
|
|
54
|
-
}
|
|
55
|
-
expect(sort(input)).toEqual(expected)
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
it('should not sort an array of simple arrays', () => {
|
|
59
|
-
const input = [
|
|
60
|
-
['b', 'a'],
|
|
61
|
-
['d', 'c', 'b', 'a'],
|
|
62
|
-
]
|
|
63
|
-
const expected = [
|
|
64
|
-
['b', 'a'],
|
|
65
|
-
['d', 'c', 'b', 'a'],
|
|
66
|
-
]
|
|
67
|
-
expect(sort(input)).toEqual(expected)
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
it('should sort an array containing objects', () => {
|
|
71
|
-
const input = [
|
|
72
|
-
{ b: 'b', a: 'a' },
|
|
73
|
-
{ d: 'd', c: 'c', b: 'b', a: 'a' },
|
|
74
|
-
]
|
|
75
|
-
const expected = [
|
|
76
|
-
{ a: 'a', b: 'b' },
|
|
77
|
-
{ a: 'a', b: 'b', c: 'c', d: 'd' },
|
|
78
|
-
]
|
|
79
|
-
expect(sort(input)).toEqual(expected)
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
it('should sort an object in descending order', () => {
|
|
83
|
-
const input = { b: 'b', a: 'a', c: 'c' }
|
|
84
|
-
const expected = { c: 'c', b: 'b', a: 'a' }
|
|
85
|
-
expect(sort(input, false)).toEqual(expected)
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
it('should throw an error for non-object, non-array input', () => {
|
|
89
|
-
const input = 'string'
|
|
90
|
-
expect(() => sort(input)).toThrow('Invalid data type: expected an object or array.')
|
|
91
|
-
|
|
92
|
-
const inputNumber = 123
|
|
93
|
-
expect(() => sort(inputNumber)).toThrow('Invalid data type: expected an object or array.')
|
|
94
|
-
|
|
95
|
-
const inputNull = null
|
|
96
|
-
expect(() => sort(inputNull)).toThrow('Invalid data type: expected an object or array.')
|
|
97
|
-
})
|
|
98
|
-
})
|