@tamtamchik/json-deep-sort 1.1.0 → 1.1.2

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 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
- [![CircleCI][ico-circleci]][link-circleci]
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-circleci]: https://img.shields.io/circleci/build/github/tamtamchik/json-deep-sort.svg?style=flat-square
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-circleci]: https://app.circleci.com/pipelines/github/tamtamchik/json-deep-sort?branch=main
74
+ [link-scrutinizer]: https://scrutinizer-ci.com/g/tamtamchik/json-deep-sort/
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@tamtamchik/json-deep-sort",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "A comprehensive utility for sorting JSON objects by keys.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
7
- "types": "./dist/index.d.ts",
7
+ "types": "./src/index.ts",
8
8
  "author": "Yuri Tkachenko <yuri.tam.tkachenko@gmail.com>",
9
9
  "license": "MIT",
10
10
  "homepage": "https://github.com/tamtamchik/json-deep-sort#readme",
@@ -32,22 +32,22 @@
32
32
  "json-tools"
33
33
  ],
34
34
  "devDependencies": {
35
- "@types/jest": "29.5.1",
36
- "@types/node": "20.2.5",
37
- "jest": "29.5.0",
38
- "ts-jest": "29.1.0",
39
- "tsup": "6.7.0",
40
- "typescript": "5.0.4"
35
+ "@types/jest": "29.5.3",
36
+ "@types/node": "20.5.1",
37
+ "jest": "29.6.2",
38
+ "ts-jest": "29.1.1",
39
+ "tsup": "7.2.0",
40
+ "typescript": "5.1.6"
41
41
  },
42
42
  "exports": {
43
43
  ".": {
44
44
  "require": "./dist/index.js",
45
45
  "import": "./dist/index.mjs",
46
- "types": "./dist/index.d.ts"
46
+ "types": "./src/index.ts"
47
47
  }
48
48
  },
49
49
  "scripts": {
50
- "build": "tsup src/index.ts --format cjs,esm --dts --clean",
50
+ "build": "tsup src/index.ts --format cjs,esm --clean",
51
51
  "dev": "npm run build -- --watch src",
52
52
  "prepublishOnly": "npm run build",
53
53
  "test": "jest",
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
+ }
package/dist/index.d.ts DELETED
@@ -1,10 +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
- declare function sort(data: any, asc?: boolean): any;
9
-
10
- export { sort };